gpt4 book ai didi

hibernate - 为 UUID 配置 Grails/Hibernate/Postgres

转载 作者:行者123 更新时间:2023-11-29 11:42:02 40 4
gpt4 key购买 nike

所以我正在尝试制作一个 grails 应用程序,它连接到一个现有的数据库中,其中主键列是一个 UUID:

      Column       |            Type             | Modifiers 
-------------------+-----------------------------+-----------
uuid | uuid | not null

但是,当我这样设置数据源时:

class DataStore {
UUID uuid
...
static mapping = {
...
id generator: 'assigned', name: 'uuid', type: 'pg-uuid'
}

它坚持 uuid 列是一个 varchar(255)。我不确定我必须做什么才能让它识别出 uuid 列是一个 uuid 列,并且我尝试将 UserType 类放在 src/groovy/中,但这并没有解决任何问题。

我也在尝试对 inet 列执行相同的操作,但我在这里一次计算一个步骤。

这里有什么帮助吗?我已经无计可施了。

编辑:我找到了这个 grails using uuid as id and mapping to to binary column ,但是当我现在尝试运行它时它只会抛出这个错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Error evaluating ORM mappings block for domain [cstools.domain.DataStore]:  No such property: UUIDUserType for class: org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateMappingBuilder
... 23 more

UUIDUserType.groovy

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;

public class UUIDUserType implements org.hibernate.usertype.UserType {

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

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

if ( !String.class.isAssignableFrom( cached.getClass() ) ) {
return null;
}

return UUID.fromString( (String) cached );
}

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

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

UUID other = (UUID) value;

return UUID.fromString( other.toString() );
}

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

return value.toString();
}

/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#equals(java.lang.Object,
* java.lang.Object)
*/
public boolean equals( Object x, Object y ) throws HibernateException {

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

UUID a = (UUID) x;
UUID b = (UUID) y;

return a.equals( b );
}

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

UUID a = (UUID) x;

return a.hashCode();
}

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

return false;
}

/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
* java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet( ResultSet rs, String[] names, Object owner ) throws HibernateException, SQLException {

String value = rs.getString( names[0] );
if ( value == null ) {
return null;
}
else {
return UUID.fromString( value );
}
}

/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
* java.lang.Object, int)
*/
public void nullSafeSet( PreparedStatement st, Object value, int index ) throws HibernateException, SQLException {

if ( value == null ) {
st.setNull( index, Types.VARCHAR );
return;
}

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

st.setString( index, value.toString() );
}

/*
* (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 {

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

return UUID.fromString( original.toString() );
}

/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#returnedClass()
*/
@SuppressWarnings( "unchecked" )
public Class returnedClass() {

return UUID.class;
}

/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {

return int[] { Types.CHAR };
}
}

最佳答案

编辑:此解决方案需要 PostgreSQL 8.4 或更高版本、相应的 JDBC 驱动程序 8.4-703 或更高版本以及 Hibernate 3.6(对于内置 pg-uuid 类型,自定义 UserType 应与之前的版本一起使用 hibernate 版本)

刚刚发现您不需要自定义 UserType 即可将 java.util.UUID 映射到 PostgreSQL uuid。从 Hibernate 3.6 开始,您可以使用内置类型 pg-uuid,它是 org.hibernate.type.PostgresUUIDType 的快捷方式(参见 Hibernate 3.6 docs about basic types)。此内置类型应该完全执行自定义 UserType 可能执行的操作。

但我认为您必须使用uuid2 生成器而不是uuid 生成器。查看Hibernate 3.6 docs about generators . uuid 生成器创建 UUID 的字符串表示,而 uuid2 生成器能够生成 java.util.UUIDjava.lang.String 或字节的值长度为 16 的数组 (byte[16])。不过,我不知道如何配置 uuid2 生成器。我不使用这个生成器,只是在我的 Entity 基类的构造函数中分配一个随机 UUID。

因此,我认为您必须将代码更改为以下内容:

class DataStore {
UUID uuid
...
static mapping = {
...
id generator: 'assigned', name: 'uuid2', type: 'pg-uuid'
}

关于hibernate - 为 UUID 配置 Grails/Hibernate/Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8452847/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com