gpt4 book ai didi

hibernate - 如果@EmbeddedId的Any字段值为null,会出现什么问题?

转载 作者:行者123 更新时间:2023-12-05 02:15:20 26 4
gpt4 key购买 nike

我正在获取 Hibernate @EmbeddedId 的问题。我的@EmbeddedId 的代码是:

@Embeddable
public class EnrolRegEmbededId implements Serializable
{
@Column(name="ENROL_NO")
private String enrolNo;
@Column(name="REG_NO")
private String regNo;
}

我的实体类是:

@Entity
@Table(name = "PTAX_ENROL_REG_PRINCIPAL_INFO")
public class Enrol_reg_principal_info implements Serializable {

@EmbeddedId
private EnrolRegEmbededId enrolReg;
@Column(name = "APPLN_TYPE")
private String type;
@Column(name = "FIRST_NM")
private String f_name;
@Column(name = "MIDDLE_NM")
private String m_name;
@Column(name = "LAST_NM")
}

问题是:当 enrolNo 和 regNo 都具有值时,我从“Enrol_reg_principal_info”类获取数据。但是当 enrolNo 或 regNo 有值时得到 NUllPointerException。

hql 是: String hql = "from Enrol_reg_principal_info prin where prin.enrolReg.regNo=:id"; 获取 regNo 的值。

String hql = "from Enrol_reg_principal_info prin where prin.enrolReg.enrolNo=:ec";获取 enrolNO 的值。

方法是:

public EnrolRegPrinModel masterDetailsEC(String EC) throws Exception {
EnrolRegPrinModel ecDetails = new EnrolRegPrinModel();
Enrol_reg_principal_info info = new Enrol_reg_principal_info();

Session s = null;
try {
s = sessionFactory.openSession();
String hql = " from Enrol_reg_principal_info prin where
prin.enrolReg.enrolNo=:ec";
Query q = s.createQuery(hql);
q.setString("ec", EC);
info = (Enrol_reg_principal_info) q.uniqueResult();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
s.close();

}
return ecDetails;
}

请帮忙。提前致谢

最佳答案

当您说 EmbeddedId 时,它表示复合主键,它需要一个非空且唯一的值。 JPA 期望这些列是非空且唯一的。根据此选择您的列。

hibernate 引用: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite-aggregated

此外,我不确定您是否已经有了 getter 和 setter。嵌入式 id 类需要正确设置 equals()hashcode() 方法。

关于hibernate - 如果@EmbeddedId的Any字段值为null,会出现什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52110754/

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