JPA实体映射问题:在实体类中引入新字段后所有字段映射为空

问题描述 投票:0回答:1

为什么JPA实体中引入新字段后所有字段都映射为null,而ArrayList的大小是正确的但所有元素都是null?

对于这个问题有什么想法吗?

@Entity
@IDClass(YourEntity.class)
@Table(name = "your_table")
public class YourEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = true) //
    private String existingField;

    @Column(nullable = true) //
    private String newField;

}

存储库类

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import javax.persistence.Tuple;
import java.util.List;

@Repository
public interface YourEntityRepository extends JpaRepository<YourEntity, Long> {

    List<Tuple> findByIDIgnoreCase(String type, String method);
}

服务等级

List<Tuple> result = yourRepository.findByIDIgnoreCase(type, method);
java spring jpa spring-data-jpa spring-data
1个回答
0
投票

我找到了 JPA 实体映射问题的解决方案。它源于在我的实体类中使用@IdClass注释。删除它并仅对一个字段使用 @Id 解决了该问题。始终仔细检查注释,因为看似不相关的注释可能会显着影响映射行为。

© www.soinside.com 2019 - 2024. All rights reserved.