`
changcheng
  • 浏览: 2101 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

使用JPA注解,持久化java.util.Map!

 
阅读更多
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!
分享到:
评论

相关推荐

    JPA注解参考_Oracle.chm

    JPA注解参考_Oracle.chm 通过它可以全面的掌握JPA编程

    java程序使用JPA注解详解.doc

    Java 程序使用 JPA 注解详解 Java 持久层 API(Java Persistence API)是一种 Java 应用程序接口,用于访问、管理和持久化数据之间的关系。JPA 使用注解来定义实体类与数据库表之间的映射关系,本文将详细介绍 JPA ...

    jpa-java持久化API.ppt

    详细的jpa-java持久化API.ppt

    spring注解+spring data jpa文档+JPA文档.rar

    Spring框架的核心特性包括依赖注入(DI)和面向切面编程(AOP),并且它还提供了对数据库操作的支持,这主要通过Spring Data JPA和Java Persistence API(JPA)实现。 Spring注解是Spring框架中的一大特色,它极大...

    JPA注解参考文档.txt

    在JPA出现之前,Java EE应用程序通常使用容器管理的实体bean来表示持久化数据。而使用JPA,则可以将任何普通的Java POJO指定为JPA实体,即可以通过JPA持久性提供程序的服务将其非持久字段持久保存到关系数据库(既...

    java程序使用JPA注解详解.zip

    Java持久化API(Java Persistence API,简称JPA)是Java平台上的一个标准,用于管理和持久化应用程序中的对象。它提供了一种将业务对象与数据库表之间的映射关系进行声明的方式,使得开发者无需编写大量的SQL语句,...

    JPA规范注解的javax.persistence包

    Java持久化API(Java Persistence API,简称JPA)是Java平台上的一个标准,用于管理和持久化应用程序中的对象。它提供了一种方式来映射Java对象到数据库中的表,简化了数据库操作,使得开发者可以使用面向对象的方式...

    JPA注解总结大全!!!!

    为了将一个普通的 Java 对象转换为 JPA 的实体类,我们需要使用 `@Entity` 注解。例如: ```java @Entity @Table(name = "users") public class Users implements Serializable { // 具体字段定义... } ``` 这里...

    Hibernate+JPA注解教程.doc

    本教程将详细介绍如何在Java开发环境中利用Hibernate和Java Persistence API(JPA)的注解进行数据持久化操作。首先,我们需要了解开发所需的环境和工具,包括MyEclipse 8.5(内含Hibernate 3.2以上版本)、JDK 5.0...

    精通Hibernate:Java对象持久化详解.zip

    《精通Hibernate:Java对象持久化详解》是一本深入解析Hibernate技术的专著,它主要针对Java开发者,旨在帮助他们理解和掌握Java对象持久化的精髓。Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了...

    EJB_JPA数据库持久层开发详解.doc

    JPA(Java Persistence API)是目前广泛采用的持久化标准,它定义了一套API,使得开发者可以使用面向对象的方式来操作数据库,同时兼容各种持久化提供商,如Hibernate、EclipseLink等。JPA简化了数据库访问,提供了...

    JPA详解视频教程 第18讲 使用jpa映射单个实体对象.avi

    JPA用于整合现有的ORM技术,可以简化现有Java EE和Java SE应用对象持久化的开发工作,实现ORM的统一。JPA详解视频教程 第18讲 使用jpa映射单个实体对象.avi

    java程序使用JPA注解详解

    Java 程序使用 JPA 注解详解 Java 程序使用 JPA 注解可以实现对象关系映射(ORM),使得 Java 应用程序能够与关系数据库进行交互。JPA(Java Persistence API)提供了多种注解来定义实体类与数据库表之间的映射关系...

    JPA持久化简介

    Hibernate、iBATIS、TopLink、Castor JDO、Apache OJB等这么多持久层框架,你还在为学习上面那个框架而苦恼吗?你还为研究下一代是那个而头疼吗? 朋友,学习JPA吧!JPA的出现就是解决您上面的苦恼的。

    jpa--2.持久化操作

    **JPA(Java Persistence API)**是Java平台上用于对象关系映射(ORM)的一个标准API,它提供了一种规范化的框架来管理Java应用程序中的数据库交互。JPA的主要目标是简化数据库访问,使得开发者可以使用面向对象的...

    JPA注解的解释和说明.pdf

    JPA(Java Persistence API)是Java平台标准版的一部分,用于在Java应用程序中实现数据持久化。本文将详细介绍JPA中常用注解的使用方法和技巧。 @Entity:此注解用于声明一个类是实体类,并且该类中的对象会被映射...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    而JPA规范,它是Java EE提供的Java持久化API,为对象与关系数据库之间提供了一种标准的持久化机制。 书中还详细讲解了WebLogic和JBoss两大主流应用服务器的配置和部署,这对于掌握Java EE应用在不同服务器上的部署...

    JPA注解.doc J PA注解.

    JPA(Java Persistence API)是Java平台上的一个标准,用于管理关系数据库中的对象持久化。它提供了一套注解,使开发人员能够轻松地将Java类映射到数据库表,而无需编写大量的SQL代码。以下是对JPA注解的详细解释: ...

Global site tag (gtag.js) - Google Analytics