如何映射用户数据类型(复合类型)与Hibernate [英] How to map User Data Type (Composite Type) with Hibernate

查看:141
本文介绍了如何映射用户数据类型(复合类型)与Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Hibernate世界颇为陌生。
我用ER CASE TOOL(TOAD)模拟了我的数据库,并且我定义了几个用户数据类型(Composite Type)。例如,假设我在PostgreSQL中声明了一个类型为Contact的类型

pre $ CREATE TYPE联系人AS
(emailVarchar ,
phoneVarchar,
mobileVarchar,
otherVarchar);

现在假设我在用户实体上使用它,如下面的SQL代码所示:

  CREATE TABLE用户(
idUser Serial NOT NULL,
登录字符变化(20)NOT NULL,
密码字符变化(20)NOT NULL,
name不同的字符(30),
不同的字符(50),
联系人联系人


- 添加用于表用户的键

ALTER TABLE用户ADD CONSTRAINT pkIdUser PRIMARY KEY(idUser)
;

我已经配置并使用Hibernate Tools来做ER模型的逆向工程,但是我遇到了问题联系用户类型的定义,因为它被映射为Serializable而不是一个自定义类型,我想要的是定义一个Contact Hibernate UserType并根据Hibernate工具的reverse.xml文件进行更改.....但是当我尝试使用我的UserType实现时,我得到以下消息:


org.hibernate.cfg.JDBCBinderException: .mypackage.FullContactInfoType在Table:users列上找到:fullcontactinfo跨越多列,只允许单列类型
在表上找到的类型为it.mypackage.FullContactInfoType:users列:fullcontactinfo跨越多列,只允许单列类型。


你有关于如何实现这个的简单例子的一些链接?到目前为止,我还没有能够映射用户数据类型......我正在考虑从ER模型中删除用户数据类型,并将它组成的字段分解。



我正在使用Hibernate 3.5.4



关注并感谢您的支持。 解决方案

让我知道这是否解决了这个问题。这里是postgres / hibernate实现的一个例子 http://www.hibernatespatial.org/tutorial.html



或者可能对这种情况有用的其他信息片段是:

假设您有用实体标注声明你的Java对象。创建一个扩展Hibernate Usertype对象的ContactUsetype对象。

  public Object nullSafeGet(ResultSet resultSet,String [] email,String [] phone,String [] mobile,String [] other ,Object owner)
抛出HibernateException,SQLException {
assert email.length == 1;
断言phone.length == 1;
assert mobile.length == 1;
assert other.length == 1

if(resultSet.wasNull()){
return null;
}
final联系contactVariable = new Contact(resultSet.getObject(email [0])。toString()
,resultSet.getObject(phone [0])。toString(),
resultSet.getObject(mobile [0])。toString(),
resultSet.getObject(other [0])。toString());
return contactVariable;

$ b $ public void nullSafeSet(PreparedStatement statement,Object value,int index)
throws HibernateException,SQLException {
statement.setObject(index,value);


最后,当您创建用户对象时

  Users user = new User(...,new Contact((some @ email.com,123-456-4566,111-11- 1111,aim))


I am quite new to Hibernate world. I have modeled by means of ER CASE TOOL (TOAD) my database and I have defined several User Data Type (Composite Type). For example assume I have a type Contact declared as follow in PostgreSQL

CREATE TYPE Contact AS
( "email" Varchar,
"phone" Varchar,
"mobile" Varchar,
"other" Varchar );

Now assume i use it on Users Entity like in the following SQL code

CREATE TABLE Users(
idUser Serial NOT NULL,
login Character varying(20) NOT NULL,
password Character varying(20) NOT NULL,
name Character varying(30),
surname Character varying(50),
contact Contact
)  

-- Add keys for table Users

ALTER TABLE Users ADD CONSTRAINT pkIdUser PRIMARY KEY (idUser)
;

I have configured and used Hibernate Tools to do a reverse engineering of ER model, but I have problem on the definition of the Contact user type because it is mapped as Serializable and not as a custom type, What I am trying to have is to define a Contact Hibernate UserType and change according the reverse.xml file of the Hibernate Tools.....but when I try to use my implementation of the UserType I obtain this message:

"org.hibernate.cfg.JDBCBinderException: The type it.mypackage.FullContactInfoType found on Table: users column: fullcontactinfo spans multiple columns. Only single column types allowed. The type it.mypackage.FullContactInfoType found on Table: users column: fullcontactinfo spans multiple columns. Only single column types allowed. "

Have you some link on a simple example on how to achieve this?. Until now I was not been to able to map the user data type...I am considering to remove the User Data Type from the ER model and explode the fields of wich it is composed.

I am using Hibernate 3.5.4

Regards and thanks in advance for your support.

解决方案

Let me know if this solves the issue. Here is an example of postgres/hibernate implementation http://www.hibernatespatial.org/tutorial.html

or the other info piece that might be useful for this situation is:

Am assuming that you have declared your Java objects with the entity annotation. Create a ContactUsetype object extending Hibernate Usertype object.

public Object nullSafeGet(ResultSet resultSet, String[] email, String[] phone, String[] mobile, String[] other, Object owner)
throws HibernateException, SQLException {
assert email.length == 1;
assert phone.length == 1;
assert mobile.length==1;
assert other.length==1

if (resultSet.wasNull()) {
return null;
}
final Contact contactVariable = new Contact(resultSet.getObject(email[0]).toString()
                                       ,resultSet.getObject(phone[0]).toString(),
                                        resultSet.getObject(mobile[0]).toString(),
                                        resultSet.getObject(other[0]).toString());
return contactVariable;
}

public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {
statement.setObject(index, value);
}
}

Finally when you create users object

Users user= new User(..., new Contact ("(some@email.com,123-456-4566,111-11-1111,aim)")

这篇关于如何映射用户数据类型(复合类型)与Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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