Hibernate的UserType和一个定义的长度 [英] Hibernate UserType and a defined length

查看:122
本文介绍了Hibernate的UserType和一个定义的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class UUIDHibernateType implements UserType 
{
private static final int [] SQL_TYPES = new int [] {Types.CHAR};

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

// ...
}

我遇到的问题是,hibernate会生成一个CHAR类型为CHAR(1)的SQL脚本,但实际上我需要CHAR(36)。如何定义自定义类型的默认长度?



目前,我一直在定义像这样的sql类型:

 < id name =id
type =org.openscada.ae.server.storage.jdbc.internal.UUIDHibernateType>
< column name =IDlength =36sql-type =CHAR(36)not-null =true/>
< generator class =assigned/>
< / id>

在这种情况下这不应该是个问题,但如果需要出现,我该怎么做如果有人有一个更好的主意如何透明地处理UUID和数据库agnostig,我很感激。


I have a hibernate Usertype something like this:

public class UUIDHibernateType implements UserType
{
    private static final int[] SQL_TYPES = new int[] { Types.CHAR };

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

    // ...
}

The problem I have is, that hibernate generates a sql script with the types CHAR(1) which is not correct, I would actually need CHAR(36). How would I define the default length of a custom type?

For the moment I'm stuck with defining the sql-type like this:

<id name="id"
    type="org.openscada.ae.server.storage.jdbc.internal.UUIDHibernateType">
    <column name="ID" length="36" sql-type="CHAR(36)" not-null="true" />
    <generator class="assigned" />
</id>

It shouldn't be a problem in this case, but how would I do it if the need arises?

PS: If someone has a better idea how to handle UUIDs transparently and database agnostig, I'm grateful.

解决方案

You can also map your UUID to a BigInteger (java.sql.Types.BIGINT) in your UserType if you don't care about how your UUID is represented in your database (in this case, it will be represented in base 10).

这篇关于Hibernate的UserType和一个定义的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆