[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-3579) Support for PostgreSQL UUID data type

Olivier Van Acker (JIRA) noreply at atlassian.com
Sun May 24 07:03:14 EDT 2009


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-3579?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=33237#action_33237 ] 

Olivier Van Acker commented on HHH-3579:
----------------------------------------

These two classes add support, by extending UserType and  IdentifierGenerator:

package org.yourpackagename;

public class UuidUserType implements UserType, Serializable {

    private UUID uuid;

    public UuidUserType() {
        super();
    }

    public UuidUserType(UUID uuid) {
        this.uuid = uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }

    public UUID getUuid() {
        return uuid;
    }

    private static final String CAST_EXCEPTION_TEXT = " cannot be cast to a java.util.UUID.";

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#sqlTypes()
	 */
    public int[] sqlTypes() {
        return new int[]{Hibernate.BIG_DECIMAL.sqlType()};
    }

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#returnedClass()
	 */
    public Class returnedClass() {
        return UuidUserType.class;
    }

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#equals(java.lang.Object,
	 *      java.lang.Object)
	 */
    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == null && y == null) {
            return true;
        } else if (x == null || y == null) {
            return false;
        }
        if (!UuidUserType.class.isAssignableFrom(x.getClass())) {
            throw new HibernateException(x.getClass().toString() + CAST_EXCEPTION_TEXT);
        } else if (!UuidUserType.class.isAssignableFrom(y.getClass())) {
            throw new HibernateException(y.getClass().toString() + CAST_EXCEPTION_TEXT);
        }

        UUID a = ((UuidUserType) x).getUuid();
        UUID b = ((UuidUserType) y).getUuid();

        return a.equals(b);
    }

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
	 */
    public int hashCode(Object x) throws HibernateException {
        if (!UuidUserType.class.isAssignableFrom(x.getClass())) {
            throw new HibernateException(x.getClass().toString() + CAST_EXCEPTION_TEXT);
        }
        UUID uuid = ((UuidUserType) x).getUuid();
        return uuid.hashCode();
    }

    /*
    * (non-Javadoc)
    *
    * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
    *      java.lang.String[], java.lang.Object)
    */

    public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
        Object value = resultSet.getObject(names[0]);
        if (value == null) {
            return null;
        } else {
            UuidUserType retValue = new UuidUserType();
            PGobject pgObject = (PGobject) value;
            retValue.setUuid(UUID.fromString(pgObject.getValue()));
            return retValue;
        }
    }

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
	 *      java.lang.Object, int)
	 */
    public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
        if (value == null) {
            preparedStatement.setNull(index, Types.NULL);
            return;
        }

        if (!UuidUserType.class.isAssignableFrom(value.getClass())) {
            throw new HibernateException(value.getClass().toString() + CAST_EXCEPTION_TEXT);
        }

        UUID uuidValue = ((UuidUserType) value).getUuid();
        String uuidStringValue = uuidValue.toString();

        preparedStatement.setObject(index, uuidValue, Types.OTHER);

    }

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
	 */
    public Object deepCopy(Object value) throws HibernateException {
        return (UuidUserType) value;
    }

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#isMutable()
	 */
    public boolean isMutable() {
        return false;
    }

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
	 */
    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
	 *      java.lang.Object)
	 */
    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }

    /*
	 * (non-Javadoc)
	 *
	 * @see org.hibernate.usertype.UserType#replace(java.lang.Object,
	 *      java.lang.Object, java.lang.Object)
	 */
    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }

    /**
     * Returns the least significant 64 bits of this UUID's 128 bit value.
     *
     * @return
     */
    @XmlElement
    public long getLeastSignificantBits() {
        return uuid.getLeastSignificantBits();
    }

    /**
     * Returns the most significant 64 bits of this UUID's 128 bit value.
     *
     * @return
     */
    public long getMostSignificantBits() {
        return uuid.getMostSignificantBits();
    }

    public static UuidUserType fromString(String uuidString) {
        return new UuidUserType(UUID.fromString(uuidString));
    }

}
[/sourcecode]

Generator:

package org.yourpackagename;

public class UuidGenerator implements IdentifierGenerator {

    public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
        return new UuidUserType(UUID.randomUUID());
    }
}


And this is what the mapping file looks like:


<hibernate-mapping default-cascade="none" default-access="property" default-lazy="false" auto-import="true">
        <class name="org.yourpackagename.Example" table="example" select-before-update="true">

          <id name="id" type="org.yourpackagename.UuidUserType">
         <column name="Id" sql-type="uuid"/>
         <generator class="org.yourpackagename.UuidGenerator"/>
         </id>

etc...




> Support for PostgreSQL UUID data type
> -------------------------------------
>
>                 Key: HHH-3579
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3579
>             Project: Hibernate Core
>          Issue Type: New Feature
>          Components: core
>    Affects Versions: 3.3.1
>            Reporter: Olivier Van Acker
>
> PostgreSQL has since version 8.3 UUID as data type nativly supported in the database. 
> The only way to get this to work in Hibernate is to add <column name="id" sql-type="uuid"/> to your mappings file (or @columnDefinition via annotations)
> and create your own custom usertype (e.g. public class UUIDUserType implements UserType, Serializable {..} ) and map this to java.util.UUID 
> worth mentioning is that java.util.UUID is only introduced in java 1.5 so there might be a backwards compatibility problem
>  

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list