__________ HibernateSessionFactory.xml ___________________________
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.PostgresqlDialect</prop> -->
<prop key="hibernate.dialect">com.test.model.PostgresqlDialectArray</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.connection.pool_size">10</prop>
<prop key="hibernate.archive.autodetection">class</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
</props>
</property>
______________ PostgresqlDialectArray.java _______________________
package com.test.model;
import java.sql.Types;
public class PostgresqlDialectArray extends org.hibernate.dialect.Postgresql82Dialect{
public PostgresqlDialectArray() {
super();
System.out.println("Register Hibernate Type ... ");
registerHibernateType(Types.ARRAY, "array");
System.out.println("Register Column Type ... ");
registerColumnType(Types.ARRAY, "integer[]");
}
}
_____________ CustomArrayType.java ______________________________
package com.test.model;
import java.io.Serializable;
import java.sql.Connection;
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.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;
public class CustomArrayType implements UserType
{
protected static final int sqlTYPE = java.sql.Types.ARRAY;
private int[] toPrimitive(Integer[] array){
int[] a = new int[array.length];
for(int i = 0 ; i < array.length ; i++)
a[i] = array[i];
return a;
}
private Integer[] toObject(int[] array){
Integer[] a = new Integer[array.length];
for(int i = 0 ; i < array.length ; i++)
a[i] = array[i];
return a;
}
@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, SessionImplementor session, final Object owner) throws HibernateException, sqlException {
Array array = rs.getArray(names[0]);
Integer[] javaArray = (Integer[]) array.getArray();
return toPrimitive(javaArray);
}
@Override
public void nullSafeSet(final PreparedStatement statement, final Object object, final int i, SessionImplementor session) throws HibernateException, sqlException {
System.out.println("test null safe set...");
Connection connection = statement.getConnection();
int[] castObject = (int[]) object;
Integer[] integers = toObject(castObject);
Array array = connection.createArrayOf("integer", integers);
statement.setArray(i, array);
System.out.println("test null safe set...");
}
@Override
public Object assemble(final Serializable cached, final Object owner) throws HibernateException {
return cached;
}
@Override
public Object deepCopy(final Object o) throws HibernateException {
return o == null ? null : ((int[]) o).clone();
}
@Override
public Serializable disassemble(final Object o) throws HibernateException {
return (Serializable) o;
}
@Override
public boolean equals(final Object x, final Object y) throws HibernateException {
return x == null ? y == null : x.equals(y);
}
@Override
public int hashCode(final Object o) throws HibernateException {
return o == null ? 0 : o.hashCode();
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Object replace(final Object original, final Object target, final Object owner) throws HibernateException {
return original;
}
@Override
public Class<int[]> returnedClass() {
return int[].class;
}
@Override
public int[] sqlTypes() {
return new int[] { sqlTYPE };
}
}
____________ MyClass.java ___________________________
package com.test.model;
import static javax.persistence.GenerationType.IDENTITY;
import java.io.Serializable;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
@Entity
@Table(name = "MyClass")
public class MyClass implements Serializable {
private static final long serialVersionUID = -2520209406925143850L;
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@Type(type = "com.test.model.CustomArrayType")
@Column(name = "arrayDay", nullable = true)
private int[] arrayDay;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int[] getArrayDay() {
return arrayDay;
}
public void setArrayDay(int[] arrayDay) {
this.arrayDay = arrayDay;
}
}
______________ ArrayTypeTests.java ___________________________
package com.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.test.model.MyClass;
import com.test.service.MyClassService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
public class ArrayTypeTests implements ApplicationContextAware {
@Autowired
private MyClassService myClassService;
public static ApplicationContext ctx;
@Test
public void test_() {
System.out.println("Test junit");
}
@Test
public void test_arrayDays() {
System.out.println("\n - Start test_array - ");
MyClass myClass = myClassService.getMyClassById(1L);
if (myClass != null) {
int[] array = myClass.getArrayDay();
System.out.println("\n valor1:" + array[0]);
System.out.println("\n valor2:" + array[1]);
System.out.println("\n\n test_array OK ");
} else {
System.out.println("\n ERROR");
}
System.out.println("\n - End test_array - ");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ctx = applicationContext;
}
}
Hibernate和PostgreSQL中具有UserType的数组-> MappingException
Hibernate和PostgreSQL中具有UserType的数组-> MappingException
推荐问题
分类汇总
- (2)
- .net(5)
- Access(210)
- android(1)
- android-studio(1)
- angular(1)
- bash(1)
- c(1)
- c#(625)
- chrome-devtools(1)
- CSS(782)
- css3动画(1)
- docker(1)
- docker-compose(2)
- dotnet(477)
- echarts5.0(1)
- elasticsearch(2)
- element-ui(1)
- eslint(1)
- eventbus(1)
- ffmpeg(2)
- fiddler(1)
- flask(1)
- flutter(1)
- git(2)
- Go(2093)
- golang(9)
- gradle(1)
- harmonyos(4)
- ios(1)
- Java(7682)
- javascript(1221)
- Jave(256)
- JS(330)
- jwt(1)
- kafka(1)
- linux(1)
- lua(1)
- matlab(1)
- mongodb(192)
- MySQL(2516)
- nestjs(1)
- nginx(1)
- Node(262)
- node.js(3)
- Oracle(458)
- php(1213)
- player(1)
- Postgres(167)
- ppt(1)
- python(11274)
- react.js(6)
- redis(2)
- rollup(1)
- seata(1)
- sequelize(1)
- sniffer(1)
- Solr(23)
- springboot(1)
- SQL(118)
- SQLServer(5624)
- Swift(224)
- sybase(21)
- typescript(5)
- uniapp(1)
- uni-app(1)
- vant-weapp(1)
- visual-studio-code(1)
- vue.js(12)
- vue3(3)
- vuex(1)
- wasm(1)
- webpack(1)
- 笔记本电脑(1)
- 调试技巧(1)
- 公众号(1)
- 机器学习(1)
- 计算机(1)
- 爬虫(1)
- 其他(33505)
- 前端(16)
- 算法(2)
- 小程序(3)
- 虚拟机(1)
- 运维(1)