using collections in Entity Fields and Properties(映射集合类到数据库)
Collection-valued persistent fields and properties must use the supported Java collectioninterfaces regardless of whether the entity uses persistent fields or properties. The followingcollection interfaces may be used:■ java.util.Collection■ java.util.SetEntitiesChapter 32 ? Introduction to the Java Persistence API 585■ java.util.List■ java.util.MapIf the entity class uses persistent fields, the type in the preceding method signatures must be oneof these collection types. Generic variants of these collection types may also be used. Forexample, if it has a persistent property that contains a set of phone numbers, the Customerentity 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, usethe javax.persistence.ElementCollection annotation on the field or property.The two attributes of @ElementCollection are targetClass and fetch. The targetClassattribute specifies the class name of the basic or embeddable class and is optional if the field orproperty is defined using Java programming language generics. The optional fetch attribute isused to specify whether the collection should be retrieved lazily or eagerly, using thejavax.persistence.FetchType constants of either LAZY or EAGER, respectively. By default, thecollection will be fetched lazily.The following entity, Person, has a persistent field, nicknames, which is a collection of Stringclasses that will be fetched eagerly. The targetClass element is not required, because it usesgenerics to define the field.@Entitypublic class Person {...@ElementCollection(fetch=EAGER)protected Set<String> nickname = new HashSet();...}Collections of entity elements and relationships may be represented by java.util.Mapcollections. 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 @ElementCollectionannotation.■ 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 annotationjavax.persistence.MapKeyColumn to set the column mapping for the key. By default, the nameEntities586 The Java EE 6Tutorial ? July 2012attribute 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 isIMAGE_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 annotationjavax.persistence.MapKeyJoinColumns to include multiple @MapKeyJoinColumnannotations. If no @MapKeyJoinColumn is present, the mapping column name is by default set toRELATIONSHIP-FIELD/PROPERTY-NAME_KEY. For example, if the relationship field name isemployee, 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 Mapvalue, use the javax.persistence.MapKey annotation. The @MapKeyClass and @MapKeyannotations 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 bemapped 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 Mapvalue.If the Map value is an entity and part of a many-to-many or one-to-many unidirectionalrelationship, it will be mapped as a join table in the underlying database. A unidirectionalone-to-many relationship that uses a Map may also be mapped using the @JoinColumnannotation.If the entity is part of a one-to-many/many-to-one bidirectional relationship, it will be mappedin the table of the entity that represents the value of the Map. If generic types are not used, thetargetEntity attribute of the @OneToMany and @ManyToMany annotations must be set to thetype of the Map value.