Note: This ORM/SQL annotation is silently ignored by ObjectDB

375 words

javax.persistence.MapKeyClass - JPA annotation

javax.persistence
Annotation MapKeyClass

Target:
Fields (including property get methods)
Specifies the type of the map key for associations of type java.util.Map. The map key can be a basic type, an embeddable class, or an entity. If the map is specified using Java generics, the MapKeyClass annotation and associated type need not be specified; otherwise they must be specified.

The MapKeyClass annotation is used in conjunction with ElementCollection or one of the collection-valued relationship annotations (OneToMany or ManyToMany). The MapKey annotation is not used when MapKeyClass is specified and vice versa.

Example 1:

    @Entity
    public class Item {
       @Id int id;
       ...
       @ElementCollection(targetClass=String.class)
       @MapKeyClass(String.class)
       Map images;  // map from image name to image filename
       ...
    }

Example 2:

    // MapKeyClass and target type of relationship can be defaulted
 
    @Entity
    public class Item {
       @Id int id;
       ...
       @ElementCollection
       Map<String, String> images; 
        ...
     }

Example 3:

     @Entity
     public class Company {
        @Id int id;
        ...
        @OneToMany(targetEntity=com.example.VicePresident.class)
        @MapKeyClass(com.example.Division.class)
        Map organization;
     }

Example 4:

     // MapKeyClass and target type of relationship are defaulted
 
     @Entity
     public class Company {
        @Id int id;
        ...
        @OneToMany
        Map<Division, VicePresident> organization;
     }

Since:
JPA 2.0
See Also:
ElementCollection
OneToMany
ManyToMany
Class value
(Required) The type of the map key.
(Required) The type of the map key.
Since:
JPA 2.0

This documentation page is derived (with some adjustments) from the open source JPA 2 RI (EclipseLink)
and is available under the terms of the Eclipse Public License, v. 1.0 and Eclipse Distribution License, v. 1.0.
Object Relational Mapping (ORM) JPA 2 providers include Hibernate, EclipseLink, TopLink, OpenJPA and DataNucleus.
Object DB is not an ORM JPA implementation but an Object Database for Java with built in JPA 2 support.