gpt4 book ai didi

java - 使用 findById 获取的实体将所有字段设置为 NULL

转载 作者:行者123 更新时间:2023-12-01 18:00:03 26 4
gpt4 key购买 nike

使用 Hibernate 获取的实体的所有字段都设置为 NULL。我怎样才能避免这种情况?这是我的 Jpa 存储库。

@Repository
public interface PolicyRepository extends JpaRepository<Entity, Long>{
Entity findByEntSeqNo(@Param("entSeqNo") long entSeqNo);
}

调试时,我可以看到获取的数据位于字段名称内 $$_hibernate_interceptor

这是我的实体类

@Entity
@Table(name = "ENTITY_TABLE")
@Lazy(value = false)
public class Entity extends AuditModel {

@Id
@Column(name = "ENT_SEQ_NO")
@GeneratedValue(generator="entitySeq")
@SequenceGenerator(name="entitySeq",sequenceName="\"ENT_SEQ_NO_seq\"", allocationSize=1)
private long entSeqNo;

@Column(name = "ENT_CUSTOMER_CODE")
@NotEmpty(message = "Customer Code is Required.")
private String entCustomerCode;

@Column(name = "ENT_CLAS_CODE")
@NotEmpty(message = "Class Code is Required.")
private String polcClasCode;

@Column(name = "ENT_PROD_CODE")
@NotEmpty(message = "Product Code is Required.")
private String entProdCode;

@Column(name = "ENT_BRANCH_CODE")
@NotEmpty(message = "Branch Code is Required.")
private String entBranchCode;

@Column(name = "ENT_YYY_NO")
@NotEmpty(message = "Yyy Number is Required.")
private String entYyylNo;

@Column(name = "ENT_XX_NO")
private String entXxNo;

@Column(name = "ENT_XXX_NO")
private String entXxxNo;

@Column(name = "ENT_COMMENCEMENT_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Date entCommencementDate;

@Version
@NotNull(message = "Version is Required.")
@Column(name = "VERSION")
private int version;

@Column(name = "ENT_EXPIRY_DATE")
@NotNull(message = "Entity Expiry Date is Required.")
@Temporal(TemporalType.TIMESTAMP)
private Date entExpiryDate;

@Column(name = "ENT_DELIVERY_MODE")
private String entDeliveryMode;

@Column(name = "ENT_COLLECTED_BY")
private String entCollectedBy;

@Column(name = "ENT_COLLECTED_USER_NIC")
private String entCollectedUserNic;

@Column(name = "ENT_COLLECTED_USER_MOBILE")
private String entCollectedUserMobile;

@Column(name = "ENT_COLLECTED_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Date entCollectedDate;

@Column(name = "ENT_POSTED_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Date entPostedDate;

@Column(name = "ENT_POSTED_REF_NO")
private String entPostedRefNo;

@Column(name = "ENT_LANG_CODE")
private String entLangCode;

@Column(name = "ENT_PP1_ID", insertable = false, updatable = false)
private Long entPp1Id;

@Column(name = "ENT_STATUS")
private Short entStatus;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ENT_PP1_ID", referencedColumnName = "ID")
private Pp1 entPp1;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ENT_EPP1_ID", referencedColumnName = "ID")
private Epp1 entEpp1;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "xxx")
private Collection<TaskLog> taskLogColletion;
}

我在这里问这个问题是因为我找不到这种行为的确切原因。所有 Jpa 操作都工作正常,只有当我尝试从数据库中选取详细信息时才会发生这种情况。我也尝试了 getOne 方法,也出现了同样的问题。

最佳答案

您不需要方法:Entity findByEntSeqNo(@Param("entSeqNo") long entSeqNo);在您的jpa接口(interface)中,因为entSeqNo是您实体的ID 。因此,当您想通过 id 获取一个实体时,您只需调用 JpaRepository 提供的 policyRepository.getOne(entSeqNo 的值) 即可。

或者您可以使用CrudRepository提供的policyRepository.findById(entSeqNo的值)它基本上是 JpaRepository 的 super 接口(interface)

Ps:请勿在界面中覆盖这些方法。

关于java - 使用 findById 获取的实体将所有字段设置为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60653937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com