[Lada-commits] [PATCH 1 of 8] Implemented new hibernate usertype: integer array; registered new type in dialect

Wald Commits scm-commit at wald.intevation.org
Tue May 12 15:20:03 CEST 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1431436527 -7200
# Node ID d067d72e602cdf35f964a39dd196588678ae17ff
# Parent  881da3d5377bdd8cdcf9dae2b178ba8aeb8f9d26
Implemented new hibernate usertype: integer array; registered new type in dialect.

diff -r 881da3d5377b -r d067d72e602c src/main/java/de/intevation/lada/util/data/IntegerArrayType.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/util/data/IntegerArrayType.java	Tue May 12 15:15:27 2015 +0200
@@ -0,0 +1,93 @@
+package de.intevation.lada.util.data;
+
+import java.io.Serializable;
+import java.sql.Array;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.hibernate.HibernateException;
+import org.hibernate.engine.spi.SessionImplementor;
+import org.hibernate.usertype.UserType;
+
+public class IntegerArrayType implements UserType {
+    protected static final int  SQLTYPE = java.sql.Types.ARRAY;
+
+    @Override
+    public int[] sqlTypes() {
+        return new int[] {SQLTYPE};
+    }
+
+    @Override
+    public Class<Integer[]> returnedClass() {
+        return Integer[].class;
+    }
+
+    @Override
+    public boolean equals(Object x, Object y)
+    throws HibernateException {
+        return x == null ? y == null : x.equals(y);
+    }
+
+    @Override
+    public int hashCode(Object x)
+    throws HibernateException {
+        return x == null ? 0 : x.hashCode();
+    }
+
+    @Override
+    public Object nullSafeGet(
+        ResultSet rs,
+        String[] names,
+        SessionImplementor session,
+        Object owner)
+    throws HibernateException, SQLException {
+        Array array = rs.getArray(names[0]);
+        Integer[] javaArray = (Integer[]) array.getArray();
+        return javaArray;
+    }
+
+    @Override
+    public void nullSafeSet(
+        PreparedStatement st,
+        Object value,
+        int index,
+        SessionImplementor session)
+    throws HibernateException, SQLException {
+        Connection connection = st.getConnection();
+        Integer[] castObject = (Integer[]) value;
+        Array array = connection.createArrayOf("integer", castObject);
+        st.setArray(index, array);
+    }
+
+    @Override
+    public Object deepCopy(Object value)
+    throws HibernateException {
+        return value == null ? null : ((Integer[]) value).clone();
+    }
+
+    @Override
+    public boolean isMutable() {
+        return true;
+    }
+
+    @Override
+    public Serializable disassemble(Object value)
+    throws HibernateException {
+        return (Integer[])this.deepCopy(value);
+    }
+
+    @Override
+    public Object assemble(Serializable cached, Object owner)
+    throws HibernateException {
+        return this.deepCopy(cached);
+    }
+
+    @Override
+    public Object replace(Object original, Object target, Object owner)
+    throws HibernateException {
+        return original;
+    }
+
+}
diff -r 881da3d5377b -r d067d72e602c src/main/java/de/intevation/lada/util/data/LadaPostgreSQLDialect.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/util/data/LadaPostgreSQLDialect.java	Tue May 12 15:15:27 2015 +0200
@@ -0,0 +1,13 @@
+package de.intevation.lada.util.data;
+
+import java.sql.Types;
+
+import org.hibernate.dialect.PostgreSQL9Dialect;
+
+public class LadaPostgreSQLDialect extends PostgreSQL9Dialect {
+    public LadaPostgreSQLDialect() {
+        super();
+        this.registerColumnType(Types.ARRAY, "integer[]");
+    }
+
+}


More information about the Lada-commits mailing list