From 222672070cbb99c297dd7d181e5aae141834de94 Mon Sep 17 00:00:00 2001 From: Tor-Einar Skog <tor-einar.skog@nibio.no> Date: Mon, 11 Mar 2019 10:17:59 +0100 Subject: [PATCH] Changing custom PostgreSQL user data types to align with Wildfly 16's Hibernate version --- pom.xml | 4 +- .../vips/logic/util/IntegerArrayUserType.java | 59 ++++++++++++++++++- .../vips/logic/util/StringJsonUserType.java | 50 +++++++++++++++- 3 files changed, 105 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index d0da06ed..d39c4af7 100755 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-spatial</artifactId> - <version>5.0.10.Final</version> + <version>5.3.8.Final</version> <exclusions> <exclusion> <artifactId>postgresql</artifactId> @@ -168,7 +168,7 @@ <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> - <version>5.0.10.Final</version> + <version>5.3.8.Final</version> <scope>provided</scope> </dependency> <dependency> diff --git a/src/main/java/no/nibio/vips/logic/util/IntegerArrayUserType.java b/src/main/java/no/nibio/vips/logic/util/IntegerArrayUserType.java index 7bdb5d98..876cb120 100755 --- a/src/main/java/no/nibio/vips/logic/util/IntegerArrayUserType.java +++ b/src/main/java/no/nibio/vips/logic/util/IntegerArrayUserType.java @@ -27,11 +27,12 @@ import java.sql.SQLException; import java.sql.Types; import org.hibernate.HibernateException; import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.usertype.UserType; /** * Adapted from this: http://stackoverflow.com/questions/21940642/hibernate-postgres-array-type - * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @copyright 2015-2019 <a href="http://www.nibio.no/">NIBIO</a> * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ @@ -99,7 +100,7 @@ public class IntegerArrayUserType implements UserType { * * @throws java.sql.SQLException */ - @Override + //@Override public Object nullSafeGet( ResultSet rs, String[] names, @@ -133,7 +134,7 @@ public class IntegerArrayUserType implements UserType { * * @throws java.sql.SQLException */ - @Override + //@Override public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { if (value == null) { st.setNull(index, Types.OTHER); @@ -216,5 +217,57 @@ public class IntegerArrayUserType implements UserType { return original; } + /** + * Retrieve an instance of the mapped class from a JDBC resultset. Implementors + * should handle possibility of null values. + * + * @param rs a JDBC result set + * @param strings the column names + * @param ssci + * @throws org.hibernate.HibernateException + * + * @throws java.sql.SQLException + */ + @Override + public Object nullSafeGet(ResultSet rs, String[] strings, SharedSessionContractImplementor ssci, Object o) throws HibernateException, SQLException { + if (rs.wasNull()) { + return null; + } + try + { + Integer[] array = (Integer[]) rs.getArray(strings[0]).getArray(); + return array; + } + catch(NullPointerException ex) + { + return new Integer[0]; + } + } + + /** + * Write an instance of the mapped class to a prepared statement. Implementors + * should handle possibility of null values. A multi-column type should be written + * to parameters starting from <tt>index</tt>. + * + * @param ps a JDBC prepared statement + * @param o the object to write + * @param i statement parameter index + * @param ssci + * @throws org.hibernate.HibernateException + * + * @throws java.sql.SQLException + */ + @Override + public void nullSafeSet(PreparedStatement ps, Object o, int i, SharedSessionContractImplementor ssci) throws HibernateException, SQLException { + if (o == null) { + ps.setNull(i, Types.OTHER); + return; + } + + Integer[] castObject = (Integer[]) o; + Array array = ssci.connection().createArrayOf("integer", castObject); // The postgres array data type + ps.setArray(i, array); + } + } diff --git a/src/main/java/no/nibio/vips/logic/util/StringJsonUserType.java b/src/main/java/no/nibio/vips/logic/util/StringJsonUserType.java index 92286a7a..8dffa05b 100755 --- a/src/main/java/no/nibio/vips/logic/util/StringJsonUserType.java +++ b/src/main/java/no/nibio/vips/logic/util/StringJsonUserType.java @@ -26,10 +26,11 @@ import java.sql.SQLException; import java.sql.Types; import org.hibernate.HibernateException; import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.usertype.UserType; /** - * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @copyright 2015-2019 <a href="http://www.nibio.no/">NIBIO</a> * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ @@ -97,7 +98,7 @@ public class StringJsonUserType implements UserType { * * @throws java.sql.SQLException */ - @Override + //@Override public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { if(rs.getString(names[0]) == null){ return null; @@ -118,7 +119,7 @@ public class StringJsonUserType implements UserType { * * @throws java.sql.SQLException */ - @Override + //@Override public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { if (value == null) { st.setNull(index, Types.OTHER); @@ -199,5 +200,48 @@ public class StringJsonUserType implements UserType { return original; } + + /** + * Retrieve an instance of the mapped class from a JDBC resultset. Implementors + * should handle possibility of null values. + * + * @param rs a JDBC result set + * @param strings the column names + * @param ssci + * @throws org.hibernate.HibernateException + * + * @throws java.sql.SQLException + */ + @Override + public Object nullSafeGet(ResultSet rs, String[] strings, SharedSessionContractImplementor ssci, Object o) throws HibernateException, SQLException { + if(rs.getString(strings[0]) == null){ + return null; + } + return rs.getString(strings[0]); + } + + /** + * Write an instance of the mapped class to a prepared statement. Implementors + * should handle possibility of null values. A multi-column type should be written + * to parameters starting from <tt>index</tt>. + * + * @param ps a JDBC prepared statement + * @param o the object to write + * @param i statement parameter index + * @param ssci + * @throws org.hibernate.HibernateException + * + * @throws java.sql.SQLException + */ + @Override + public void nullSafeSet(PreparedStatement ps, Object o, int i, SharedSessionContractImplementor ssci) throws HibernateException, SQLException { + if (o == null) { + ps.setNull(i, Types.OTHER); + return; + } + + ps.setObject(i, o, Types.OTHER); + } + } -- GitLab