Hibernate does not allow an embedded object with an int field to be null?
Posted
by Jason Novak
on Stack Overflow
See other posts from Stack Overflow
or by Jason Novak
Published on 2010-05-15T01:23:27Z
Indexed on
2010/05/15
1:24 UTC
Read the original article
Hit count: 247
hibernate
Hibernate does not allow me to persist an object that contains an null embedded object with an integer field. For example, if I have a class called Thing that looks like this
@Entity
public class Thing {
@Id
public String id;
public Part part;
}
where Part is an embeddable class that looks like this
@Embeddable
public class Part {
public String a;
public int b;
}
then trying to persist a Thing object with a null Part causes Hibernate to throw an Exception. In particular, this code
Thing th = new Thing();
th.id = "thing.1";
th.part = null;
session.saveOrUpdate(th);
causes Hibernate to throw this Exception
org.hibernate.PropertyValueException: not-null property references a null or transient value: com.ace.moab.api.jobs.Thing.part
My guess is that this is happening because Part is an embedded class and so Part.a and Part.b are simply columns in the Thing database table. Since the Thing.part is null Hibernate wants to set the Part.a and Part.b column values to null for the row for thing.1. However, Part.b is an integer and Hibernate will not allow integer columns in the database to be null. This is what causes the Exception, right?
So I am looking for workarounds for this problem. I noticed making Part.b an Integer instead of an int seems to work, but for reasons I won't bore you with this is not a good option for us. Thanks!
© Stack Overflow or respective owner