Hi, I've a problem loading an object via
dom4jSession with a
not-null string property when the string is
empty (NOTE: I have an empty string: "", not a string variable with a null value).
Hibernate throws
org.hibernate.PropertyValueException: not-null property references a null or transient value: hemptystring.StringContainer.string when you load the object via
dom4jSession.
I've set up a simple example.
Here it is a simple pojo class with a single string property and its mapping:
Code:
public class StringContainer {
private int id;
private String string;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public StringContainer() {
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
}
Tha mapping:
Code:
<hibernate-mapping>
<class name="hemptystring.StringContainer" table="stringContainer" node="hemptystring.StringContainer">
<id name="id" node="@id">
<generator class="native"/>
</id>
<property name="string" not-null="true" />
</class>
</hibernate-mapping>
And here it is a simple main that throws the exception:
Code:
public static void main(String[] args) throws Exception {
final SessionFactory sessionFactory;
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
try{
StringContainer sc = new StringContainer();
sc.setString("");
session.save(sc);
Session dom4jSession = session.getSession(EntityMode.DOM4J);
dom4jSession.createQuery("from StringContainer").list();
session.getTransaction().commit();
}catch(Exception e){
session.getTransaction().rollback();
throw e;
}
}
Debugging here and there in the Hibernate's code I've located the problem here:
Code:
public abstract class NullableType extends AbstractType {
...
public final Object fromXMLString(String xml, Mapping factory) throws HibernateException {
return xml==null || xml.length()==0 ? null : fromStringValue(xml);
}
...
}
This could be a bug, because xml.length() == 0 does
not mean that the value of the property is null.
If I've an empty string, it would be great if the "" value doesn't became a null value.
They are definetly different.
Please let me know if anyone agree with me. I can easily post a report with a patch on JIRA.
Thanks,
Daniele.