Using annotations to persist java.util.Maps
转自:http://www.jroller.com/eyallupu/entry/using_annotations_to_persist_java
Mapping any kind of collections is quite simple, @OneToMany on the inverse side, @ManyToOne on the owning side and it works (usually). Maps are little bit more complicated, and less intuitive. Suppose that I want to add to a customer entity a map of properties, each property has a name (the map's key) and a value. Something like:
private Map<String,String> attributes;
Looks simple, isn't that? Yes but currently (as far as I can understand the documentation) there is no support for “true/real maps” the specification only defines support of maps in which the map's key is a property from the map's value. It means that if I want to map the customer's attributes then I have to create a new entity, with three properties
The attribute key (which will be used s the Map's key)
The attribute value
And an Id (since this is an entity)
Then I can map it on the customer class using the @OneToMany and @MapKey annotations.
The Attribute class
package par.common;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Attribute implements Serializable {
/**
* SUID
*/
private static final long serialVersionUID = 3581307841164176872L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
int id;
public String attrKey;
public String value;
@ManyToOne
Customer customer;
public Attribute(String one, String two, Customer customer) {
this.attrKey = one;
this.value = two;
this.customer = customer;
}
//A public default constructor is mandatory!!
public Attribute() {
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getAttrKey() {
return attrKey;
}
public void setAttrKey(String attrKey) {
this.attrKey = attrKey;
}
}
@ManyToOne
The @ManyToOne annotation of the customer field tells the entity manager that many attributes can be linked to one customer (M:1). On a parent-children relation this is the children end of the relation. This is also considered to be the owning side of the relation, in this side the actual relation is stored in the database (the customer property is the owner of the relation). The other side on the relation - the customer's side - is the inverse side, to fetch all of its sons the customer (the inverse side) has to access the attributes' table (the owning side).
And the field and property mappings of the Customer class:
private Map<String, Attribute> attributes = new HashMap<String, Attribute>(10);
@OneToMany(mappedBy="customer",cascade = CascadeType.ALL)
@MapKey(name = "attrKey")
public Map<String, Attribute> getAttributes () {
return attributes;
}
public void setAttributes (Map<String, Attribute> attributes) {
this. attributes = attributes;
}
@OneToMany
The @oneToMany annotation specifies that a customer might have many attributes linked to, the “mappedBy” parameter of the annotation tells the entity manager what is the property/field name that owns the relation. Since this is the inverse side of the relation the ORM has to be notified about the property, on the owning side of the relation, that owns the relation.
@MapKey
This annotation specifies what is the map of the key, it has one parameter – 'name'. The name should be a valid property name from the entity in the map, empty (which implies to the PK of the entity, this is the default). When the entity manager loads a a java.util.Map mapped relation, each entity stored in the map using the MapKey property as its key.
Conclusions
We can use JEE EJB3 specification to persist maps, but we have to notice that the mapping is not as straight forward as we would expect it to be, for example to get the value of an attribute I would have to
String attrValue = customer.getAttributes().get(attrKey).getValue();
But this can be wrapped in a method inside the Customer class.
Posted at 10:48AM Feb 10, 2006 by Eyal Lupu in Persistence | Comments[2]
Comments:
Very useful example. Thanks!
Posted by Stephen M on April 14, 2006 at 11:26 PM GMT+02:00 #
Great article!
Do you know how is the mapping influenced if you have subclasses of the Attribute class, and for example want to store the super class instances with sub classes inside the Map?
Thanks!
分享到:
相关推荐
作. Java 企.版 5 (Java EE 5) ...使用 JPA .,可以使用批注配置.体的 JPA 行.。批注是一种使用元.据修. Java 源代.的..表. 方法,它...相.的 Java .文件,以便在.行.由 JPA 持.性提供程序解.以管理 JPA 行.。
JPA注解参考_Oracle.chm 通过它可以全面的掌握JPA编程
JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解.doc JPA注解....
java程序使用JPA注解详解.zip java程序使用JPA注解详解.zip java程序使用JPA注解详解.zip java程序使用JPA注解详解.zip java程序使用JPA注解详解.zip
java程序使用JPA注解详解.doc
详细的jpa-java持久化API.ppt
Apress.Pro.JPA.2.in.Java.EE.8.An.In-Depth.Guide.to.Java.Persistence.APIs.3rd.Edition.
2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 4、本资源作为“参考资料”如果需要实现其他功能,...
java程序使用JPA注解详解 JPA注解详解 java 程序使用 JPA注解 详解
jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作
JPA规范注解的javax.persistence包 ejb3-persistence.jar
spring注解完整版+spring data jpa官方文档中文翻译+JPA2.0官方文档 文档内容齐全 值得参考学习
drools-persistence-jpa-5.2.0.Final.jar drools
JPA用于整合现有的ORM技术,可以简化现有Java EE和Java SE应用对象持久化的开发工作,实现ORM的统一。JPA详解视频教程 第18讲 使用jpa映射单个实体对象.avi
JPA 教程 Java 持久化教程 JPA 教程 Java 持久化教程
详细的JPA注解总结大全 ,,,,,,,
Hibernate、iBATIS、TopLink、Castor JDO、Apache OJB等这么多持久层框架,你还在为学习上面那个框架而苦恼吗?你还为研究下一代是那个而头疼吗? 朋友,学习JPA吧!JPA的出现就是解决您上面的苦恼的。
第10章 Java持久化API(JPA) 364 10.1 实体简介 365 10.1.1 对象/关系数据库映射(ORM) 365 10.1.2 JPA的映射规则 367 10.1.3 JPA规范简介 368 10.2 实体入门 368 10.2.1 开发实体 369 10.2.2 在Java SE环境下使用 ...
Hibernate+JPA注解教程.docHibernate+JPA注解教程.docHibernate+JPA注解教程.docHibernate+JPA注解教程.doc
精通Hibernate:Java对象持久化详解解压后是pdf文档。