你能帮我使用 Hibernate 映射这个类吗?
public class MyClass{
private Long id;
private String name;
private int[] values;
...
}
我使用的是 PostgreSQL,表中的列类型是整数[]
我的数组应该如何映射?
Hibernate(和 JPA)无法直接映射 PostgreSQL 数组类型。看到这个问题 https://stackoverflow.com/questions/1647583/mapping-a-postgres-array-with-hibernate如果您确实需要按原样保留数据库结构,请了解如何继续。这个线程 https://forum.hibernate.org/viewtopic.php?t=946973有所需自定义类型的示例。
如果您可以更改架构,则可以让 hibernate 创建一个附加表来处理集合 -List<Integer>
。然后,根据您使用的 hibernate 版本:
- 符合 JPA 2.0 - 使用@ElementCollection http://download.oracle.com/javaee/6/api/javax/persistence/ElementCollection.html
- 符合 JPA 1.0 - 使用@CollectionOfElements https://web.archive.org/web/20101126125212/http://docs.jboss.org:80/hibernate/stable/core/api/org/hibernate/annotations/CollectionOfElements.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)