4

I am fetching a problem with Hibernate @EmbeddedId. Code of my @EmbeddedId is:

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

My entity class is :

@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")
}

Problem is : I get data from 'Enrol_reg_principal_info' class when both enrolNo and regNo have value. But get NUllPointerException when either enrolNo or regNo have value.

The hql is : String hql = " from Enrol_reg_principal_info prin where prin.enrolReg.regNo=:id"; for get value for regNo.

String hql = " from Enrol_reg_principal_info prin where prin.enrolReg.enrolNo=:ec"; for get value for enrolNO.

And the method is :

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;
}

Please help. Thanks in advance

aich.pom
  • 173
  • 1
  • 13
  • Can you show the code of class where you are trying to fetch? – codeLover Aug 31 '18 at 07:47
  • Enrol_reg_principal_info info = new Enrol_reg_principal_info(); info = (Enrol_reg_principal_info) q.uniqueResult(); @codeLover – aich.pom Aug 31 '18 at 08:32
  • Please share the entire method by editing your post – codeLover Aug 31 '18 at 08:33
  • Please share complete exception trace too – codeLover Aug 31 '18 at 08:52
  • java.lang.NullPointerException at com.rest.spring.daoImplementation.MasterDetailsImp.masterDetailsRC(MasterDetailsImp.java:212) at java.lang.reflect.Method.invoke(Method.java:606) -Here 212 line is _info = (Enrol_reg_principal_info) q.uniqueResult();_ – aich.pom Aug 31 '18 at 08:55

2 Answers2

5

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.

Karthik R
  • 5,523
  • 2
  • 18
  • 30
  • I make the getters and setters @Karthik R – aich.pom Aug 31 '18 at 08:34
  • Frankly I don't think its a correct explanation as @aich.pom is trying to fetch result via HQL not by findById method – codeLover Aug 31 '18 at 08:51
  • How equals() and hashcode() method will set for EmbeddedId? @@Karthik R – aich.pom Aug 31 '18 at 10:08
  • 1
    @aich.pom, by overriding. use IDE default and generate a good one. equals and hashcode is not reason for exception, but just an addon which EmbeddedId asks for as per documentation. – Karthik R Aug 31 '18 at 10:16
  • 2
    @codeLover , I agree with Karthik that this answers the question. The problem is not in the query, but in the way the entity class is modeled. As stated in the jboss doc referenced above, "for composite ids, no part can be null". If the column contains null values, then EmbeddedId is not a good candidate for modeling the composite key. – chaserb Apr 26 '19 at 04:54
1

I think the issue is that you are trying to fetch results via uniqueResult method. Since it is a composite primary key there is a possibility that for the ec sent by you there are multiple records available in database and there is a thumb rule with uniqueResult that you should fetch one result only(even if no record is found then also you will face exception).

Try fetching the results as :

List<Enrol_reg_principal_info> info = (List<Enrol_reg_principal_info>) q.list();
codeLover
  • 2,571
  • 1
  • 11
  • 27
  • I use your idea . But the issue is not solved.@codeLover – aich.pom Aug 31 '18 at 09:09
  • Still the same exception? Also please share the query getting displayed in cosole – codeLover Aug 31 '18 at 09:12
  • Yes. There is no problem with the query. I check it. – aich.pom Aug 31 '18 at 10:06
  • How many records do you have in database for the value of ec your are passing... and is not your stacktrace telling what is null..coz there is no probabilty of having null in the line mentioned by you – codeLover Aug 31 '18 at 10:14
  • I have only one value for one ec. And not telling what is null is my problem. I can't understand the problem and can't resolved it. – aich.pom Aug 31 '18 at 11:17
  • After checking same mistake I get result from your solution. But when I write code: String fname=info.get(0).getF_name(); I get the error NullPointerException – aich.pom Sep 04 '18 at 10:49