changeset 653:d067d72e602c

Implemented new hibernate usertype: integer array; registered new type in dialect.
author Raimund Renkert <raimund.renkert@intevation.de>
date Tue, 12 May 2015 15:15:27 +0200
parents 881da3d5377b
children 54992c5f9898
files src/main/java/de/intevation/lada/util/data/IntegerArrayType.java src/main/java/de/intevation/lada/util/data/LadaPostgreSQLDialect.java
diffstat 2 files changed, 106 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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;
+    }
+
+}
--- /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[]");
+    }
+
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)