W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
以下代碼顯示級聯(lián)持久操作。
下面的代碼來自PersonDaoImpl.java。
package cn.w3cschool.common; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Employee emp = new Employee(); emp.setName("Tom"); emp.setId(1); Address addr = new Address(); addr.setId(1); addr.setStreet("street"); addr.setCity("city"); addr.setState("state"); emp.setAddress(addr); em.persist(emp); } @PersistenceContext private EntityManager em; }
以下代碼來自Employee.java。
package cn.w3cschool.common; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Employee { @Id private int id; private String name; @ManyToOne(cascade=CascadeType.PERSIST) Address address; 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 Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String toString() { return "Employee id: " + getId() + " name: " + getName() + " with " + getAddress(); } }
以下代碼來自Address.java。
package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Address { @Id private int id; private String street; private String city; private String state; private String zip; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStreet() { return street; } public void setStreet(String address) { this.street = address; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } public String toString() { return "Address id: " + getId() + ", street: " + getStreet() + ", city: " + getCity() + ", state: " + getState() + ", zip: " + getZip(); } }下載 Persist_With_Relationship_Cascade.zip
以下是數(shù)據(jù)庫轉儲。
Table Name: ADDRESS Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: CITY, Column Type: VARCHAR: Column Value: city Column Name: STATE, Column Type: VARCHAR: Column Value: state Column Name: STREET, Column Type: VARCHAR: Column Value: street Column Name: ZIP, Column Type: VARCHAR: Column Value: null Table Name: EMPLOYEE Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: Tom Column Name: ADDRESS_ID, Column Type: INTEGER: Column Value: 1
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: