create table Husband
(
id int not null auto_increment,
name varchar(20),
primary key (id)
);
create table Wife
(
id int not null auto_increment,
name varchar(20),
husband_id int,
primary key (id)
);
alter table Wife add constraint FK_Reference_1 foreign key (id)
references Husband (id) on delete restrict on update restrict;
Husband
package com.ono2one.bean;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name="husband",catalog="JPA_One2One_fk")
public class Husband implements Serializable{
private int id;
private String name;
private Wife wife;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
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;
}
@OneToOne(mappedBy="husband")
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
Wife
package com.ono2one.bean;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name="wife",catalog="JPA_One2One_fk")
public class Wife implements Serializable{
private int id;
private String name;
private Husband husband;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
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;
}
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="husband_id")
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}
JPAUtil
package com.ono2one.util;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtil {
private static EntityManager entityManager;
public static EntityManager getInstance(){
if(entityManager!=null){
return entityManager;
}else{
return makeInstance();
}
}
private static synchronized EntityManager makeInstance() {
if(entityManager==null){
EntityManagerFactory entityManagerFactory=Persistence.createEntityManagerFactory("JPA_One2One_fkPU");
return entityManagerFactory.createEntityManager();
}
return null;
}
}
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="JPA_One2One_fkPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.ono2one.bean.Husband</class>
<class>com.ono2one.bean.Wife</class>
<properties>
<property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/>
<property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3306/JPA_One2One_pk"/>
<property name = "hibernate.connection.username" value = "root"/>
<property name = "hibernate.connection.password" value = "root"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
HusbandDAO
package com.ono2one.dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.junit.Test;
import com.ono2one.bean.Husband;
import com.ono2one.bean.Wife;
import com.ono2one.util.JPAUtil;
public class HusbandDAO {
@Test
public void insert(){
EntityManager entityManager=JPAUtil.getInstance();
EntityTransaction entityTransaction=entityManager.getTransaction();
try {
entityTransaction.begin();
Husband husband=new Husband();
husband.setName("张三");
entityManager.persist(husband);
Wife wife=new Wife();
wife.setName("如花");
wife.setHusband(husband);
entityManager.persist(wife);
entityTransaction.commit();
} catch (Exception e) {
e.printStackTrace();
entityTransaction.rollback();
}
}
}