Using Collections in Entity Fields and Properties
Collection-valued persistent fields and properties must use the supported Java collection
interfaces regardless of whether the entity uses persistent fields or properties. The following
collection interfaces may be used:
■ java.util.Collection
■ java.util.Set
Entities
Chapter 32 • Introduction to the Java Persistence API 585
■ java.util.List
■ java.util.Map
If the entity class uses persistent fields, the type in the preceding method signatures must be one
of these collection types. Generic variants of these collection types may also be used. For
example, if it has a persistent property that contains a set of phone numbers, the Customer
entity would have the following methods:
Set<PhoneNumber> getPhoneNumbers() { ... }
void setPhoneNumbers(Set<PhoneNumber>) { ... }
If a field or property of an entity consists of a collection of basic types or embeddable classes, use
the javax.persistence.ElementCollection annotation on the field or property.
The two attributes of @ElementCollection are targetClass and fetch. The targetClass
attribute specifies the class name of the basic or embeddable class and is optional if the field or
property is defined using Java programming language generics. The optional fetch attribute is
used to specify whether the collection should be retrieved lazily or eagerly, using the
javax.persistence.FetchType constants of either LAZY or EAGER, respectively. By default, the
collection will be fetched lazily.
The following entity, Person, has a persistent field, nicknames, which is a collection of String
classes that will be fetched eagerly. The targetClass element is not required, because it uses
generics to define the field.
@Entity
public class Person {
...
@ElementCollection(fetch=EAGER)
protected Set<String> nickname = new HashSet();
...
}
Collections of entity elements and relationships may be represented by java.util.Map
collections. A Map consists of a key and a value.
When using Map elements or relationships, the following rules apply.
■ The Map key or value may be a basic Java programming language type, an embeddable class,
or an entity.
■ When the Map value is an embeddable class or basic type, use the @ElementCollection
annotation.
■ When the Map value is an entity, use the @OneToMany or @ManyToMany annotation.
■ Use the Map type on only one side of a bidirectional relationship.
If the key type of a Map is a Java programming language basic type, use the annotation
javax.persistence.MapKeyColumn to set the column mapping for the key. By default, the name
Entities
586 The Java EE 6Tutorial • July 2012
attribute of @MapKeyColumn is of the form RELATIONSHIP-FIELD/PROPERTY-NAME_KEY.
For example, if the referencing relationship field name is image, the default name attribute is
IMAGE_KEY.
If the key type of a Map is an entity, use the javax.persistence.MapKeyJoinColumn annotation.
If the multiple columns are needed to set the mapping, use the annotation
javax.persistence.MapKeyJoinColumns to include multiple @MapKeyJoinColumn
annotations. If no @MapKeyJoinColumn is present, the mapping column name is by default set to
RELATIONSHIP-FIELD/PROPERTY-NAME_KEY. For example, if the relationship field name is
employee, the default name attribute is EMPLOYEE_KEY.
If Java programming language generic types are not used in the relationship field or property,
the key class must be explicitly set using the javax.persistence.MapKeyClass annotation.
If the Map key is the primary key or a persistent field or property of the entity that is the Map
value, use the javax.persistence.MapKey annotation. The @MapKeyClass and @MapKey
annotations cannot be used on the same field or property.
If the Map value is a Java programming language basic type or an embeddable class, it will be
mapped as a collection table in the underlying database. If generic types are not used, the
@ElementCollection annotation’s targetClass attribute must be set to the type of the Map
value.
If the Map value is an entity and part of a many-to-many or one-to-many unidirectional
relationship, it will be mapped as a join table in the underlying database. A unidirectional
one-to-many relationship that uses a Map may also be mapped using the @JoinColumn
annotation.
If the entity is part of a one-to-many/many-to-one bidirectional relationship, it will be mapped
in the table of the entity that represents the value of the Map. If generic types are not used, the
targetEntity attribute of the @OneToMany and @ManyToMany annotations must be set to the
type of the Map value.
分享到:
相关推荐
包括上下文(Contexts)、实体(Entities)、复杂类型(Complex Types)、标量属性(Scalar Properties)、标识属性(Identity Properties)、字段(Fields)、引用(References)、集合(Collections)、阴影属性...
Microsoft MVP Troy Magennis introduces state-of-the-art techniques for working with in-memory collections more elegantly and efficiently—and writing code that is exceptionally powerful, robust, and ...
Eliminate unnecessary code by taking advantage of the MVVM pattern in Silverlight and WPF using this book and eBook - less code, fewer bugs Build an enterprise application using Silverlight and WPF,...
Java平台提供了一个全新的集合框架,框架的核心为Collection、List(列表)、Set(集合)和Map(映射)。集合类的框架为集合的实现者提供了大量的接口和抽象类,并对其中的某些机制给予了描述,例如,Iterator(迭代...
In the realm of iOS development, collections play a crucial role in organizing and managing data. The Apple documentation on collections provides a comprehensive overview of various types of ...
8. **实用工具类**:如Arrays、Collections和Guava库中的工具类,它们提供了丰富的静态方法来处理集合。 通过阅读"Java Generics and Collections",开发者不仅可以掌握Java泛型和集合的基本使用,还能深入了解它们...
16.1.3. 处理关联和集合类(Handling associations and collections) 16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling ...
Apache Commons Collections是一个Java库,包含了丰富的集合操作工具和算法,为Java平台的开发提供了大量的实用类和接口。这个"commons-collections-3.2.2-"版本是该库的一个特定发行版,主要用于解决WebLogic服务器...
be linked to an entity in the knowledge base), and entity type information for each of the queries. The data included in this package were originally released by LDC to TAC KBP coordinators and ...
Apache Commons Collections是一个强大的Java集合框架扩展库,它为Java的内置集合类提供了丰富的功能增强。这个"commons-collections4-4.2-bin"是Apache Commons Collections的第4版的第2次更新,是一个二进制分发包...
Java集合类是Java编程语言中用于存储和管理对象的关键组件,它们构成了Java Collections Framework的核心。这个框架提供了一组高效、灵活的数据结构,使得开发者能够轻松地处理数据集合,而无需关心底层实现的复杂性...
1. **HashSet**:Iesi.Collections提供的HashSet类是无序、不重复元素的集合,它的性能优于.NET Framework自带的HashSet,特别适合在ORM框架中使用,能有效避免数据库操作时的冗余。 2. **IdentitySet**:这个集合...
hibernate的知识:持久化类(Persistent Classes)、对象关系数据库映射基础(Basic OR Mapping)、集合类(Collections)映射、关联关系映射、拦截器与事件(Interceptors and events)等。
Apache Commons Collections 4是该库的一个主要版本,它包含了大量对Java集合框架的增强,如迭代器、比较器、堆栈、队列、映射等。这个库提供了许多实用工具类和算法,如列表装饰器、映射装饰器、多值映射、双向映射...
This book is about the usage of data structures and algorithms in computer programming. Designing an efficient algorithm to solve a computer science problem is a skill of Computer programmer. This is ...
通过分析这些文件,我们可以了解到如何在XML映射文件中定义集合映射,以及在Java实体类中如何使用这些映射。同时,通过运行这些示例,可以实际体验到集合映射在操作数据时的便利性。 总的来说,Hibernate的集合映射...
C#的集合类主要分布在几个不同的命名空间中,包括System.Collections、System.Collections.Generic和System.Collections.Specialized。这些命名空间提供了不同类型的集合,满足了各种数据存储和操作的需求。 首先,...