Hibernate/JPA/HSQL : How to create a Dialect mapping for User Type ARRAY

六眼飞鱼酱① 提交于 2019-12-09 07:39:26
alfonx

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)

I solved this by working around it. HSQLDB does not support Arrays, at all. But Since all I need to do is to serialize and deserialize my Array's for my unit tests I can just convert everything to BLOBs...

To Do this I just modified my UserType objects to return an Array or a Blob depending on whether a static global flag is set, which defaults to using Arrays and uses Blobs when I set up HSQLDB.

Had this same issue for longer than I'd like to admit. Ended up adding this to my test package and test-specific profile:

package com.example.test;

import org.hibernate.dialect.PostgreSQL9Dialect;

import java.sql.Types;

public class PostgreSQLDialectArray extends PostgreSQL9Dialect {

    public PostgreSQLDialectArray() { 
        super();
        registerHibernateType(Types.ARRAY, "array");
        registerColumnType(Types.ARRAY, "integer[]" );
    }
}

spring:
  jpa:
    hibernate:
      ddl-auto: create-drop
    properties:
      hibernate:
        dialect: com.example.test.PostgreSQLDialectArray

Seems to be functioning as expected thus far.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!