-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate with PostgreSQL Arrays
PostPosted: Fri Jul 14, 2006 9:51 am 
Newbie

Joined: Thu Jul 13, 2006 10:18 am
Posts: 1
Is it possible to use PostgreSQL arrays with Hibernate?

I have this table:

CREATE TABLE test (
test_id INTEGER NOT NULL PRIMARY KEY,
test_arr VARCHAR[]
);

How can I map test_arr column? I tryed to use a single String (and use a formated array as content) but I can read but cannot write.

The databes reports this error:

"column "teste_arr" is of type character varying[] but expression is of type character varying"

And the following stack trace:

AbstractFlushingEventListener:299 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at test.Test.main(Test.java:19)
Caused by: java.sql.BatchUpdateException: Entrada em lote 0 update test set test_arr={'val1','val2'} where test_id=1 foi abortada. Chame getNextException para ver a causa.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2423)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1268)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:345)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2485)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 8 more
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at test.Test.main(Test.java:19)
Caused by: java.sql.BatchUpdateException: Entrada em lote 0 update test set test_arr={'val1','val2'} where test_id=1 foi abortada. Chame getNextException para ver a causa.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2423)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1268)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:345)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2485)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 8 more

Thanks,

Hibernate 3.1.2
PorstgreSQL 8.1.3

--
Ricardo


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 1:10 pm 
Newbie

Joined: Fri Jul 14, 2006 7:32 am
Posts: 9
Some of my columns in a PostgreSQL database are arrays too and I'm using a custom user-defined "type" class to convert them between Java and PostgreSQL type.

Example:

Java class:
Code:
public class Test
{
    private Long test_id;
    private String[] test_arr;
}


Hibernate mapping document:
Code:
<class name="Test" table="test" dynamic-update="true">
  <id name="test_id" column="test_id">
    <generator class="native"/>
  </id>
  <property name="test_arr" column="test_arr" type="StringArrayType"/>
</class>


Java class "StringArrayType" (only important methods shown):
Code:
import org.hibernate.usertype.UserType;
public class StringArrayType implements UserType
{
    public int[] sqlTypes() {
        return new int[] {Types.VARCHAR};
    }

    public Class returnedClass() {
        return String[].class;
    }

    public Object nullSafeGet(ResultSet arg0, String[] arg1, Object arg2)
            throws HibernateException, SQLException
    {
        final String resultRaw = arg0.getString(arg1[0]);
           
        if (arg0.wasNull())
            return null;
        else {
            Pattern pat = Pattern.compile("[{}]");
            Matcher matcher = pat.matcher (resultRaw);
            String intermediate = matcher.replaceAll("");
           
            pat = Pattern.compile(",");
            return (pat.split (intermediate));
        }           
    }

   [...]
}


So, I'm converting the string which is returnd by PostgreSQL and which looks like
Code:
{element1,element1, element3}


by using Java built-in regular expressions string handling into a String array in method nullSafeGet() of a class which implements the Hibernet UserType interface.

There is also a nullSafeSet() method which you must implement if you want to write your Java array back into the database.

I have written similar classes to convert PostgreSQL boolean and floating point arrays. They all seem to work fine.
Look for "custom user type" in the Hibernate documentation for more information.

HTH

- andreas


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.