W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在JPA中,我們可以將通用類型的Map映射到數(shù)據(jù)庫。
以下代碼定義了一個(gè)通用映射,其鍵值為Employee,值類型為Integer。
@ElementCollection @CollectionTable(name="EMP_SENIORITY") @MapKeyJoinColumn(name="EMP_ID") @Column(name="SENIORITY") private Map<Employee, Integer> seniorities;
下面的代碼來自Department.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.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.w3cschool.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
這里是數(shù)據(jù)庫表轉(zhuǎn)儲。
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
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: