Hibernate / JPA / HSQL:如何为用户类型ARRAY创建Dialect映射 [英] Hibernate/JPA/HSQL : How to create a Dialect mapping for User Type ARRAY

查看:146
本文介绍了Hibernate / JPA / HSQL:如何为用户类型ARRAY创建Dialect映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @ org.hibernate.annotations。

使用Postgres创建用户类型,并且可以成功读取和写入。 Type(type =com.xxx.datamodel.ext.FooType)
@Column(name =foo,nullable = false)
private int [] foo

@ org.hibernate.annotations.Type(type =com.xxx.datamodel.ext.BarType)
@Column(name =bar,nullable = false)
private double [] bar

但是,当我尝试使用HSQLDialect(用于单元测试)时,我得到:

 原因:org.hibernate.MappingException:否JDBC类型的Dialect映射:2003 
org.hibernate.dialect.TypeNames.get (TypeNames.java:79)
at org.hibernate.dialect.TypeNames.get(TypeNames.java:104)
at org.hibernate.dialect.Dialect.getTypeName(Dialect.java:314)
在org.hibernate.mapping.Column.getSqlType(Column.java:205)
在org.hibernate.mapping.Table.sqlCreateString(Table.java:420)
在org.hibernate。 cfg.Configuration.gen erateSchemaCreationScript(Configuration.java:895)
at org.hibernate.tool.hbm2ddl.SchemaExport。< init>(SchemaExport.java:105)
at org.hibernate.impl.SessionFactoryImpl。< init> ;(SessionFactoryImpl.java:353)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
... 55 more

2003是java.sql.Types.Array
它看起来像在尝试在测试之前创建模式时失败了,我不知道如何告诉HSQL创建正确的类型/模式。

我发现了另一个有点相关的帖子,提示我扩展了HSQLDialect并注册了一个列类型:

  registerColumnType(Types.ARRAY,
FooType.class.getCanonicalName());

registerColumnType(Types.ARRAY,
BarType.class.getCanonicalName());

这种方法的问题是每个SQL类型只允许一个映射,并且它不在 int [] double [] 之间正确解析。不知道这是否是正确的方法。也许有其他方法可以重写模式创建过程吗?

解决方案

解决方案我解决了这个问题。我此时使用Hibernate session.doWork(...)来获得JDBC连接,并使用JDBC完成: http://docs.oracle.com/javase/tutorial/jdbc/basics/array.html



提示:定义数组类型(当您调用时必须执行的操作)


connection.createArrayOf(TYPENAME,Object [])


)你可以参考这个源代码获取允许类型的名称:
http://grepcode.com/file/repo1.maven .org / maven2 / postgresql / postgresql / 9.0-801.jdbc4 / org / postgresql / jdbc2 / TypeInfoCache.java


(这是一个提示回答:更新R esult在JDBC / PostgreSQL中使用SQL数组类型设置


I have successfully created User Types with Postgres, and can read and write successfully.

@org.hibernate.annotations.Type(type = "com.xxx.datamodel.ext.FooType" )
@Column(name = "foo", nullable = false)
private int[] foo

@org.hibernate.annotations.Type(type = "com.xxx.datamodel.ext.BarType" )
@Column(name = "bar", nullable = false)
private double[] bar

However, when I try to use the HSQLDialect (for unit testing) I get:

Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: 2003
at org.hibernate.dialect.TypeNames.get(TypeNames.java:79)
at org.hibernate.dialect.TypeNames.get(TypeNames.java:104)
at org.hibernate.dialect.Dialect.getTypeName(Dialect.java:314)
at org.hibernate.mapping.Column.getSqlType(Column.java:205)
at org.hibernate.mapping.Table.sqlCreateString(Table.java:420)
at org.hibernate.cfg.Configuration.generateSchemaCreationScript(Configuration.java:895)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:105)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:353)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
... 55 more

2003 is java.sql.Types.Array It looks like it fails while trying to create the schema before the tests, and I'm not sure how to tell HSQL to create the proper type/schema.

I found another somewhat related post that suggested I extend HSQLDialect and register a column type:

registerColumnType(Types.ARRAY, 
                   FooType.class.getCanonicalName());

registerColumnType(Types.ARRAY, 
                   BarType.class.getCanonicalName());

The problem with this approach is that there is only one mapping allowed per SQL Type and it doesn't resolve correctly between int[] and double[]. Not sure if this is even the correct approach. Perhaps there is some other way to override the schema creation process?

解决方案

I solved it by working around it. I used Hibernate session.doWork(...) at this point to get a JDBC connection and did it with JDBC: http://docs.oracle.com/javase/tutorial/jdbc/basics/array.html

Hint: To define the array type (which you must do when you call

connection.createArrayOf(TYPENAME,Object[])

) you can consult this source code for the names of allowed types: http://grepcode.com/file/repo1.maven.org/maven2/postgresql/postgresql/9.0-801.jdbc4/org/postgresql/jdbc2/TypeInfoCache.java

(this is a hint from this answer: Updating ResultSets with SQL Array types in JDBC / PostgreSQL)

这篇关于Hibernate / JPA / HSQL:如何为用户类型ARRAY创建Dialect映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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