W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
以下代碼顯示了如何進(jìn)行單向一對多映射。
下面的代碼來自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() { Professor emp = new Professor(); emp.setName("name"); emp.setSalary(12345); Phone p = new Phone(); p.setNumber("1234509876");; emp.addPhone(p); em.persist(p); em.persist(emp); } @PersistenceContext private EntityManager em; }
以下代碼來自Professor.java。
package cn.w3cschool.common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.OneToMany; @Entity public class Professor { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @OneToMany @JoinTable(name="EMP_PHONE", joinColumns=@JoinColumn(name="EMP_ID"), inverseJoinColumns=@JoinColumn(name="PHONE_ID")) private Collection<Phone> phones; public Professor() { phones = new ArrayList<Phone>(); } 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 void addPhone(Phone phone) { if (!getPhones().contains(phone)) { getPhones().add(phone); } } public Collection<Phone> getPhones() { return phones; } public String toString() { return "Employee id: " + getId() + " name: " + getName() + " with " + getPhones().size() + " phones"; } }
以下代碼來自Phone.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; @Entity public class Phone { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Column(name="NUM") private String number; private String type; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String phoneNo) { this.number = phoneNo; } public String getType() { return type; } public void setType(String phoneType) { this.type = phoneType; } public String toString() { return "Phone id: " + getId() + ", no: " + getNumber() + ", type: " + getType(); } }下載 OneToMany_Unidirectional.zip
以下是數(shù)據(jù)庫轉(zhuǎn)儲。
Table Name: EMP_PHONE Row: Column Name: EMP_ID, Column Type: INTEGER: Column Value: 1 Column Name: PHONE_ID, Column Type: INTEGER: Column Value: 1 Table Name: PHONE Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: NUM, Column Type: VARCHAR: Column Value: 1234509876 Column Name: TYPE, Column Type: VARCHAR: Column Value: null Table Name: PROFESSOR Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: name Column Name: SALARY, Column Type: BIGINT: Column Value: 12345
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: