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

惠州什么是JPA嵌入式键映射?_北大青鸟IT学校

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


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

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

以下代码显示如何在一对多映射中使用嵌入式键。

实例

下面的代码来自Department.java。

package cn.w3cschool.common;

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

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

import javax.persistence.MapKey;
import javax.persistence.OneToMany;


@Entitypublic class Department {
   @Id @GeneratedValue(strategy=GenerationType.IDENTITY)    private int id;    private String name;
   
   @OneToMany(mappedBy="department")    private Map<EmployeeName, Employee> employeesByName;  
   
   public Department() {
       employeesByName = new HashMap<EmployeeName,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 Map<EmployeeName, Employee> getEmployees() {
       return employeesByName;
   }    public void addEmployee(Employee employee) {
       EmployeeName empName = new EmployeeName();
       empName.setFirst_Name(employee.getFirstName());
       empName.setLast_Name(employee.getLastName());
       employeesByName.put(empName, employee);        if (employee.getDepartment() != null) {
           employee.getDepartment().removeEmployee(employee);
       }
       employee.setDepartment(this);
   }    public void removeEmployee(Employee employee) {
       Iterator iter = employeesByName.entrySet().iterator();        while (iter.hasNext()) {
           Employee current = ((Map.Entry<EmployeeName,Employee>) iter.next()).getValue();            if (current.getId() == employee.getId()) {
               iter.remove();
               current.setDepartment(null);
           }
       }
   }    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(employeesByName.size());
       return aBuffer.toString();
   }
}

以下代码来自Employee.java。

package cn.w3cschool.common;

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

@Entitypublic class Employee {
   @Id @GeneratedValue(strategy=GenerationType.IDENTITY)    private int id;
   @Column(name="F_NAME")    private String firstName;
   @Column(name="L_NAME")    private String lastName;    
   private long salary;
   
   @ManyToOne    private Department department;    
   public int getId() {
       return id;
   }    
   public void setId(int id) {
       this.id = id;
   }    
   public String getFirstName() {
       return firstName;
   }    public void setFirstName(String firstName) {
       this.firstName = firstName;
   }    public String getLastName() {
       return lastName;
   }    public void setLastName(String lastName) {
       this.lastName = lastName;
   }    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 String toString() {
       StringBuffer aBuffer = new StringBuffer("Employee ");
       aBuffer.append(" id: ");
       aBuffer.append(id);
       aBuffer.append(" name: ");
       aBuffer.append(lastName);
       aBuffer.append(", ");
       aBuffer.append(firstName);
       aBuffer.append(" with dept: ");        if(null != department) {
           aBuffer.append(department.getName());
       }
       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.setFirstName("Tom");

   Department d = new Department();
 
   d.setName("test");
   
   e.setDepartment(d);
   
   EmployeeName name = new EmployeeName();
   name.setFirst_Name("Tom");
   d.getEmployees().put(name, e);
   
   
   em.persist(e);    
   em.persist(d);
 

 }

 @PersistenceContext  private EntityManager em;
}

以下代码来自EmployeeName.java。

package cn.w3cschool.common;


import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddablepublic class EmployeeName {
   @Column(name="F_NAME", insertable=false, updatable=false)    private String first_Name;
   @Column(name="L_NAME", insertable=false, updatable=false)    private String last_Name;    
   public String getFirst_Name() {
       return first_Name;
   }    public void setFirst_Name(String firstName) {
       first_Name = firstName;
   }    public String getLast_Name() {
       return last_Name;
   }    public void setLast_Name(String lastName) {
       last_Name = lastName;
   }
}

以下是数据库转储。

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: F_NAME,
   Column Type: VARCHAR:
   Column Value: Tom

   Column Name: L_NAME,
   Column Type: VARCHAR:
   Column Value: null

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

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

想了解更多关于Java的资讯,可以来惠州北大青鸟新方舟校区了解一下。

java4.png

Java

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


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