JPA Hibernate Call Postgres Function Void Return MappingException:

前端 未结 7 2044
不知归路
不知归路 2020-12-31 11:38

I have a problem where I am getting an: org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111 when trying to call a postgres function using JPA cre

相关标签:
7条回答
  • 2020-12-31 12:23

    I had enough messing around with JPA trying to get it to run a stored procedure.

    I ended up using JDBC with a prepared statement. I did it in 15 minutes after spending several fruitless hours trying to fit a square peg into a round hole. I called the same jndi datasource my persistence unit uses to get a connection, created a prepared statement and closed it when done.

    So if you need to run a stored procedure (or Postgres function) from a (now mostly) JPA app, here is what worked for me:

    @Stateless
    @LocalBean
    public class UserQueryBean implements Serializable {
    
        @Resource(mappedName="jdbc/DatabasePool") 
        private javax.sql.DataSource ds;
    
        ...
    
        public void runRequestCleanup() {
    
            String queryString = "SELECT cleanup_function_that_hibernateJPA_choked_on()";
            Connection conn = null;
            PreparedStatement statement = null;
            try {
                conn = ds.getConnection();
                statement = conn.prepareCall(queryString);
                statement.executeQuery();
            } catch (SQLException ex) {
                Logger.getLogger(UserQueryBean.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
                try {
                    statement.close();
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(UserQueryBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            // bit of logging code here    
        }
        ...
    }
    

    There seems to be a horrible oversight to leave out the simple ability to run a function or stored procedure on the server from JPA; especially one that doesn't return anything except void or the number of rows affected. And if it was deliberate ... no comment.

    Edit: added close connection.

    0 讨论(0)
提交回复
热议问题