Hibernate and Postgres – a custom UserType to handle arrays

Here is a custom Hibernate UserType that will work to persist Java array types (string[], int[], boolean[], etc..) in a single column. I have seen Oracle implementations, but here is one that uses Java.sql.array and works for Postgres. If you’d like to know more about how this works, Andrew Phillips has written a great explanation of custom Hibernate UserTypes. Enjoy! _______________________ import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.sql.Array; import org.hibernate.*; import org.hibernate.usertype.UserType; /* * Custom UserType implemented to store java array (ie; String[]) types */ public class CustomArrayType implements UserType { public int[] sqlTypes() { … Continue reading Hibernate and Postgres – a custom UserType to handle arrays

The “Quick Study”

“One who is able to memorize something easily and quickly or is able to understand and deal with something easily and successfully”.. Have you ever been called a “quick study”? I think a single day lesson is adequate to examine a technology of interest. Perhaps it’s my mild programmer A.D.D, but I don’t have patience for the multi-part series, where each lesson spans days, weeks, or months. Examples include the “Summer of Nhibernate“, “Learn step-by-step in 7 days“. Of course, s/w methodologies are not learned in a day, but specific software platforms and applications make a good “quick study”. The goal of each quick study will be to: 1) … Continue reading The “Quick Study”

Resolving Postgres "returning auto generated keys no supported"

I discovered Jean-Pol Landrain’s, “TriggerAssignedIdentityGenerator” to address the issue of the Hibernate ID Generator not supporting an Id created by the database via a BEFORE INSERT trigger. However the TriggerAssignedIdentityGenerator design for Oracle did not work for the older JDBC PostgreSQL driver which doesn’t support the return of auto-generated keys. The probem was resolved by changes to the ‘prepare’ and ‘executeAndExtract’ methods: protected PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException { return session.getBatcher().prepareStatement(insertSQL + ” RETURNING *”);} protected Serializable executeAndExtract(PreparedStatement insert) throws SQLException { insert.execute(); // get the new id back ResultSet rs = insert.getResultSet(); if (rs.next()) { return new … Continue reading Resolving Postgres "returning auto generated keys no supported"