本文介绍了存储地图< String,String>使用JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以使用注释使用JPA2在以下类中持久保存属性地图
I am wondering if it is possible using annotations to persist the attributes map in the following class using JPA2
public class Example { long id; // .... Map<String, String> attributes = new HashMap<String, String>(); // .... }因为我们已有预售现有的生产数据库,理想情况下属性的值可以映射到以下现有表:
As we already have a pre existing production database, so ideally the values of attributes could map to the following existing table:
create table example_attributes { example_id bigint, name varchar(100), value varchar(100));推荐答案
JPA 2.0通过 @ElementCollection 可以与 java.util.Map 集合的支持一起使用的注释。 这样的事情应该有效:
JPA 2.0 supports collections of primitives through the @ElementCollection annotation that you can use in conjunction with the support of java.util.Map collections. Something like this should work:
@Entity public class Example { @Id long id; // .... @ElementCollection @MapKeyColumn(name="name") @Column(name="value") @CollectionTable(name="example_attributes", joinColumns=@JoinColumn(name="example_id")) Map<String, String> attributes = new HashMap<String, String>(); // maps from attribute name to value }参见(在JPA 2.0规范)
- 2.6 - 可嵌入类和基本类型的集合
- 2.7地图集合
- 10.1.11 - ElementCollection注释
- 11.1.29 MapKeyColumn注释
- 2.6 - Collections of Embeddable Classes and Basic Types
- 2.7 Map Collections
- 10.1.11 - ElementCollection Annotation
- 11.1.29 MapKeyColumn Annotation
See also (in the JPA 2.0 specification)
更多推荐
存储地图< String,String>使用JPA
发布评论