举报投诉联系我们 手机版 热门标签 鳄鱼CMS
您的位置:鳄鱼CMS > JPA ElementCollection映射关键实体示例

JPA ElementCollection映射关键实体示例

2023-04-04 23:31 Java教程

 JPA ElementCollection映射关键实体示例

JPA教程 - JPA ElementCollection映射关键实体示例


在JPA中,我们可以将通用类型的Map映射到数据库。

以下代码定义了一个通用映射,其键值为Employee,值类型为Integer。

@ElementCollection
@CollectionTable(name="EMP_SENIORITY")
@MapKeyJoinColumn(name="EMP_ID")
@Column(name="SENIORITY")
private Map<Employee, Integer> seniorities;

例子

下面的代码来自Department.java。

package cn..common;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MapKeyJoinColumn;

@Entity
public class Department {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    
    @ElementCollection
    @CollectionTable(name="EMP_SENIORITY")
    @MapKeyJoinColumn(name="EMP_ID")
    @Column(name="SENIORITY")
    private Map<Employee, Integer> seniorities;
    
    public Department() {
        seniorities = new HashMap<Employee, Integer>();
    }
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String deptName) {
        this.name = deptName;
    }
    
    public Map<Employee, Integer> getEmployees() {
        return seniorities;
    }

    public void setEmployeeSeniority(Employee employee, int seniority) {
        seniorities.put(employee, seniority);
    }

    public void removeEmployee(Employee employee) {
        seniorities.remove(employee);
    }

    public String toString() {
        StringBuffer aBuffer = new StringBuffer("Department ");
        aBuffer.append(" id: ");
        aBuffer.append(id);
        aBuffer.append(" name: ");
        aBuffer.append(name);
        aBuffer.append(" employeeCount: ");
        aBuffer.append(seniorities.size());
        return aBuffer.toString();
    }
}

下面的代码来自PersonDaoImpl.java。

package cn..common;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class PersonDaoImpl {
  public void test() {
    Employee e = new Employee();
    e.setName("Tom");
    Department d = new Department();
  
    d.setName("test");
    d.getEmployees().put(e, e.getId());
    
    
    em.persist(e);    
    em.persist(d);
   

  }

  @PersistenceContext
  private EntityManager em;
}
下载 ElementCollection_MapKeyEntity.zip

这里是数据库表转储。

Table Name: DEPARTMENT
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: NAME,
    Column Type: VARCHAR:
    Column Value: test

Table Name: EMPLOYEE
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: NAME,
    Column Type: VARCHAR:
    Column Value: Tom

    Column Name: SALARY,
    Column Type: BIGINT:
    Column Value: 0





Table Name: EMP_SENIORITY
 Row:
    Column Name: DEPARTMENT_ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: SENIORITY,
    Column Type: INTEGER:
    Column Value: 0

    Column Name: EMP_ID,
    Column Type: INTEGER:
    Column Value: 1



阅读全文
以上是鳄鱼CMS为你收集整理的 JPA ElementCollection映射关键实体示例全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 鳄鱼CMS eyucms.com 版权所有 联系我们