广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州什么是JPA ElementCollection枚举映射?_北大青鸟IT学校

作者:邓华发布时间:2021-05-05分类:Java技术浏览:900


导读:相信大家在学习Java的过程都会遇到各种各样的问题,例如:什么是JPA ElementCollection枚举映射?那么下面我们一起来看看惠州北大青鸟学术部老师是怎么讲解这道题的。

相信大家在学习Java的过程都会遇到各种各样的问题,例如:什么是JPA ElementCollection枚举映射?那么下面我们一起来看看惠州北大青鸟学术部老师是怎么讲解这道题的。

以下代码显示如何将带有枚举键值的Java java.util.Map保存到数据库。

PhoneType 是枚举类型。

   @ElementCollection
   @CollectionTable(name="EMP_PHONE")
   @MapKeyEnumerated(EnumType.STRING)
   @MapKeyColumn(name="PHONE_TYPE")
   @Column(name="PHONE_NUM")    private Map<PhoneType, String> phoneNumbers = new HashMap();

实例

以下代码来自Employee.java。

package cn.w3cschool.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.EnumType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.MapKeyEnumerated;

enum PhoneType {
   Home, Mobile, Work
}


@Entitypublic class Employee {
   @Id @GeneratedValue(strategy=GenerationType.IDENTITY)    private int id;    private String name;    private long salary;

   @ElementCollection
   @CollectionTable(name="EMP_PHONE")
   @MapKeyEnumerated(EnumType.STRING)
   @MapKeyColumn(name="PHONE_TYPE")
   @Column(name="PHONE_NUM")    private Map<PhoneType, String> phoneNumbers = new HashMap();
           
   @ManyToOne    private Department department;    
   public int getId() {
       return id;
   }    
   public void setId(int id) {
       this.id = id;
   }    
   public String getName() {
       return name;
   }    
   public void setName(String name) {
       this.name = name;
   }    public long getSalary() {
       return salary;
   }    public void setSalary(long salary) {
       this.salary = salary;
   }    
   public Department getDepartment() {
       return department;
   }    
   public void setDepartment(Department department) {
       this.department = department;
   }    public Map<PhoneType, String> getPhoneNumbers() {
       return phoneNumbers;
   }    public void setPhoneNumbers(Map<PhoneType, String> phoneNumbers) {
       this.phoneNumbers = phoneNumbers;
   }    
   public String toString() {
       StringBuffer aBuffer = new StringBuffer("Employee ");
       aBuffer.append(" id: ");
       aBuffer.append(id);
       aBuffer.append(" with dept: ");        if(null != department) {
           aBuffer.append(department.getName());
       }
       aBuffer.append(" phoneNumbers: ");        for (Map.Entry e : phoneNumbers.entrySet()) {
           aBuffer.append(e.getKey().toString() + "[" + e.getValue() + "] ");
       }        
       return aBuffer.toString();
   }
}

下面的代码来自PersonDaoImpl.java。

package cn.w3cschool.common;

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

import org.springframework.transaction.annotation.Transactional;

@Transactionalpublic class PersonDaoImpl {  public void test() {
   Employee e = new Employee();
   e.setName("Tom");
   
   Department d = new Department();
   d.addEmployee(e);
   d.setName("test");
   
   e.setDepartment(d);
   e.getPhoneNumbers().put(PhoneType.Home, "1231231231");
   
   d.addEmployee(e);
   em.persist(d);
   em.persist(e);
 }

 @PersistenceContext  private EntityManager em;
}

下面的代码来自Department.java。

package cn.w3cschool.common;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

@Entitypublic class Department {
   @Id @GeneratedValue(strategy=GenerationType.IDENTITY)    private int id;    private String name;
   
   @OneToMany(mappedBy="department")
   @OrderBy("name ASC")    private List<Employee> employees;    
   public Department() {
       employees = new ArrayList<Employee>();
   }    
   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 void addEmployee(Employee employee) {
           getEmployees().add( employee);            if (employee.getDepartment() != null) {
               employee.getDepartment().getEmployees().remove(employee.getName());
           }
           employee.setDepartment(this);
   }    
   public List<Employee> getEmployees() {
       return employees;
   }    public String toString() {
       StringBuffer aBuffer = new StringBuffer("Department ");
       aBuffer.append(" id: ");
       aBuffer.append(id);
       aBuffer.append(" name: ");
       aBuffer.append(name);
       aBuffer.append(" employeeCount: ");        if(null != employees) {          
           aBuffer.append(employees.size());
       }
       return aBuffer.toString();
   }
}

以下是数据库转储。

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

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





Table Name: EMP_PHONE
Row:
   Column Name: EMPLOYEE_ID,
   Column Type: INTEGER:
   Column Value: 1

   Column Name: PHONE_NUM,
   Column Type: VARCHAR:
   Column Value: 1231231231

   Column Name: PHONE_TYPE,
   Column Type: VARCHAR:
   Column Value: Home

惠州高端IT学校的知名品牌,要学习,好工作,请来到北大青鸟校区。

java6.png

Java

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表