I have a problem with Hibernate @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")
}
I get data from class Enrol_reg_principal_info
when both enrolNo and regNo have a value.
Why do I get NUllPointerException when either enrolNo or regNo have no value?
String hql = " from Enrol_reg_principal_info prin where prin.enrolReg.regNo=:id";
to get a value for regNo;
String hql = " from Enrol_reg_principal_info prin where prin.enrolReg.enrolNo=:ec";
to get a value for 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;
}
When you say EmbeddedId
, it represents a composite primary key, which expects a non-null and unique value. JPA expects these columns to be non-null and unique. Choose your columns in accordance to that.
Hibernate reference: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite-aggregated
Also, I'm not sure if you already have getters and setters. The embedded id class needs to have equals()
and hashcode()
methods properly set.