MapKey (Java EE 5)

Java EE API


javax.persistence Annotation Type MapKey


@Target(value={METHOD,FIELD})
@Retention(value=RUNTIME)
public @interface MapKey

Implements: Annotation
@Target(value={METHOD, FIELD})
@Retention(value=RUNTIME)

此类用于指定 java.util.Map 类型的关联的映射键。

如果使用主键以外的持久字段或属性作为映射键,则它应该关联一个唯一性约束。

示例 1:


    @Entity
    public class Department {
        ...
        @OneToMany(mappedBy="department")
        @MapKey(name="empId")
        public Map getEmployees() {... }
        ...
    }

    @Entity
    public class Employee {
        ...
        @Id Integer getEmpid() { ... }
        @ManyToOne
        @JoinColumn(name="dept_id")
        public Department getDepartment() { ... }
        ...
    }

示例 2:

    @Entity
        public class Department {
        ...
        @OneToMany(mappedBy="department")
        @MapKey(name="empPK")
        public Map getEmployees() {... }
        ...
    }

    @Entity
        public class Employee {
        @EmbeddedId public EmployeePK getEmpPK() { ... }
        ...
        @ManyToOne
        @JoinColumn(name="dept_id")
        public Department getDepartment() { ... }
        ...
    }

    @Embeddable
    public class EmployeePK {
        String name;
        Date bday;
    }
 
英文文档:

Is used to specify the map key for associations of type Map.

If a persistent field or property other than the primary key is used as a map key then it is expected to have a uniqueness constraint associated with it.

    Example 1:

    @Entity
    public class Department {
        ...
        @OneToMany(mappedBy="department")
        @MapKey(name="empId")
        public Map getEmployees() {... }
        ...
    }

    @Entity
    public class Employee {
        ...
        @Id Integer getEmpid() { ... }
        @ManyToOne
        @JoinColumn(name="dept_id")
        public Department getDepartment() { ... }
        ...
    }

    Example 2:

    @Entity
        public class Department {
        ...
        @OneToMany(mappedBy="department")
        @MapKey(name="empPK")
        public Map getEmployees() {... }
        ...
    }

    @Entity
        public class Employee {
        @EmbeddedId public EmployeePK getEmpPK() { ... }
        ...
        @ManyToOne
        @JoinColumn(name="dept_id")
        public Department getDepartment() { ... }
        ...
    }

    @Embeddable
    public class EmployeePK {
        String name;
        Date bday;
    }
 

Since:
Java Persistence 1.0

Optional Element Summary
 String
 

abstract public String name()
用作映射键的关联实体的持久字段或属性的名称。如果未指定该名称元素,则使用关联实体的主键作为映射键。如果主键是复合主键,并映射成 IdClass,则使用主键类的实例作为键。
英文文档:

name

public abstract String name
The name of the persistent field or property of the associated entity that is used as the map key. If the name element is not specified, the primary key of the associated entity is used as the map key. If the primary key is a composite primary key and is mapped as IdClass, an instance of the primary key class is used as the key.

Default:
""


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!