Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate CompositeUserType for immutable object

Tags:

java

hibernate

I'm creating a CompositeUserType in hibernate to map EAST and NORTH fields to a Coordinate object. At present my Coordinate object is immutable, and I'd like to keep it that way if possible.

I've written my nullSafeGet, pulling the coordinates from the ResultSet and calling the constructor:

@Override
public Object nullSafeGet(ResultSet rs, String[] names,
            SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
    Integer easting = (Integer)Hibernate.INTEGER.nullSafeGet(rs, names[0]);
    Integer northing = (Integer)Hibernate.INTEGER.nullSafeGet(rs, names[1]);
    if(easting==null || northing==null)
        return null;
    return new Coordinate(easting, northing);
}

I don't know what to do with setPropertyValue, which seems to want to set the coords one at a time. It is possible to instantiate an immutable object with CompositeUserType, or I am trying to do the impossible?

(Also trying to work out what to do about Hibernate.INTEGER being deprecated, but one thing at a time...)

like image 301
Pengin Avatar asked Feb 08 '11 08:02

Pengin


1 Answers

setPropertyValue() is never called if isMutable() returns false, so you can throw UnsupportedOperationException from it.

like image 80
axtavt Avatar answered Sep 24 '22 03:09

axtavt