2

I use Postgres and I have one column ufsinss character(2)[] like array. I created userType model to use, because I found some posts in google indicating the way to deal with this problem, because Hibernate doesn´t have native ways to deal with it.

My userType have this two code.

public static final String TYPE = "arrayStringType";

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

    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor sessionImpl, Object obj) throws HibernateException, SQLException {

        Array array = rs.getArray(names[0]);
        return NullUtil.isNull(array) ? null : (String[]) array.getArray(); 
   }

    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor sessionImpl) throws HibernateException, SQLException {

        Connection connection = st.getConnection();
        String[] stringArray = (String[]) value;
        Array array = connection.createArrayOf("character", stringArray);
        st.setArray(index, array);      
    }

My model entity is declared like this:

@Type(type = ArrayStringType.TYPE)
private String[] ufsInss;

But I´m getting error:

Mar 25, 2014 10:33:27 PM org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() for servlet [Faces Servlet] in context with path [/ecred2_manat] threw exception
java.lang.reflect.UndeclaredThrowableException
    at com.sun.proxy.$Proxy79.flush(Unknown Source)
    at com.paradigma.ecred.view.filter.OpenSessionInViewFilter.doFilter(OpenSessionInViewFilter.java:38)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    ... [snip] ...
    ... 51 more
Caused by: java.lang.AbstractMethodError: org.postgresql.jdbc3g.Jdbc3gConnection.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array;
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at com.sun.proxy.$Proxy80.createArrayOf(Unknown Source)
    at com.paradigma.ecred.dao.hibernate.type.ArrayStringType.nullSafeSet(ArrayStringType.java:34)
    at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:158)
    ... [snip] ...
    ... 56 more

Edit 1

After change my JDBC version to postgresql-9.3-1101.jdbc41.jar now I got this error when I try to get the execute the Criteria statement from Hibernate.

org.hibernate.exception.SQLGrammarException: Não foi possível encontrar tipo matriz para nome fornecido character.
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:146)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at com.sun.proxy.$Proxy80.createArrayOf(Unknown Source)
    at com.paradigma.ecred.dao.hibernate.type.ArrayStringType.nullSafeSet(ArrayStringType.java:34)
    at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:158)
 ... [snip] 
Caused by: org.postgresql.util.PSQLException: Não foi possível encontrar tipo matriz para nome fornecido character.
    at org.postgresql.jdbc4.AbstractJdbc4Connection.createArrayOf(AbstractJdbc4Connection.java:83)
    at org.postgresql.jdbc4.Jdbc4Connection.createArrayOf(Jdbc4Connection.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138)
    ... 100 more

Edit 3

After updating my code

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor sessionImpl) throws HibernateException, SQLException {

    Connection connection = st.getConnection();
    String[] stringArray = (String[]) value;

    if (NullUtil.isNull(stringArray)) {
        st.setNull(index, IntegerType.INSTANCE.sqlType());          
    } else {            
        Array array = connection.createArrayOf("varchar", stringArray);
        st.setArray(index, array);      
    }

}

I got new error

Mar 26, 2014 6:46:44 AM com.sun.faces.context.ExceptionHandlerImpl throwIt
Informações: Exception when handling error trying to reset the response.
org.hibernate.exception.GenericJDBCException: ERRO: transação atual foi interrompida, comandos ignorados até o fim do bloco de transação
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at com.sun.proxy.$Proxy81.executeUpdate(Unknown Source)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3185)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3087)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3416)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:140)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:276)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
    at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:62)
    at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1185)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1614)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
    at com.paradigma.ecred.dao.impl.HibernateDAOImpl.findByFilter(HibernateDAOImpl.java:118)
    at com.paradigma.ecred.dao.impl.HibernateDAOImpl.findByFilter(HibernateDAOImpl.java:1)
    at com.paradigma.ecred.service.impl.ServiceImpl.findByFilter(ServiceImpl.java:96)
    at com.paradigma.ecred.service.impl.PropostaServiceImpl.findByFilter(PropostaServiceImpl.java:1111)
    at com.paradigma.ecred.view.LazyPojoDataModel.load(LazyPojoDataModel.java:70)
    at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:737)
    at org.primefaces.component.datatable.DataTableRenderer.preEncode(DataTableRenderer.java:93)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:81)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1896)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.paradigma.ecred.view.filter.NoCacheFilter.doFilter(NoCacheFilter.java:36)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.paradigma.ecred.view.filter.OpenSessionInViewFilter.doFilter(OpenSessionInViewFilter.java:34)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.paradigma.ecred.view.filter.IE8CompatibilityFixFilter.doFilter(IE8CompatibilityFixFilter.java:23)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:64)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

Above had more lines error but SO limits.

5
  • 1
    What JDBC driver version are you using? It doesn't appear to implement createArrayOf. Commented Mar 26, 2014 at 1:58
  • @CraigRinger I´m using postgresql-9.2-1002.jdbc3.jar Commented Mar 26, 2014 at 8:57
  • 1
    Do you need JDBC3 for some specific reason, like a very old Java runtime? Commented Mar 26, 2014 at 8:58
  • @CraigRinger I updated my JDBC version to the latest and got this message error now. In portuguese Não foi possível encontrar tipo matriz para nome fornecido character. Commented Mar 26, 2014 at 9:12
  • Show the new exception please - edit the post and add the new exception text after the old one, along with the new driver version. Then comment here when done. Commented Mar 26, 2014 at 9:16

2 Answers 2

2

You had two problems:

  • You were using the JDBC3 driver; and

  • You were using the invalid type name character in your createArrayOf call. You must use a valid SQL type that matches how your table is defined, e.g. varchar or text.

Try:

Array array = connection.createArrayOf("text", stringArray);

(adjust depending on how you declared your array in the database).

Sign up to request clarification or add additional context in comments.

8 Comments

In my first line of my post I told the type ufsinss character(2)[].
@user3316847 It looks like PgJDBC can't find an array type for character[], which is odd, as it works in SQL directly. Consider using varchar for the table definition - there's really no reason to use character anyway.
@user3316847 The error seems pretty self explanatory. You're trying to insert character[] into an integer field. Your SQL seems plain wrong.
@user3316847 If you have a new problem please post a new question. Also, "Got errors" is completely uninformative to anybody without some kind of psychic powers.
@DiegoMacario tried to edit with the comment You can use in the nullSafeGet this code array.getBaseTypeName(); to retrieve the type of your date, and to change text to bpchar. Edit rejected, but wanted to preserve as a comment here.
|
0

First thing you need to update yout drive, because your error is about missing one class. Later you create a class to convert...

You need to make your methods as I suggested to know the type, in your case.

NullSafeGet will be like this

Array array = rs.getArray(names[0]);        
    return NullUtil.isNull(array) ? null : (String[]) array.getArray();

Using in this method, you can retrive the type of your column, as I told. But you need to insert manually data in the column to reatrive and use this code.

String columnType = array.getBaseTypeName();

So it will return bpchar.

And you will implement the method nullSafeSet.

Connection connection = st.getConnection();
String[] stringArray = (String[]) value;

if (NullUtil.isNull(stringArray)) {
    st.setNull(index, Types.ARRAY);
} else {            
    Array array = connection.createArrayOf("bpchar", stringArray);
    st.setArray(index, array);      
}

You can use this to make with other array types, but remenber to get the type of your column.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.