JPA视频学习(四)双向一对一关联配置
1.persistence.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="JPAOneToOnePU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/> <property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8"/> <property name = "hibernate.connection.username" value = "root"/> <property name="hibernate.max_fetch_depth" value="3"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
2.实体bean的配置:
people实体:
import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class People { private Integer id; private String name; private IDCard idcard; public People(){ } public People(String name){ this.name=name; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(length = 10, nullable = false) public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToOne(optional=false,cascade=CascadeType.ALL) @JoinColumn(name="idcard_id")//指定外键名称 public IDCard getIdcard() { return idcard; } public void setIdcard(IDCard idcard) { this.idcard = idcard; } }
IDCard实体:
import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class IDCard { private Integer id; private String cardno; private People people; public IDCard(){} public IDCard(String cardno){ this.cardno=cardno; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(length=18,nullable=false) public String getCardno() { return cardno; } public void setCardno(String cardno) { this.cardno = cardno; } @OneToOne(mappedBy="idcard",cascade={CascadeType.PERSIST ,CascadeType.MERGE,CascadeType.REFRESH} ,optional=false)//指定为关系的被维护端,这里的optional可以去掉,因为外键不允许为空 public People getPeople() { return people; } public void setPeople(People people) { this.people = people; } }
3.Junit测试:
package junit.test; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.junit.BeforeClass; import org.junit.Test; import com.persia.bean.IDCard; import com.persia.bean.People; public class OneToOneTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } //@Test public void save(){ EntityManagerFactory factory=Persistence.createEntityManagerFactory("JPAOneToOnePU"); EntityManager eManager=factory.createEntityManager(); eManager.getTransaction().begin(); People people=new People("一对一"); IDCard card=new IDCard("123466789"); people.setIdcard(card); card.setPeople(people); eManager.persist(people); eManager.getTransaction().commit(); eManager.close(); factory.close(); } //@Test public void getPeople(){ EntityManagerFactory factory=Persistence.createEntityManagerFactory("JPAOneToOnePU"); EntityManager eManager=factory.createEntityManager(); People people=eManager.find(People.class, 1); System.out.println(people.getName()+"idcard:"+people.getIdcard().getCardno()); eManager.close(); factory.close(); } //@Test public void updatePeople(){ EntityManagerFactory factory=Persistence.createEntityManagerFactory("JPAOneToOnePU"); EntityManager eManager=factory.createEntityManager(); eManager.getTransaction().begin(); People people=eManager.find(People.class, 1); people.setName("sky"); people.getIdcard().setCardno("987654321"); eManager.merge(people); eManager.getTransaction().commit(); eManager.close(); factory.close(); } @Test public void deletePeople(){ EntityManagerFactory factory=Persistence.createEntityManagerFactory("JPAOneToOnePU"); EntityManager eManager=factory.createEntityManager(); eManager.getTransaction().begin(); People people=eManager.find(People.class, 1); eManager.remove(people); eManager.getTransaction().commit(); eManager.close(); factory.close(); } }