From 9b07dd67297dc93b8e91e387bf23af20efffc979 Mon Sep 17 00:00:00 2001 From: Guillaume Briand Date: Tue, 3 Dec 2019 17:22:03 +0100 Subject: [PATCH] Add support for mapping java.util.Date ARRAY or List to PostgreSQL DATE[] and TIMESTAMP[] #150 --- .../hibernate/type/array/DateArrayType.java | 52 +++++++++++++++ .../type/array/TimestampArrayType.java | 35 ++++++++++ .../internal/DateArrayTypeDescriptor.java | 19 ++++++ .../internal/ListArrayTypeDescriptor.java | 2 + .../TimestampArrayTypeDescriptor.java | 18 +++++ .../hibernate/type/array/ArrayTypeTest.java | 64 ++++++++++++++---- .../type/array/ListArrayTypeTest.java | 39 ++++++++++- .../hibernate/type/model/BaseEntity.java | 20 +++--- .../hibernate/type/array/DateArrayType.java | 52 +++++++++++++++ .../type/array/TimestampArrayType.java | 35 ++++++++++ .../internal/DateArrayTypeDescriptor.java | 19 ++++++ .../internal/ListArrayTypeDescriptor.java | 2 + .../TimestampArrayTypeDescriptor.java | 18 +++++ .../hibernate/type/array/ArrayTypeTest.java | 65 +++++++++++++++---- .../type/array/ListArrayTypeTest.java | 37 ++++++++++- .../hibernate/type/model/BaseEntity.java | 25 ++++--- .../hibernate/type/array/DateArrayType.java | 52 +++++++++++++++ .../type/array/TimestampArrayType.java | 35 ++++++++++ .../internal/DateArrayTypeDescriptor.java | 19 ++++++ .../internal/ListArrayTypeDescriptor.java | 26 ++++---- .../TimestampArrayTypeDescriptor.java | 18 +++++ .../hibernate/type/array/ArrayTypeTest.java | 60 +++++++++++++---- .../type/array/ListArrayTypeTest.java | 42 +++++++++++- .../hibernate/type/model/BaseEntity.java | 25 ++++--- .../hibernate/type/array/DateArrayType.java | 35 ++++++++++ .../type/array/TimestampArrayType.java | 36 ++++++++++ .../internal/DateArrayTypeDescriptor.java | 18 +++++ .../internal/ListArrayTypeDescriptor.java | 2 + .../TimestampArrayTypeDescriptor.java | 18 +++++ .../hibernate/type/array/ArrayTypeTest.java | 57 +++++++++++++--- .../type/array/ListArrayTypeTest.java | 36 +++++++++- .../hibernate/type/model/BaseEntity.java | 25 ++++--- 32 files changed, 892 insertions(+), 114 deletions(-) create mode 100644 hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java create mode 100644 hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java create mode 100644 hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java create mode 100644 hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java create mode 100644 hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java create mode 100644 hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java create mode 100644 hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java create mode 100644 hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java create mode 100644 hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java create mode 100644 hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java create mode 100644 hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java create mode 100644 hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java create mode 100644 hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java create mode 100644 hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java create mode 100644 hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java create mode 100644 hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java diff --git a/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java new file mode 100644 index 000000000..1acf3786b --- /dev/null +++ b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java @@ -0,0 +1,52 @@ +package com.vladmihalcea.hibernate.type.array; + +import com.vladmihalcea.hibernate.type.AbstractHibernateType; +import com.vladmihalcea.hibernate.type.array.internal.ArraySqlTypeDescriptor; +import com.vladmihalcea.hibernate.type.array.internal.DateArrayTypeDescriptor; +import com.vladmihalcea.hibernate.type.util.Configuration; +import org.hibernate.usertype.DynamicParameterizedType; + +import java.util.Date; +import java.util.Properties; + +/** + * Maps an {@code Date[]} array on a PostgreSQL ARRAY type. + *

+ * For more details about how to use it, check out this article on vladmihalcea.com. + * + * @author Guillaume Briand + */ +public class DateArrayType + extends AbstractHibernateType + implements DynamicParameterizedType { + + public static final DateArrayType INSTANCE = new DateArrayType(); + + public DateArrayType() { + super( + ArraySqlTypeDescriptor.INSTANCE, + new DateArrayTypeDescriptor() + ); + } + + public DateArrayType(Configuration configuration) { + super( + ArraySqlTypeDescriptor.INSTANCE, + new DateArrayTypeDescriptor(), configuration + ); + } + + public String getName() { + return "date-array"; + } + + @Override + protected boolean registerUnderJavaType() { + return true; + } + + @Override + public void setParameterValues(Properties parameters) { + ((DateArrayTypeDescriptor) getJavaTypeDescriptor()).setParameterValues(parameters); + } +} diff --git a/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java new file mode 100644 index 000000000..e5aa7a218 --- /dev/null +++ b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java @@ -0,0 +1,35 @@ +package com.vladmihalcea.hibernate.type.array; + +import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType; +import com.vladmihalcea.hibernate.type.array.internal.TimestampArrayTypeDescriptor; +import com.vladmihalcea.hibernate.type.util.Configuration; + +import java.util.Date; + +/** + * Maps an {@code Date[]} array on a PostgreSQL timestamp[] ARRAY type. + *

+ * For more details about how to use it, check out this article on vladmihalcea.com. + * + * @author Vlad Mihalcea + */ +public class TimestampArrayType extends AbstractArrayType { + + public static final TimestampArrayType INSTANCE = new TimestampArrayType(); + + public TimestampArrayType() { + super( + new TimestampArrayTypeDescriptor() + ); + } + + public TimestampArrayType(Configuration configuration) { + super( + new TimestampArrayTypeDescriptor(), configuration + ); + } + + public String getName() { + return "timestamp-array"; + } +} diff --git a/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java new file mode 100644 index 000000000..c3093b366 --- /dev/null +++ b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java @@ -0,0 +1,19 @@ +package com.vladmihalcea.hibernate.type.array.internal; + +import java.util.Date; + +/** + * @author Guillaume Briand + */ +public class DateArrayTypeDescriptor + extends AbstractArrayTypeDescriptor { + + public DateArrayTypeDescriptor() { + super(Date[].class); + } + + @Override + protected String getSqlArrayType() { + return "date"; + } +} diff --git a/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java index 4a1550fe7..730bbb0bc 100644 --- a/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java +++ b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java @@ -89,6 +89,8 @@ public void setParameterValues(Properties parameters) { sqlArrayType = "text"; } else if (UUID.class.isAssignableFrom(arrayElementClass)) { sqlArrayType = "uuid"; + } else if (Date.class.isAssignableFrom(arrayElementClass)) { + sqlArrayType = "timestamp"; } else { throw new UnsupportedOperationException("The " + arrayElementClass + " is not supported yet!"); } diff --git a/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java new file mode 100644 index 000000000..9ec1c9840 --- /dev/null +++ b/hibernate-types-4/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java @@ -0,0 +1,18 @@ +package com.vladmihalcea.hibernate.type.array.internal; + +import java.util.Date; + +/** + * @author Vlad Mihalcea + */ +public class TimestampArrayTypeDescriptor extends AbstractArrayTypeDescriptor { + + public TimestampArrayTypeDescriptor() { + super(Date[].class); + } + + @Override + protected String getSqlArrayType() { + return "timestamp"; + } +} diff --git a/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java b/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java index 25eae2e03..259c90352 100644 --- a/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java +++ b/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java @@ -15,15 +15,19 @@ import javax.persistence.EntityManager; import javax.persistence.Table; import javax.sql.DataSource; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.fail; - import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.UUID; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /** * @author Vlad Mihalcea */ @@ -93,6 +97,10 @@ public String hibernateDialect() { @Test public void test() { + + final Date date1 = Timestamp.valueOf("1991-12-31 00:00:00"); + final Date date2 = Timestamp.valueOf("1990-01-01 00:00:00"); + doInJPA(new JPATransactionFunction() { @Override @@ -107,7 +115,10 @@ public Void apply(EntityManager entityManager) { event.setSensorNames(new String[]{"Temperature", "Pressure"}); event.setSensorValues(new int[]{12, 756}); event.setSensorLongValues(new long[]{42L, 9223372036854775800L}); - event.setSensorStates(new SensorState[] {SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}); + event.setSensorStates(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}); + event.setDateValues(new Date[]{date1, date2}); + event.setTimestampValues(new Date[]{date1, date2}); + entityManager.persist(event); return null; @@ -124,6 +135,10 @@ public Void apply(EntityManager entityManager) { assertArrayEquals(new int[]{12, 756}, event.getSensorValues()); assertArrayEquals(new long[]{42L, 9223372036854775800L}, event.getSensorLongValues()); assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates()); + assertEquals(date1.getTime(), event.getDateValues()[0].getTime()); + assertEquals(date2.getTime(), event.getDateValues()[1].getTime()); + assertEquals(date1.getTime(), event.getTimestampValues()[0].getTime()); + assertEquals(date2.getTime(), event.getTimestampValues()[1].getTime()); return null; } @@ -147,12 +162,20 @@ public static class Event extends BaseEntity { @Type(type = "int-array") @Column(name = "sensor_values", columnDefinition = "integer[]") private int[] sensorValues; - + @Type(type = "long-array") @Column(name = "sensor_long_values", columnDefinition = "bigint[]") private long[] sensorLongValues; - - @Type( type = "sensor-state-array") + + @Type(type = "date-array") + @Column(name = "date_values", columnDefinition = "date[]") + private Date[] dateValues; + + @Type(type = "timestamp-array") + @Column(name = "timestamp_values", columnDefinition = "timestamp[]") + private Date[] timestampValues; + + @Type(type = "sensor-state-array") @Column(name = "sensor_states", columnDefinition = "sensor_state[]") private SensorState[] sensorStates; @@ -179,7 +202,7 @@ public int[] getSensorValues() { public void setSensorValues(int[] sensorValues) { this.sensorValues = sensorValues; } - + public long[] getSensorLongValues() { return sensorLongValues; } @@ -187,18 +210,33 @@ public long[] getSensorLongValues() { public void setSensorLongValues(long[] sensorLongValues) { this.sensorLongValues = sensorLongValues; } - + public SensorState[] getSensorStates() { - return sensorStates; + return sensorStates; } public void setSensorStates(SensorState[] sensorStates) { - this.sensorStates = sensorStates; + this.sensorStates = sensorStates; + } + + public Date[] getDateValues() { + return dateValues; + } + + public void setDateValues(Date[] dateValues) { + this.dateValues = dateValues; + } + + public Date[] getTimestampValues() { + return timestampValues; + } + + public void setTimestampValues(Date[] timestampValues) { + this.timestampValues = timestampValues; } } public enum SensorState { ONLINE, OFFLINE, UNKNOWN; } - } diff --git a/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java b/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java index 6146c8440..17b08fd03 100644 --- a/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java +++ b/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java @@ -19,7 +19,9 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; import java.util.Arrays; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -94,6 +96,10 @@ public String hibernateDialect() { @Test public void test() { + + final Date date1 = Timestamp.valueOf("1991-12-31 00:00:00"); + final Date date2 = Timestamp.valueOf("1990-01-01 00:00:00"); + doInJPA(new JPATransactionFunction() { @Override @@ -109,6 +115,8 @@ public Void apply(EntityManager entityManager) { event.setSensorValues(Arrays.asList(12, 756)); event.setSensorLongValues(Arrays.asList(42L, 9223372036854775800L)); event.setSensorStates(Arrays.asList(SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN)); + event.setDateValues(Arrays.asList(date1, date2)); + event.setTimestampValues(Arrays.asList(date1, date2)); entityManager.persist(event); @@ -127,6 +135,10 @@ public Void apply(EntityManager entityManager) { assertArrayEquals(new Integer[]{12, 756}, event.getSensorValues().toArray()); assertArrayEquals(new Long[]{42L, 9223372036854775800L}, event.getSensorLongValues().toArray()); assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates().toArray()); + assertEquals(date1.getTime(), event.getDateValues().get(0).getTime()); + assertEquals(date2.getTime(), event.getDateValues().get(1).getTime()); + assertEquals(date1.getTime(), event.getTimestampValues().get(0).getTime()); + assertEquals(date2.getTime(), event.getTimestampValues().get(1).getTime()); return null; } @@ -163,10 +175,11 @@ public Void apply(EntityManager entityManager) { @TypeDef(name = "string-list-array", typeClass = ListArrayType.class), @TypeDef(name = "int-list-array", typeClass = ListArrayType.class), @TypeDef(name = "long-list-array", typeClass = ListArrayType.class), + @TypeDef(name = "date-list-array", typeClass = ListArrayType.class), + @TypeDef(name = "timestamp-list-array", typeClass = ListArrayType.class), @TypeDef(name = "sensor-state-array", typeClass = ListArrayType.class, parameters = { @Parameter(name = ListArrayType.SQL_ARRAY_TYPE, value = "sensor_state")} ) - }) @Table(name = "event") public static class Event extends BaseEntity { @@ -190,6 +203,14 @@ public static class Event extends BaseEntity { @Column(name = "sensor_states", columnDefinition = "sensor_state[]") private List sensorStates; + @Type(type = "date-list-array") + @Column(name = "date_values", columnDefinition = "date[]") + private List dateValues; + + @Type(type = "timestamp-list-array") + @Column(name = "timestamp_values", columnDefinition = "timestamp[]") + private List timestampValues; + public List getSensorIds() { return sensorIds; } @@ -229,6 +250,22 @@ public List getSensorStates() { public void setSensorStates(List sensorStates) { this.sensorStates = sensorStates; } + + public List getDateValues() { + return dateValues; + } + + public void setDateValues(List dateValues) { + this.dateValues = dateValues; + } + + public List getTimestampValues() { + return timestampValues; + } + + public void setTimestampValues(List timestampValues) { + this.timestampValues = timestampValues; + } } public enum SensorState { diff --git a/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java b/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java index 63d7bf0ee..56ab918e4 100644 --- a/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java +++ b/hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java @@ -1,12 +1,8 @@ package com.vladmihalcea.hibernate.type.model; -import com.vladmihalcea.hibernate.type.array.IntArrayType; -import com.vladmihalcea.hibernate.type.array.LongArrayType; -import com.vladmihalcea.hibernate.type.array.StringArrayType; -import com.vladmihalcea.hibernate.type.array.UUIDArrayType; +import com.vladmihalcea.hibernate.type.array.*; import com.vladmihalcea.hibernate.type.json.JsonNodeBinaryType; import com.vladmihalcea.hibernate.type.json.JsonNodeStringType; - import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; @@ -18,12 +14,14 @@ * @author Vlad Mihalcea */ @TypeDefs({ - @TypeDef(name = "uuid-array", typeClass = UUIDArrayType.class), - @TypeDef(name = "string-array", typeClass = StringArrayType.class), - @TypeDef(name = "int-array", typeClass = IntArrayType.class), - @TypeDef(name = "long-array", typeClass = LongArrayType.class), - @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class), - @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class), + @TypeDef(name = "uuid-array", typeClass = UUIDArrayType.class), + @TypeDef(name = "string-array", typeClass = StringArrayType.class), + @TypeDef(name = "int-array", typeClass = IntArrayType.class), + @TypeDef(name = "long-array", typeClass = LongArrayType.class), + @TypeDef(name = "date-array", typeClass = DateArrayType.class), + @TypeDef(name = "timestamp-array", typeClass = TimestampArrayType.class), + @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class), + @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class) }) @MappedSuperclass public class BaseEntity { diff --git a/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java new file mode 100644 index 000000000..1acf3786b --- /dev/null +++ b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java @@ -0,0 +1,52 @@ +package com.vladmihalcea.hibernate.type.array; + +import com.vladmihalcea.hibernate.type.AbstractHibernateType; +import com.vladmihalcea.hibernate.type.array.internal.ArraySqlTypeDescriptor; +import com.vladmihalcea.hibernate.type.array.internal.DateArrayTypeDescriptor; +import com.vladmihalcea.hibernate.type.util.Configuration; +import org.hibernate.usertype.DynamicParameterizedType; + +import java.util.Date; +import java.util.Properties; + +/** + * Maps an {@code Date[]} array on a PostgreSQL ARRAY type. + *

+ * For more details about how to use it, check out this article on vladmihalcea.com. + * + * @author Guillaume Briand + */ +public class DateArrayType + extends AbstractHibernateType + implements DynamicParameterizedType { + + public static final DateArrayType INSTANCE = new DateArrayType(); + + public DateArrayType() { + super( + ArraySqlTypeDescriptor.INSTANCE, + new DateArrayTypeDescriptor() + ); + } + + public DateArrayType(Configuration configuration) { + super( + ArraySqlTypeDescriptor.INSTANCE, + new DateArrayTypeDescriptor(), configuration + ); + } + + public String getName() { + return "date-array"; + } + + @Override + protected boolean registerUnderJavaType() { + return true; + } + + @Override + public void setParameterValues(Properties parameters) { + ((DateArrayTypeDescriptor) getJavaTypeDescriptor()).setParameterValues(parameters); + } +} diff --git a/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java new file mode 100644 index 000000000..e5aa7a218 --- /dev/null +++ b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java @@ -0,0 +1,35 @@ +package com.vladmihalcea.hibernate.type.array; + +import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType; +import com.vladmihalcea.hibernate.type.array.internal.TimestampArrayTypeDescriptor; +import com.vladmihalcea.hibernate.type.util.Configuration; + +import java.util.Date; + +/** + * Maps an {@code Date[]} array on a PostgreSQL timestamp[] ARRAY type. + *

+ * For more details about how to use it, check out this article on vladmihalcea.com. + * + * @author Vlad Mihalcea + */ +public class TimestampArrayType extends AbstractArrayType { + + public static final TimestampArrayType INSTANCE = new TimestampArrayType(); + + public TimestampArrayType() { + super( + new TimestampArrayTypeDescriptor() + ); + } + + public TimestampArrayType(Configuration configuration) { + super( + new TimestampArrayTypeDescriptor(), configuration + ); + } + + public String getName() { + return "timestamp-array"; + } +} diff --git a/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java new file mode 100644 index 000000000..c3093b366 --- /dev/null +++ b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java @@ -0,0 +1,19 @@ +package com.vladmihalcea.hibernate.type.array.internal; + +import java.util.Date; + +/** + * @author Guillaume Briand + */ +public class DateArrayTypeDescriptor + extends AbstractArrayTypeDescriptor { + + public DateArrayTypeDescriptor() { + super(Date[].class); + } + + @Override + protected String getSqlArrayType() { + return "date"; + } +} diff --git a/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java index 779658058..2bb0b9499 100644 --- a/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java +++ b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java @@ -89,6 +89,8 @@ public void setParameterValues(Properties parameters) { sqlArrayType = "text"; } else if (UUID.class.isAssignableFrom(arrayElementClass)) { sqlArrayType = "uuid"; + } else if(Date.class.isAssignableFrom(arrayElementClass)) { + sqlArrayType = "timestamp"; } else { throw new UnsupportedOperationException("The " + arrayElementClass + " is not supported yet!"); } diff --git a/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java new file mode 100644 index 000000000..9ec1c9840 --- /dev/null +++ b/hibernate-types-43/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java @@ -0,0 +1,18 @@ +package com.vladmihalcea.hibernate.type.array.internal; + +import java.util.Date; + +/** + * @author Vlad Mihalcea + */ +public class TimestampArrayTypeDescriptor extends AbstractArrayTypeDescriptor { + + public TimestampArrayTypeDescriptor() { + super(Date[].class); + } + + @Override + protected String getSqlArrayType() { + return "timestamp"; + } +} diff --git a/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java b/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java index d9c29ed8c..7058127b6 100644 --- a/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java +++ b/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java @@ -15,15 +15,19 @@ import javax.persistence.EntityManager; import javax.persistence.Table; import javax.sql.DataSource; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.fail; - import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.UUID; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /** * @author Vlad Mihalcea */ @@ -93,6 +97,10 @@ public String hibernateDialect() { @Test public void test() { + + final Date date1 = Timestamp.valueOf("1991-12-31 00:00:00"); + final Date date2 = Timestamp.valueOf("1990-01-01 00:00:00"); + doInJPA(new JPATransactionFunction() { @Override @@ -107,7 +115,10 @@ public Void apply(EntityManager entityManager) { event.setSensorNames(new String[]{"Temperature", "Pressure"}); event.setSensorValues(new int[]{12, 756}); event.setSensorLongValues(new long[]{42L, 9223372036854775800L}); - event.setSensorStates(new SensorState[] {SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}); + event.setSensorStates(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}); + event.setDateValues(new Date[]{date1, date2}); + event.setTimestampValues(new Date[]{date1, date2}); + entityManager.persist(event); return null; @@ -124,6 +135,10 @@ public Void apply(EntityManager entityManager) { assertArrayEquals(new int[]{12, 756}, event.getSensorValues()); assertArrayEquals(new long[]{42L, 9223372036854775800L}, event.getSensorLongValues()); assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates()); + assertEquals(date1.getTime(), event.getDateValues()[0].getTime()); + assertEquals(date2.getTime(), event.getDateValues()[1].getTime()); + assertEquals(date1.getTime(), event.getTimestampValues()[0].getTime()); + assertEquals(date2.getTime(), event.getTimestampValues()[1].getTime()); return null; } @@ -147,12 +162,20 @@ public static class Event extends BaseEntity { @Type(type = "int-array") @Column(name = "sensor_values", columnDefinition = "integer[]") private int[] sensorValues; - + @Type(type = "long-array") @Column(name = "sensor_long_values", columnDefinition = "bigint[]") private long[] sensorLongValues; - - @Type( type = "sensor-state-array") + + @Type(type = "date-array") + @Column(name = "date_values", columnDefinition = "date[]") + private Date[] dateValues; + + @Type(type = "timestamp-array") + @Column(name = "timestamp_values", columnDefinition = "timestamp[]") + private Date[] timestampValues; + + @Type(type = "sensor-state-array") @Column(name = "sensor_states", columnDefinition = "sensor_state[]") private SensorState[] sensorStates; @@ -179,7 +202,7 @@ public int[] getSensorValues() { public void setSensorValues(int[] sensorValues) { this.sensorValues = sensorValues; } - + public long[] getSensorLongValues() { return sensorLongValues; } @@ -187,19 +210,33 @@ public long[] getSensorLongValues() { public void setSensorLongValues(long[] sensorLongValues) { this.sensorLongValues = sensorLongValues; } - + public SensorState[] getSensorStates() { - return sensorStates; + return sensorStates; } public void setSensorStates(SensorState[] sensorStates) { - this.sensorStates = sensorStates; + this.sensorStates = sensorStates; + } + + public Date[] getDateValues() { + return dateValues; + } + + public void setDateValues(Date[] dateValues) { + this.dateValues = dateValues; + } + + public Date[] getTimestampValues() { + return timestampValues; + } + + public void setTimestampValues(Date[] timestampValues) { + this.timestampValues = timestampValues; } - } public enum SensorState { ONLINE, OFFLINE, UNKNOWN; } - } diff --git a/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java b/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java index d1752a906..cbe673eba 100644 --- a/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java +++ b/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java @@ -19,7 +19,9 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; import java.util.Arrays; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -94,6 +96,10 @@ public String hibernateDialect() { @Test public void test() { + + final Date date1 = Timestamp.valueOf("1991-12-31 00:00:00"); + final Date date2 = Timestamp.valueOf("1990-01-01 00:00:00"); + doInJPA(new JPATransactionFunction() { @Override @@ -109,6 +115,8 @@ public Void apply(EntityManager entityManager) { event.setSensorValues(Arrays.asList(12, 756)); event.setSensorLongValues(Arrays.asList(42L, 9223372036854775800L)); event.setSensorStates(Arrays.asList(SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN)); + event.setDateValues(Arrays.asList(date1, date2)); + event.setTimestampValues(Arrays.asList(date1, date2)); entityManager.persist(event); @@ -127,6 +135,10 @@ public Void apply(EntityManager entityManager) { assertArrayEquals(new Integer[]{12, 756}, event.getSensorValues().toArray()); assertArrayEquals(new Long[]{42L, 9223372036854775800L}, event.getSensorLongValues().toArray()); assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates().toArray()); + assertEquals(date1.getTime(), event.getDateValues().get(0).getTime()); + assertEquals(date2.getTime(), event.getDateValues().get(1).getTime()); + assertEquals(date1.getTime(), event.getTimestampValues().get(0).getTime()); + assertEquals(date2.getTime(), event.getTimestampValues().get(1).getTime()); return null; } @@ -163,7 +175,6 @@ public Void apply(EntityManager entityManager) { @TypeDef(name = "sensor-state-array", typeClass = ListArrayType.class, parameters = { @Parameter(name = ListArrayType.SQL_ARRAY_TYPE, value = "sensor_state")} ) - }) @Table(name = "event") public static class Event extends BaseEntity { @@ -187,6 +198,14 @@ public static class Event extends BaseEntity { @Column(name = "sensor_states", columnDefinition = "sensor_state[]") private List sensorStates; + @Type(type = "list-array") + @Column(name = "date_values", columnDefinition = "date[]") + private List dateValues; + + @Type(type = "list-array") + @Column(name = "timestamp_values", columnDefinition = "timestamp[]") + private List timestampValues; + public List getSensorIds() { return sensorIds; } @@ -226,6 +245,22 @@ public List getSensorStates() { public void setSensorStates(List sensorStates) { this.sensorStates = sensorStates; } + + public List getDateValues() { + return dateValues; + } + + public void setDateValues(List dateValues) { + this.dateValues = dateValues; + } + + public List getTimestampValues() { + return timestampValues; + } + + public void setTimestampValues(List timestampValues) { + this.timestampValues = timestampValues; + } } public enum SensorState { diff --git a/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java b/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java index f1e4ba287..ed38c06c3 100644 --- a/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java +++ b/hibernate-types-43/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java @@ -1,9 +1,6 @@ package com.vladmihalcea.hibernate.type.model; -import com.vladmihalcea.hibernate.type.array.IntArrayType; -import com.vladmihalcea.hibernate.type.array.LongArrayType; -import com.vladmihalcea.hibernate.type.array.StringArrayType; -import com.vladmihalcea.hibernate.type.array.UUIDArrayType; +import com.vladmihalcea.hibernate.type.array.*; import com.vladmihalcea.hibernate.type.json.*; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; @@ -16,15 +13,17 @@ * @author Vlad Mihalcea */ @TypeDefs({ - @TypeDef(name = "uuid-array", typeClass = UUIDArrayType.class), - @TypeDef(name = "string-array", typeClass = StringArrayType.class), - @TypeDef(name = "int-array", typeClass = IntArrayType.class), - @TypeDef(name = "long-array", typeClass = LongArrayType.class), - @TypeDef(name = "json", typeClass = JsonStringType.class), - @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class), - @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class), - @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class), - @TypeDef(name = "jsonb-lob", typeClass = JsonBlobType.class), + @TypeDef(name = "uuid-array", typeClass = UUIDArrayType.class), + @TypeDef(name = "string-array", typeClass = StringArrayType.class), + @TypeDef(name = "int-array", typeClass = IntArrayType.class), + @TypeDef(name = "long-array", typeClass = LongArrayType.class), + @TypeDef(name = "date-array", typeClass = DateArrayType.class), + @TypeDef(name = "timestamp-array", typeClass = TimestampArrayType.class), + @TypeDef(name = "json", typeClass = JsonStringType.class), + @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class), + @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class), + @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class), + @TypeDef(name = "jsonb-lob", typeClass = JsonBlobType.class) }) @MappedSuperclass public class BaseEntity { diff --git a/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java new file mode 100644 index 000000000..1acf3786b --- /dev/null +++ b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java @@ -0,0 +1,52 @@ +package com.vladmihalcea.hibernate.type.array; + +import com.vladmihalcea.hibernate.type.AbstractHibernateType; +import com.vladmihalcea.hibernate.type.array.internal.ArraySqlTypeDescriptor; +import com.vladmihalcea.hibernate.type.array.internal.DateArrayTypeDescriptor; +import com.vladmihalcea.hibernate.type.util.Configuration; +import org.hibernate.usertype.DynamicParameterizedType; + +import java.util.Date; +import java.util.Properties; + +/** + * Maps an {@code Date[]} array on a PostgreSQL ARRAY type. + *

+ * For more details about how to use it, check out this article on vladmihalcea.com. + * + * @author Guillaume Briand + */ +public class DateArrayType + extends AbstractHibernateType + implements DynamicParameterizedType { + + public static final DateArrayType INSTANCE = new DateArrayType(); + + public DateArrayType() { + super( + ArraySqlTypeDescriptor.INSTANCE, + new DateArrayTypeDescriptor() + ); + } + + public DateArrayType(Configuration configuration) { + super( + ArraySqlTypeDescriptor.INSTANCE, + new DateArrayTypeDescriptor(), configuration + ); + } + + public String getName() { + return "date-array"; + } + + @Override + protected boolean registerUnderJavaType() { + return true; + } + + @Override + public void setParameterValues(Properties parameters) { + ((DateArrayTypeDescriptor) getJavaTypeDescriptor()).setParameterValues(parameters); + } +} diff --git a/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java new file mode 100644 index 000000000..e5aa7a218 --- /dev/null +++ b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java @@ -0,0 +1,35 @@ +package com.vladmihalcea.hibernate.type.array; + +import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType; +import com.vladmihalcea.hibernate.type.array.internal.TimestampArrayTypeDescriptor; +import com.vladmihalcea.hibernate.type.util.Configuration; + +import java.util.Date; + +/** + * Maps an {@code Date[]} array on a PostgreSQL timestamp[] ARRAY type. + *

+ * For more details about how to use it, check out this article on vladmihalcea.com. + * + * @author Vlad Mihalcea + */ +public class TimestampArrayType extends AbstractArrayType { + + public static final TimestampArrayType INSTANCE = new TimestampArrayType(); + + public TimestampArrayType() { + super( + new TimestampArrayTypeDescriptor() + ); + } + + public TimestampArrayType(Configuration configuration) { + super( + new TimestampArrayTypeDescriptor(), configuration + ); + } + + public String getName() { + return "timestamp-array"; + } +} diff --git a/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java new file mode 100644 index 000000000..c3093b366 --- /dev/null +++ b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java @@ -0,0 +1,19 @@ +package com.vladmihalcea.hibernate.type.array.internal; + +import java.util.Date; + +/** + * @author Guillaume Briand + */ +public class DateArrayTypeDescriptor + extends AbstractArrayTypeDescriptor { + + public DateArrayTypeDescriptor() { + super(Date[].class); + } + + @Override + protected String getSqlArrayType() { + return "date"; + } +} diff --git a/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java index 6b168aba1..8d7c6ae0e 100644 --- a/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java +++ b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java @@ -37,9 +37,9 @@ protected String getSqlArrayType() { @Override public Object unwrap(Object value, Class type, WrapperOptions options) { - if(value instanceof Object[]) { + if (value instanceof Object[]) { return value; - } else if(value instanceof List) { + } else if (value instanceof List) { return super.unwrap(((List) value).toArray(), type, options); } else { throw new UnsupportedOperationException("The provided " + value + " is not a Object[] or List!"); @@ -50,7 +50,7 @@ public Object unwrap(Object value, Class type, WrapperOptions options) { public Object wrap(Object value, WrapperOptions options) { Object wrappedObject = super.wrap(value, options); List list = new ArrayList(); - if(wrappedObject instanceof Object[]) { + if (wrappedObject instanceof Object[]) { Object[] wrappedArray = (Object[]) wrappedObject; Collections.addAll(list, wrappedArray); } else { @@ -64,31 +64,33 @@ public void setParameterValues(Properties parameters) { Class entityClass = ReflectionUtils.getClass(parameters.getProperty(DynamicParameterizedType.ENTITY)); String property = parameters.getProperty(DynamicParameterizedType.PROPERTY); Type memberGenericType = ReflectionUtils.getMemberGenericTypeOrNull(entityClass, property); - if(memberGenericType instanceof ParameterizedType) { + if (memberGenericType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) memberGenericType; Type arrayElementType = parameterizedType.getActualTypeArguments()[0]; String arrayElementClassName = ReflectionUtils.getFieldValue(arrayElementType, "name"); - if(arrayElementClassName == null) { + if (arrayElementClassName == null) { arrayElementClassName = arrayElementType.toString().replaceAll("class ", ""); } Class arrayElementClass = ReflectionUtils.getClass(arrayElementClassName); setArrayObjectClass( arrayElementClass.isArray() ? - arrayElementClass : - ArrayUtil.toArrayClass(arrayElementClass) + arrayElementClass : + ArrayUtil.toArrayClass(arrayElementClass) ); sqlArrayType = parameters.getProperty(AbstractArrayType.SQL_ARRAY_TYPE); - if(sqlArrayType == null) { - if(Integer.class.isAssignableFrom(arrayElementClass)) { + if (sqlArrayType == null) { + if (Integer.class.isAssignableFrom(arrayElementClass)) { sqlArrayType = "integer"; - } else if(Long.class.isAssignableFrom(arrayElementClass)) { + } else if (Long.class.isAssignableFrom(arrayElementClass)) { sqlArrayType = "bigint"; - } else if(String.class.isAssignableFrom(arrayElementClass)) { + } else if (String.class.isAssignableFrom(arrayElementClass)) { sqlArrayType = "text"; - } else if(UUID.class.isAssignableFrom(arrayElementClass)) { + } else if (UUID.class.isAssignableFrom(arrayElementClass)) { sqlArrayType = "uuid"; + } else if (Date.class.isAssignableFrom(arrayElementClass)) { + sqlArrayType = "timestamp"; } else { throw new UnsupportedOperationException("The " + arrayElementClass + " is not supported yet!"); } diff --git a/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java new file mode 100644 index 000000000..9ec1c9840 --- /dev/null +++ b/hibernate-types-5/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java @@ -0,0 +1,18 @@ +package com.vladmihalcea.hibernate.type.array.internal; + +import java.util.Date; + +/** + * @author Vlad Mihalcea + */ +public class TimestampArrayTypeDescriptor extends AbstractArrayTypeDescriptor { + + public TimestampArrayTypeDescriptor() { + super(Date[].class); + } + + @Override + protected String getSqlArrayType() { + return "timestamp"; + } +} diff --git a/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java b/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java index 687bca024..275e6e714 100644 --- a/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java +++ b/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java @@ -15,15 +15,19 @@ import javax.persistence.EntityManager; import javax.persistence.Table; import javax.sql.DataSource; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.fail; - import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.UUID; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /** * @author Vlad Mihalcea */ @@ -93,6 +97,10 @@ public String hibernateDialect() { @Test public void test() { + + final Date date1 = Timestamp.valueOf("1991-12-31 00:00:00"); + final Date date2 = Timestamp.valueOf("1990-01-01 00:00:00"); + doInJPA(new JPATransactionFunction() { @Override @@ -107,7 +115,10 @@ public Void apply(EntityManager entityManager) { event.setSensorNames(new String[]{"Temperature", "Pressure"}); event.setSensorValues(new int[]{12, 756}); event.setSensorLongValues(new long[]{42L, 9223372036854775800L}); - event.setSensorStates(new SensorState[] {SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}); + event.setSensorStates(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}); + event.setDateValues(new Date[]{date1, date2}); + event.setTimestampValues(new Date[]{date1, date2}); + entityManager.persist(event); return null; @@ -124,6 +135,10 @@ public Void apply(EntityManager entityManager) { assertArrayEquals(new int[]{12, 756}, event.getSensorValues()); assertArrayEquals(new long[]{42L, 9223372036854775800L}, event.getSensorLongValues()); assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates()); + assertEquals(date1.getTime(), event.getDateValues()[0].getTime()); + assertEquals(date2.getTime(), event.getDateValues()[1].getTime()); + assertEquals(date1.getTime(), event.getTimestampValues()[0].getTime()); + assertEquals(date2.getTime(), event.getTimestampValues()[1].getTime()); return null; } @@ -151,8 +166,16 @@ public static class Event extends BaseEntity { @Type(type = "long-array") @Column(name = "sensor_long_values", columnDefinition = "bigint[]") private long[] sensorLongValues; - - @Type( type = "sensor-state-array") + + @Type(type = "date-array") + @Column(name = "date_values", columnDefinition = "date[]") + private Date[] dateValues; + + @Type(type = "timestamp-array") + @Column(name = "timestamp_values", columnDefinition = "timestamp[]") + private Date[] timestampValues; + + @Type(type = "sensor-state-array") @Column(name = "sensor_states", columnDefinition = "sensor_state[]") private SensorState[] sensorStates; @@ -187,18 +210,33 @@ public long[] getSensorLongValues() { public void setSensorLongValues(long[] sensorLongValues) { this.sensorLongValues = sensorLongValues; } - + public SensorState[] getSensorStates() { - return sensorStates; + return sensorStates; } public void setSensorStates(SensorState[] sensorStates) { - this.sensorStates = sensorStates; + this.sensorStates = sensorStates; + } + + public Date[] getDateValues() { + return dateValues; + } + + public void setDateValues(Date[] dateValues) { + this.dateValues = dateValues; + } + + public Date[] getTimestampValues() { + return timestampValues; + } + + public void setTimestampValues(Date[] timestampValues) { + this.timestampValues = timestampValues; } } public enum SensorState { ONLINE, OFFLINE, UNKNOWN; } - } diff --git a/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java b/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java index 6258818d4..7d2e11a1d 100644 --- a/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java +++ b/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java @@ -11,12 +11,17 @@ import org.hibernate.annotations.TypeDefs; import org.junit.Test; -import javax.persistence.*; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityManager; +import javax.persistence.Table; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; import java.util.Arrays; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -91,6 +96,10 @@ public String hibernateDialect() { @Test public void test() { + + final Date date1 = Timestamp.valueOf("1991-12-31 00:00:00"); + final Date date2 = Timestamp.valueOf("1990-01-01 00:00:00"); + doInJPA(new JPATransactionFunction() { @Override @@ -106,6 +115,8 @@ public Void apply(EntityManager entityManager) { event.setSensorValues(Arrays.asList(12, 756)); event.setSensorLongValues(Arrays.asList(42L, 9223372036854775800L)); event.setSensorStates(Arrays.asList(SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN)); + event.setDateValues(Arrays.asList(date1, date2)); + event.setTimestampValues(Arrays.asList(date1, date2)); entityManager.persist(event); @@ -124,6 +135,10 @@ public Void apply(EntityManager entityManager) { assertArrayEquals(new Integer[]{12, 756}, event.getSensorValues().toArray()); assertArrayEquals(new Long[]{42L, 9223372036854775800L}, event.getSensorLongValues().toArray()); assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates().toArray()); + assertEquals(date1.getTime(), event.getDateValues().get(0).getTime()); + assertEquals(date2.getTime(), event.getDateValues().get(1).getTime()); + assertEquals(date1.getTime(), event.getTimestampValues().get(0).getTime()); + assertEquals(date2.getTime(), event.getTimestampValues().get(1).getTime()); return null; } @@ -160,7 +175,6 @@ public Void apply(EntityManager entityManager) { @TypeDef(name = "sensor-state-array", typeClass = ListArrayType.class, parameters = { @Parameter(name = ListArrayType.SQL_ARRAY_TYPE, value = "sensor_state")} ) - }) @Table(name = "event") public static class Event extends BaseEntity { @@ -184,6 +198,14 @@ public static class Event extends BaseEntity { @Column(name = "sensor_states", columnDefinition = "sensor_state[]") private List sensorStates; + @Type(type = "list-array") + @Column(name = "date_values", columnDefinition = "date[]") + private List dateValues; + + @Type(type = "list-array") + @Column(name = "timestamp_values", columnDefinition = "timestamp[]") + private List timestampValues; + public List getSensorIds() { return sensorIds; } @@ -223,6 +245,22 @@ public List getSensorStates() { public void setSensorStates(List sensorStates) { this.sensorStates = sensorStates; } + + public List getDateValues() { + return dateValues; + } + + public void setDateValues(List dateValues) { + this.dateValues = dateValues; + } + + public List getTimestampValues() { + return timestampValues; + } + + public void setTimestampValues(List timestampValues) { + this.timestampValues = timestampValues; + } } public enum SensorState { diff --git a/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java b/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java index f1e4ba287..ed38c06c3 100644 --- a/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java +++ b/hibernate-types-5/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java @@ -1,9 +1,6 @@ package com.vladmihalcea.hibernate.type.model; -import com.vladmihalcea.hibernate.type.array.IntArrayType; -import com.vladmihalcea.hibernate.type.array.LongArrayType; -import com.vladmihalcea.hibernate.type.array.StringArrayType; -import com.vladmihalcea.hibernate.type.array.UUIDArrayType; +import com.vladmihalcea.hibernate.type.array.*; import com.vladmihalcea.hibernate.type.json.*; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; @@ -16,15 +13,17 @@ * @author Vlad Mihalcea */ @TypeDefs({ - @TypeDef(name = "uuid-array", typeClass = UUIDArrayType.class), - @TypeDef(name = "string-array", typeClass = StringArrayType.class), - @TypeDef(name = "int-array", typeClass = IntArrayType.class), - @TypeDef(name = "long-array", typeClass = LongArrayType.class), - @TypeDef(name = "json", typeClass = JsonStringType.class), - @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class), - @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class), - @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class), - @TypeDef(name = "jsonb-lob", typeClass = JsonBlobType.class), + @TypeDef(name = "uuid-array", typeClass = UUIDArrayType.class), + @TypeDef(name = "string-array", typeClass = StringArrayType.class), + @TypeDef(name = "int-array", typeClass = IntArrayType.class), + @TypeDef(name = "long-array", typeClass = LongArrayType.class), + @TypeDef(name = "date-array", typeClass = DateArrayType.class), + @TypeDef(name = "timestamp-array", typeClass = TimestampArrayType.class), + @TypeDef(name = "json", typeClass = JsonStringType.class), + @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class), + @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class), + @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class), + @TypeDef(name = "jsonb-lob", typeClass = JsonBlobType.class) }) @MappedSuperclass public class BaseEntity { diff --git a/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java new file mode 100644 index 000000000..43a40eefb --- /dev/null +++ b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/DateArrayType.java @@ -0,0 +1,35 @@ +package com.vladmihalcea.hibernate.type.array; + +import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType; +import com.vladmihalcea.hibernate.type.array.internal.DateArrayTypeDescriptor; +import com.vladmihalcea.hibernate.type.util.Configuration; + +import java.util.Date; + +/** + * Maps an {@code Date[]} array on a PostgreSQL date[] ARRAY type. + *

+ * For more details about how to use it, check out this article on vladmihalcea.com. + * + * @author Guillaume Briand + */ +public class DateArrayType extends AbstractArrayType { + + public static final DateArrayType INSTANCE = new DateArrayType(); + + public DateArrayType() { + super( + new DateArrayTypeDescriptor() + ); + } + + public DateArrayType(Configuration configuration) { + super( + new DateArrayTypeDescriptor(), configuration + ); + } + + public String getName() { + return "date-array"; + } +} diff --git a/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java new file mode 100644 index 000000000..42df554de --- /dev/null +++ b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/TimestampArrayType.java @@ -0,0 +1,36 @@ +package com.vladmihalcea.hibernate.type.array; + +import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType; +import com.vladmihalcea.hibernate.type.array.internal.DateArrayTypeDescriptor; +import com.vladmihalcea.hibernate.type.array.internal.TimestampArrayTypeDescriptor; +import com.vladmihalcea.hibernate.type.util.Configuration; + +import java.util.Date; + +/** + * Maps an {@code Date[]} array on a PostgreSQL timestamp[] ARRAY type. + *

+ * For more details about how to use it, check out this article on vladmihalcea.com. + * + * @author Vlad Mihalcea + */ +public class TimestampArrayType extends AbstractArrayType { + + public static final TimestampArrayType INSTANCE = new TimestampArrayType(); + + public TimestampArrayType() { + super( + new TimestampArrayTypeDescriptor() + ); + } + + public TimestampArrayType(Configuration configuration) { + super( + new TimestampArrayTypeDescriptor(), configuration + ); + } + + public String getName() { + return "timestamp-array"; + } +} diff --git a/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java new file mode 100644 index 000000000..411bf1557 --- /dev/null +++ b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/DateArrayTypeDescriptor.java @@ -0,0 +1,18 @@ +package com.vladmihalcea.hibernate.type.array.internal; + +import java.util.Date; + +/** + * @author Guillaume Briand + */ +public class DateArrayTypeDescriptor extends AbstractArrayTypeDescriptor { + + public DateArrayTypeDescriptor() { + super(Date[].class); + } + + @Override + protected String getSqlArrayType() { + return "date"; + } +} diff --git a/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java index 070e6f1d3..705d5279e 100644 --- a/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java +++ b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/ListArrayTypeDescriptor.java @@ -82,6 +82,8 @@ public void setParameterValues(Properties parameters) { sqlArrayType = "text"; } else if (UUID.class.isAssignableFrom(arrayElementClass)) { sqlArrayType = "uuid"; + } else if (Date.class.isAssignableFrom(arrayElementClass)) { + sqlArrayType = "timestamp"; } else { throw new UnsupportedOperationException("The " + arrayElementClass + " is not supported yet!"); } diff --git a/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java new file mode 100644 index 000000000..9ec1c9840 --- /dev/null +++ b/hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/array/internal/TimestampArrayTypeDescriptor.java @@ -0,0 +1,18 @@ +package com.vladmihalcea.hibernate.type.array.internal; + +import java.util.Date; + +/** + * @author Vlad Mihalcea + */ +public class TimestampArrayTypeDescriptor extends AbstractArrayTypeDescriptor { + + public TimestampArrayTypeDescriptor() { + super(Date[].class); + } + + @Override + protected String getSqlArrayType() { + return "timestamp"; + } +} diff --git a/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java b/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java index eb7a313b3..1fe66bbf1 100644 --- a/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java +++ b/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/array/ArrayTypeTest.java @@ -17,6 +17,9 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.time.LocalDate; +import java.time.ZoneId; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -24,6 +27,7 @@ /** * @author Vlad Mihalcea + * @author Guillaume Briand */ public class ArrayTypeTest extends AbstractPostgreSQLIntegrationTest { @@ -69,6 +73,10 @@ public String hibernateDialect() { @Test public void test() { + + Date date1 = Date.from(LocalDate.of(1991, 12, 31).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); + Date date2 = Date.from(LocalDate.of(1990, 1, 1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); + doInJPA(entityManager -> { Event nullEvent = new Event(); nullEvent.setId(0L); @@ -81,6 +89,9 @@ public void test() { event.setSensorValues(new int[]{12, 756}); event.setSensorLongValues(new long[]{42L, 9223372036854775800L}); event.setSensorStates(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}); + event.setDateValues(new Date[]{date1, date2}); + event.setTimestampValues(new Date[]{date1, date2}); + entityManager.persist(event); }); @@ -90,8 +101,10 @@ public void test() { assertArrayEquals(new UUID[]{UUID.fromString("c65a3bcb-8b36-46d4-bddb-ae96ad016eb1"), UUID.fromString("72e95717-5294-4c15-aa64-a3631cf9a800")}, event.getSensorIds()); assertArrayEquals(new String[]{"Temperature", "Pressure"}, event.getSensorNames()); assertArrayEquals(new int[]{12, 756}, event.getSensorValues()); - assertArrayEquals(new long[]{42L, 9223372036854775800L}, event.getSensorLongValues()); + assertArrayEquals(new long[]{42L, 9223372036854775800L}, event.getSensorLongValues()); assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates()); + assertArrayEquals(new Date[]{date1, date2}, event.getDateValues()); + assertArrayEquals(new Date[]{date1, date2}, event.getTimestampValues()); }); doInJPA(entityManager -> { @@ -100,12 +113,14 @@ public void test() { " id, " + " sensor_ids, " + " sensor_names, " + - " sensor_values " + + " sensor_values, " + + " date_values " + "from event ", Tuple.class) .unwrap(org.hibernate.query.NativeQuery.class) .addScalar("sensor_ids", UUIDArrayType.INSTANCE) .addScalar("sensor_names", StringArrayType.INSTANCE) .addScalar("sensor_values", IntArrayType.INSTANCE) + .addScalar("date_values", DateArrayType.INSTANCE) .getResultList(); assertEquals(2, events.size()); @@ -113,10 +128,10 @@ public void test() { } @Entity(name = "Event") + @Table(name = "event") @TypeDef(name = "sensor-state-array", typeClass = EnumArrayType.class, parameters = { @Parameter(name = EnumArrayType.SQL_ARRAY_TYPE, value = "sensor_state")} ) - @Table(name = "event") public static class Event extends BaseEntity { @Type(type = "uuid-array") @Column(name = "sensor_ids", columnDefinition = "uuid[]") @@ -133,11 +148,27 @@ public static class Event extends BaseEntity { @Type(type = "long-array") @Column(name = "sensor_long_values", columnDefinition = "bigint[]") private long[] sensorLongValues; - + + @Type(type = "date-array") + @Column(name = "date_values", columnDefinition = "date[]") + private Date[] dateValues; + + @Type(type = "timestamp-array") + @Column(name = "timestamp_values", columnDefinition = "timestamp[]") + private Date[] timestampValues; + @Type(type = "sensor-state-array") @Column(name = "sensor_states", columnDefinition = "sensor_state[]") private SensorState[] sensorStates; + public UUID[] getSensorIds() { + return sensorIds; + } + + public void setSensorIds(UUID[] sensorIds) { + this.sensorIds = sensorIds; + } + public String[] getSensorNames() { return sensorNames; } @@ -161,7 +192,7 @@ public long[] getSensorLongValues() { public void setSensorLongValues(long[] sensorLongValues) { this.sensorLongValues = sensorLongValues; } - + public SensorState[] getSensorStates() { return sensorStates; } @@ -170,12 +201,20 @@ public void setSensorStates(SensorState[] sensorStates) { this.sensorStates = sensorStates; } - public UUID[] getSensorIds() { - return sensorIds; + public Date[] getDateValues() { + return dateValues; } - public void setSensorIds(UUID[] sensorIds) { - this.sensorIds = sensorIds; + public void setDateValues(Date[] dateValues) { + this.dateValues = dateValues; + } + + public Date[] getTimestampValues() { + return timestampValues; + } + + public void setTimestampValues(Date[] timestampValues) { + this.timestampValues = timestampValues; } } diff --git a/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java b/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java index e165c9fe3..1e4837ca9 100644 --- a/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java +++ b/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/array/ListArrayTypeTest.java @@ -17,7 +17,10 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.time.LocalDate; +import java.time.ZoneId; import java.util.Arrays; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -70,6 +73,10 @@ public String hibernateDialect() { @Test public void test() { + + Date date1 = Date.from(LocalDate.of(1991, 12, 31).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); + Date date2 = Date.from(LocalDate.of(1990, 1, 1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); + doInJPA(entityManager -> { Event nullEvent = new Event(); nullEvent.setId(0L); @@ -82,7 +89,8 @@ public void test() { event.setSensorValues(Arrays.asList(12, 756)); event.setSensorLongValues(Arrays.asList(42L, 9223372036854775800L)); event.setSensorStates(Arrays.asList(SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN)); - + event.setDateValues(Arrays.asList(date1, date2)); + event.setTimestampValues(Arrays.asList(date1, date2)); entityManager.persist(event); }); @@ -94,6 +102,8 @@ public void test() { assertArrayEquals(new Integer[]{12, 756}, event.getSensorValues().toArray()); assertArrayEquals(new Long[]{42L, 9223372036854775800L}, event.getSensorLongValues().toArray()); assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates().toArray()); + assertArrayEquals(new Date[]{date1, date2}, event.getDateValues().toArray()); + assertArrayEquals(new Date[]{date1, date2}, event.getTimestampValues().toArray()); }); doInJPA(entityManager -> { @@ -141,6 +151,14 @@ public static class Event extends BaseEntity { @Column(name = "sensor_states", columnDefinition = "sensor_state[]") private List sensorStates; + @Type(type = "list-array") + @Column(name = "date_values", columnDefinition = "date[]") + private List dateValues; + + @Type(type = "list-array") + @Column(name = "timestamp_values", columnDefinition = "timestamp[]") + private List timestampValues; + public List getSensorIds() { return sensorIds; } @@ -180,6 +198,22 @@ public List getSensorStates() { public void setSensorStates(List sensorStates) { this.sensorStates = sensorStates; } + + public List getDateValues() { + return dateValues; + } + + public void setDateValues(List dateValues) { + this.dateValues = dateValues; + } + + public List getTimestampValues() { + return timestampValues; + } + + public void setTimestampValues(List timestampValues) { + this.timestampValues = timestampValues; + } } public enum SensorState { diff --git a/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java b/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java index f1e4ba287..431d6268f 100644 --- a/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java +++ b/hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/model/BaseEntity.java @@ -1,9 +1,6 @@ package com.vladmihalcea.hibernate.type.model; -import com.vladmihalcea.hibernate.type.array.IntArrayType; -import com.vladmihalcea.hibernate.type.array.LongArrayType; -import com.vladmihalcea.hibernate.type.array.StringArrayType; -import com.vladmihalcea.hibernate.type.array.UUIDArrayType; +import com.vladmihalcea.hibernate.type.array.*; import com.vladmihalcea.hibernate.type.json.*; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; @@ -16,15 +13,17 @@ * @author Vlad Mihalcea */ @TypeDefs({ - @TypeDef(name = "uuid-array", typeClass = UUIDArrayType.class), - @TypeDef(name = "string-array", typeClass = StringArrayType.class), - @TypeDef(name = "int-array", typeClass = IntArrayType.class), - @TypeDef(name = "long-array", typeClass = LongArrayType.class), - @TypeDef(name = "json", typeClass = JsonStringType.class), - @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class), - @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class), - @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class), - @TypeDef(name = "jsonb-lob", typeClass = JsonBlobType.class), + @TypeDef(name = "uuid-array", typeClass = UUIDArrayType.class), + @TypeDef(name = "string-array", typeClass = StringArrayType.class), + @TypeDef(name = "int-array", typeClass = IntArrayType.class), + @TypeDef(name = "long-array", typeClass = LongArrayType.class), + @TypeDef(name = "date-array", typeClass = DateArrayType.class), + @TypeDef(name = "timestamp-array", typeClass = TimestampArrayType.class), + @TypeDef(name = "json", typeClass = JsonStringType.class), + @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class), + @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class), + @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class), + @TypeDef(name = "jsonb-lob", typeClass = JsonBlobType.class), }) @MappedSuperclass public class BaseEntity {