-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Xml persistence not-null empty string problem
PostPosted: Mon Jan 15, 2007 9:13 pm 
Newbie

Joined: Mon Jan 15, 2007 7:17 pm
Posts: 1
Location: Milan, Italy
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.