If Any field value of @EmbeddedId is null, which problem will arise?
Asked Answered
B

3

5

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;
}
Boutte answered 31/8, 2018 at 7:43 Comment(6)
Can you show the code of class where you are trying to fetch?Pit
Enrol_reg_principal_info info = new Enrol_reg_principal_info(); info = (Enrol_reg_principal_info) q.uniqueResult(); @PitBoutte
Please share the entire method by editing your postPit
Please share complete exception trace tooPit
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();Boutte
Clarify via edits, not comments; delete & flag obsolete comments. minimal reproducible example Help centerTenebrous
G
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.

Gerfalcon answered 31/8, 2018 at 7:47 Comment(5)
I make the getters and setters @Karthik RBoutte
Frankly I don't think its a correct explanation as @Boutte is trying to fetch result via HQL not by findById methodPit
How equals() and hashcode() method will set for EmbeddedId? @@Karthik RBoutte
@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.Gerfalcon
@Pit , 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.Reyesreykjavik
P
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();
Pit answered 31/8, 2018 at 9:0 Comment(6)
I use your idea . But the issue is not solved.@PitBoutte
Still the same exception? Also please share the query getting displayed in cosolePit
Yes. There is no problem with the query. I check it.Boutte
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 youPit
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.Boutte
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 NullPointerExceptionBoutte
D
0

A converter converting null to a blank on read or write will do the trick:

@Converter
public class NullToBlankConverter implements AttributeConverter<String, String>
{

    @Override
    public String convertToDatabaseColumn(String attribute)
    {
        return nullSafe(attribute);
    }

    @Override
    public String convertToEntityAttribute(String attribute)
    {
        return nullSafe(attribute);
    }

    private String nullSafe(String attribute)
    {
        return attribute == null ? "" : attribute;
    }
}

apply this converter on your attribute

@Convert(converter = NullToBlankConverter.class)
@Column(name = "MIDDLE_NM")
private String m_name;
Drogheda answered 31/7, 2024 at 11:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.