How to map User Data Type (Composite Type) with Hibernate
我对hibernate世界很陌生。
我已经通过ER CASE TOOL(TOAD)对数据库进行建模,并且定义了几种用户数据类型(复合类型)。例如,假设我在PostgreSQL
中声明了如下类型的Contact。
1 2 3 4 5 | CREATE TYPE Contact AS ("email" Varchar, "phone" Varchar, "mobile" Varchar, "other" Varchar ); |
现在假定我像下面的SQL代码中那样在用户实体上使用它
1 2 3 4 5 6 7 8 9 10 11 12 13 |
我已经配置并使用了Hibernate Tools来对ER模型进行逆向工程,但是我对Contact用户类型的定义有疑问,因为它被映射为Serializable而不是自定义类型,这是我想要的是定义联系人Hibernate UserType并根据Hibernate Tools ....的reverse.xml文件进行更改,但是当我尝试使用UserType的实现时,会显示以下消息:
"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."
您是否获得了有关如何实现此目标的简单示例的链接?直到现在,我还无法映射用户数据类型...我正在考虑从ER模型中删除用户数据类型,并展开组成它的字段。
我正在使用Hibernate 3.5.4
在此先感谢您的支持。
让我知道这是否可以解决问题。这是postgres / hibernate实现的示例http://www.hibernatespatial.org/tutorial.html
或可能对这种情况有用的其他信息是:
假设您已使用实体注释声明了Java对象。创建一个ContactUsetype对象,该对象扩展了Hibernate Usertype对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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); } } |
最后,当您创建用户对象
1 |