-->
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.  [ 2 posts ] 
Author Message
 Post subject: not-null property references a null or transient value
PostPosted: Tue Oct 13, 2009 1:31 pm 
Newbie

Joined: Wed Jun 17, 2009 5:27 pm
Posts: 9
I've created a UserType (see below) to handle a situation in our mySQL database where we've been saving null dates as 0000-00-00 00:00:00.

When I try and persist my entity with a null for dispDT (see below) it generates this exception: "javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: myEntity.dispDt"

By setting a breakpoint in every method in MySQLTimeStampUserType I can see it calls the deepCopy method and never calls the nullSafeSet method. I thought the whole point of the nuyllSafeSet method was to allow me to manipulate the value before persisting it. What am I doing wrong?

Code:
   
    @Basic(optional = false)
    @Column(name = "disp_dt")
    @Type(type = "mypackage.MySQLTimeStampUserType")
//    @Temporal(TemporalType.TIMESTAMP)
    private Date dispDt;


Code:
public class MySQLTimeStampUserType implements UserType {

    private static final int[] SQL_TYPES = {Types.TIMESTAMP};

    public int[] sqlTypes() {
        return SQL_TYPES;
    }

    public Class returnedClass() {
        return Date.class;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == y) {
            return true;
        } else if (x == null || y == null) {
            return false;
        } else {
            return x.equals(y);
        }

    }

    public int hashCode(Object arg0) throws HibernateException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
        // if the date is 0000-00-00 00:00:00 return null, else return the timestamp
        Date result = null;
        if (!resultSet.wasNull()) {
            if (!resultSet.getString(names[0]).equals("0000-00-00 00:00:00")) {
                result = resultSet.getDate(names[0]);
            }
        }
        return result;
    }

    public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException, SQLException {
        // if the date is null set the value to "0000-00-00 00:00:00" else save the timestamp
        if (value == null) {
            statement.setString(index, "0000-00-00 00:00:00");
        } else {
            statement.setTimestamp(index,(Timestamp) value);
        }

    }

    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    public boolean isMutable() {
        return false;
    }

    public Serializable disassemble(Object value) throws HibernateException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }
}


Top
 Profile  
 
 Post subject: Re: not-null property references a null or transient value
PostPosted: Wed Oct 14, 2009 2:20 pm 
Newbie

Joined: Wed Jun 17, 2009 5:27 pm
Posts: 9
answered here

http://stackoverflow.com/questions/1567 ... nt-working


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

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.