我正在尝试实现Hstore-Datatype并在JPA实体中使用它。然而,当我试图持久化一些测试数据时,我得到了几个错误。
为了实现Hstore-Datatype,我使用了以下教程:Storing Sets of Key/value Pairs in a Single Db Column With Hibernate Using PostgreSQL Hstore Type
这是我现在在应用程序中的代码:
一个帮助器,用于将Map转换为符合hstore语法的String,反之亦然:
public class HstoreHelper {
private static final String K_V_SEPARATOR = "=>";
public static String toString(Map<String, String> m) {
if (m.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
int n = m.size();
for (String key : m.keySet()) {
sb.append("\"" + key + "\"" + K_V_SEPARATOR + "\"" + m.get(key) + "\"");
if (n > 1) {
sb.append(", ");
n--;
}
}
return sb.toString();
}
public static Map<String, String> toMap(String s) {
Map<String, String> m = new HashMap<String, String>();
if (s.isEmpty()) {
return m;
}
String[] tokens = s.split(", ");
for (String token : tokens) {
String[] kv = token.split(K_V_SEPARATOR);
String k = kv[0];
k = k.trim().substring(1, k.length() - 1);
String v = kv[1];
v = v.trim().substring(1, v.length() - 1);
m.put(k, v);
}
return m;
}
}
字符串
UserType实现:
public class HstoreUserType implements UserType {
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return cached;
}
public Object deepCopy(Object o) throws HibernateException {
Map m = (Map) o;
return new HashMap(m);
}
public Serializable disassemble(Object o) throws HibernateException {
return (Serializable) o;
}
public boolean equals(Object o1, Object o2) throws HibernateException {
Map m1 = (Map) o1;
Map m2 = (Map) o2;
return m1.equals(m2);
}
public int hashCode(Object o) throws HibernateException {
return o.hashCode();
}
public boolean isMutable() {
return true;
}
public Object nullSafeGet(ResultSet rs, String[] strings, Object o)
throws HibernateException, SQLException {
String col = strings[0];
String val = rs.getString(col);
return HstoreHelper.toMap(val);
}
public void nullSafeSet(PreparedStatement ps, Object obj, int i)
throws HibernateException, SQLException {
String s = HstoreHelper.toString((Map) obj);
ps.setObject(i, s, Types.OTHER);
}
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}
public Class returnedClass() {
return Map.class;
}
public int[] sqlTypes() {
return new int[] { Types.INTEGER };
}
}
型
并在实体bean中使用它:
@Entity
@TypeDef(name = "hstore", typeClass = HstoreUserType.class)
@XmlRootElement
@Table(name = "address")
public class Address {
.
.
.
@Type(type = "hstore")
@Column(columnDefinition = "hstore")
private Map<String, String> hs = new HashMap<String, String>();
.
.
.
}
型
我尝试使用address示例发布应用程序,但出现以下错误:
14:40:47,906 INFO [stdout] (MSC service thread 1-1) Hibernate: insert into address (housenumber, location, street, zipcode, address_id) values (?, ?, ?, ?, ?)
14:40:47,921 WARN [com.arjuna.ats.arjuna] (MSC service thread 1-1) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffff0a49215c:-6aa8b5cf:53b3d0e3:133, org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization@628b66 >: java.lang.AbstractMethodError
at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:155) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
14:40:48,062 INFO [org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl] (MSC service thread 1-1) HHH000010: On release of batch it still contained JDBC statements
14:40:48,093 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC00001: Failed to start service jboss.deployment.unit."ductbased.war".component.TestDataGenerator.START: org.jboss.msc.service.StartException in service jboss.deployment.unit."ductbased.war".component.TestDataGenerator.START: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
14:40:49,265 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"ductbased.war\".component.TestDataGenerator.START" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"ductbased.war\".component.TestDataGenerator.START: Failed to start service"}}}}
型
有人能告诉我为什么会出现错误吗?该教程的作者声称,该代码为他工作,所以我独自一人有点:D这也是第一次,我实现了自己的数据类型.在此先谢谢您!
2条答案
按热度按时间jbose2ul1#
对于那些在2020年登陆这里的人来说,hibernate-types现在支持开箱即用。所有学分归Vlad Mihalcea所有。复制部分代码以保持相同。
https://vladmihalcea.com/map-postgresql-hstore-jpa-entity-property-hibernate/
字符串
在你的数据库里
型
在您的POJO中
型
样本输入
型
7kqas0il2#
下面是一个如何将HStore与Hibernate一起使用的示例。
首先编写一个用户类型:
字符串
其次编写一个SQL函数适配器:
型
并以自定义方言进行注册:
型
因此,您将能够在以下实体中使用它:
型
以及在JPQL查询中,例如:
型
有关更深入的文章以及完整的代码示例,请参阅:https://walczak.it/blog/performant-internationalization-in-postgresql-hibernate-jpa-hstore-column-with-translations