-->
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.  [ 3 posts ] 
Author Message
 Post subject: Reverse eng. for postgres UUID
PostPosted: Fri Apr 02, 2010 9:40 am 
Newbie

Joined: Fri Apr 02, 2010 8:40 am
Posts: 9
Hi all,

does the reverse eng. works for UUID database type on (8.4) postgres database (using jdbc-84.701)? For me it creates a Serializable attribute on my object.

Any help appriciated.

Tried to use hibernate.reveng.xml

<sql-type jdbc-type="uuid" length='20' hibernate-type="UUID" />

but then found out there is no hibernate type of UUID nor a jdbc-type of uuid or is it ? (what should i map it to then ? string ? ) anyway didn't work

also wanted to try DelegatingReverseEngineeringStrategy but couldn't found sources ... any help

Thanks


Top
 Profile  
 
 Post subject: Re: Reverse eng. for postgres UUID
PostPosted: Fri Apr 09, 2010 9:00 am 
Senior
Senior

Joined: Tue Aug 04, 2009 7:45 am
Posts: 124
Hibernate tools source code could be found here
https://anonsvn.jboss.org/repos/hiberna ... Ext/tools/


Top
 Profile  
 
 Post subject: Re: Reverse eng. for postgres UUID
PostPosted: Wed May 05, 2010 9:08 am 
Beginner
Beginner

Joined: Tue Oct 20, 2009 6:28 am
Posts: 20
Hi blindmen,

I had to implement a usertype class and use it with my code generator.

Code:
package com.level2.enterprise.crm.hibernate.datatypes;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

import com.sun.xml.txw2.annotation.XmlElement;



public class UuidUserType implements UserType, Serializable {

    /**
    *
    */
   private static final long serialVersionUID = 1553511429434634194L;
   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(value.toString()));
            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();
       
        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));
    }
}



And then

Code:
<table name="survey_answer">
      <primary-key>
         <generator class="sequence">
            <param name="sequence">survey_answers_id_survey_answer_seq</param>
         </generator>
         <key-column name="id_survey_answer" />
      </primary-key>
      [b]<column name="uuid"
         type="com.level2.enterprise.crm.hibernate.datatypes.UuidUserType" />[/b]


   </table>


This works for me. But if you find a better solution.

blindmen wrote:
Hi all,

does the reverse eng. works for UUID database type on (8.4) postgres database (using jdbc-84.701)? For me it creates a Serializable attribute on my object.

Any help appriciated.

Tried to use hibernate.reveng.xml

<sql-type jdbc-type="uuid" length='20' hibernate-type="UUID" />

but then found out there is no hibernate type of UUID nor a jdbc-type of uuid or is it ? (what should i map it to then ? string ? ) anyway didn't work

also wanted to try DelegatingReverseEngineeringStrategy but couldn't found sources ... any help

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.