当表有UUID字段时,我可以使用Hibernate工具对Postgresql数据库进行反向工程吗?

时间:2012-05-08 21:10:05

标签: hibernate postgresql hibernate-mapping uuid hibernate-tools

我想实现以下工作流程: 我在postgresql 9中设计表,我使用Eclipse Indigo的Hibernate Tools为这些表生成POJO,我希望Hibernate使用注释。 使用Eclipse Indigo,Eclipse市场上的最新Postgresql JDBC驱动程序,Java 1.6和Hibernate工具,这不起作用。

反向工程向导将表的UUID字段视为OTHER,并生成类型为Serializable的字段,而不是UUID。插入POJOS抱怨尝试将bytea数据插入UUID字段。简单地说:如何使用休眠工具对POJOS进行反向工程,以便自动处理UUID?

这是生成的字段:

private Serializable instanceId;

1 个答案:

答案 0 :(得分:2)

自定义用户类型

您需要使用一个自定义的usertype来告诉Hibernate如何将UUID列序列化和反序列化为java对象。这是我的(使用Hibernate 3.6.8.Final):

package your.package.usertype;

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.HibernateException;
import org.hibernate.usertype.UserType;

public class UUIDType implements UserType {
    private final int[] sqlTypesSupported = new int[] { Types.NUMERIC };
    private final String CAST_EXCEPTION_TEXT = " cannot be cast to a java.util.UUID"; 

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

    @SuppressWarnings("rawtypes")
    public Class returnedClass() {
        return UUID.class;
    }

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

    public int hashCode(Object x) throws HibernateException {
        return x == null ? null : x.hashCode();
    }    

    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
        assert(names.length == 1);
        Object value = rs.getObject(names[0]);
        if (value == null) {
            return null;
        }        

        UUID uuid = UUID.fromString( rs.getString( names[0] ) );
        return rs.wasNull() ? null : uuid;
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, Types.NULL);
            return;
        } 

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

        UUID uuid = (UUID) value;
        st.setObject(index, uuid, Types.OTHER);
    }

    public Object deepCopy(Object value) throws HibernateException {
        if (value == null)
            return null;
        UUID uuid = (UUID) value;
        return new UUID( uuid.getMostSignificantBits(), uuid.getLeastSignificantBits() );
    }

    public boolean isMutable() {
        return false;
    }

    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }

    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }

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

Hibernate映射

您的Hibernate映射需要为uuid属性指定用户类型的类名:

<property name="uuid" type="your.package.usertype.UUIDType">
    <column name="uuid" not-null="true" />
</property>

逆向工程

由于您正在使用代码生成,因此需要告知逆向工程过程将这些列映射到您的自定义UUIDType用户类型。您应该可以在hibernate.reveng.xml文件中通过将所有OTHER类型映射到UUIDType(未经测试)来执行此操作:

<type-mapping>
    <sql-type jdbc-type="OTHER" hibernate-type="your.package.usertype.UUIDType" />
</type-mapping>

或指定特定的表/列:

<table name="your_table">
    <column name="uuid" type="your.package.usertype.UUIDType" />
</table>

或者,您可以通过创建自己的ReverseEngineeringStrategy类来更好地控制逆向工程过程,而不是使用hibernate.reveng.xml:

package your.package.reveng;

import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.TableIdentifier;

public class CustomReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {
    public CustomReverseEngineeringStrategy( ReverseEngineeringStrategy delegate ){
        super(delegate);
    }

    public String columnToHibernateTypeName( TableIdentifier table, String columnName, int sqlType, int length, int precision, int scale, boolean nullable, boolean generatedIdentifier ){
        if( table.getName().equals("your_table") && columnName.equals("uuid") ){
            return "your.package.usertype.UUIDType";
        } else {
            return super.columnToHibernateTypeName( table, columnName, sqlType, length, precision, scale, nullable, generatedIdentifier );
        }
    }
}

编辑:从3.6.0.Beta1开始,它看起来像org.hibernate.type.PostgresUUIDType。您可能只能使用此类型,但我认为您仍需要在映射中指定它并直接进行逆向工程以将其用于所需的列。