krackmoe
krackmoe

Reputation: 1763

Call a Postgres function via Hibernate

I wrote a PostgreSQL function with the return type of void:

CREATE OR REPLACE FUNCTION queryinteriorexteriorcount()
  RETURNS void AS .....

The function works as expected when I call it from pgAdmin. However, I doesn't seems to work when called from Hibernate. Instead, I just keep getting the following exception thrown at me:

failed.org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111

I tried to create a custom dialect and registered my function like so:

public class PostgisDialect extends PostgisDialect {

    private static final long serialVersionUID = 3397563913838305367L;

    public PostgresDialect(){
        registerFunction("queryinteriorexteriorcount", new StandardSQLFunction("queryinteriorexteriorcount"));
    }
}

Then i changed the dialect in my hibernate.cfg.xml:

<property name="dialect">at.opendata.hibernate.PostgresDialect</property>

and tried to call the function (again) in the following way:

Query query = session.createSQLQuery("SELECT queryinteriorexteriorcount()");
query.uniqueResult();

Can you please tell me how I could properly call this function? I don't expect any return values, I just want to call it - the function would take care of everything else.

Upvotes: 2

Views: 5214

Answers (1)

bpgergo
bpgergo

Reputation: 16037

If you want to fall back to JDBC, here's how to handle stored procedures and get Connection from Hibernate session.

session.doWork(new Work() {
    @Override
    public void execute(Connection connection) throws SQLException {
        connection.prepareCall("{call queryinteriorexteriorcount()}").executeQuery();
    }
});

Upvotes: 2

Related Questions