JPA主键双向关联一对一映射

JPA主键双向关联一对一映射

create   table  Husband
(
   id                   
int   not   null  auto_increment,
   name                 
varchar ( 200 ),
   
primary   key  (id)
);
create   table  Wife
(
   id                   
int   not   null ,
   name                 
varchar ( 20 ),
   
primary   key  (id)
);
alter   table  Wife  add   constraint  FK_Reference_1  foreign   key  (id)
      
references  Husband (id)  on   delete   restrict   on   update   restrict ;
Wife.java
package  com.jpa.one2one.bean;

import  java.io.Serializable;

import  javax.persistence.Column;
import  javax.persistence.Entity;
import  javax.persistence.Id;
import  javax.persistence.OneToOne;
import  javax.persistence.PrimaryKeyJoinColumn;
import  javax.persistence.Table;

@SuppressWarnings(
" serial " )
@Entity
@Table
public   class  Wife  implements  Serializable{
    
private   int  id;
    
private  String name;
    
private  Husband husband;
    @Id
    @Column
    
public   int  getId() {
        
return  id;
    }
    
public   void  setId( int  id) {
        
this .id  =  id;
    }
    @Column(name
= " name " )
    
public  String getName() {
        
return  name;
    }
    
public   void  setName(String name) {
        
this .name  =  name;
    }
    @OneToOne
    @PrimaryKeyJoinColumn
    
public  Husband getHusband() {
        
return  husband;
    }
    
public   void  setHusband(Husband husband) {
        
this .husband  =  husband;
    }
    
}
Husband.java
package  com.jpa.one2one.bean;

import  javax.persistence.CascadeType;
import  javax.persistence.Column;
import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.GenerationType;
import  javax.persistence.Id;
import  javax.persistence.OneToOne;
import  javax.persistence.PrimaryKeyJoinColumn;
import  javax.persistence.Table;

@Entity
@Table
public   class  Husband {
    
    
private   int  id;
    
private  String name;
    
private  Wife wife;
    @Id
    @Column
    @GeneratedValue(strategy
= GenerationType.AUTO)
    
public   int  getId() {
        
return  id;
    }
    
public   void  setId( int  id) {
        
this .id  =  id;
    }
    @Column(name
= " name " )
    
public  String getName() {
        
return  name;
    }
    
public   void  setName(String name) {
        
this .name  =  name;
    }
    @OneToOne(cascade
= CascadeType.ALL)
    @PrimaryKeyJoinColumn
    
public  Wife getWife() {
        
return  wife;
    }
    
public   void  setWife(Wife wife) {
        
this .wife  =  wife;
    }
    
}
HusbandDAO
package  com.jpa.one2one.dao;

import  javax.persistence.EntityManager;
import  javax.persistence.EntityTransaction;

import  org.junit.Test;

import  com.jpa.one2one.bean.Husband;
import  com.jpa.one2one.bean.Wife;
import  com.jpa.one2one.util.JPAUtil;

public   class  HusbandDAO {
    
    @Test
    
public   void  insert(){
        EntityManager entityManager
= JPAUtil.getInstance();
        EntityTransaction transaction
= entityManager.getTransaction();
        
try  {
            transaction.begin();
            Husband husband
= new  Husband();
            husband.setName(
" 张三 " );
            entityManager.persist(husband);
            Wife wife
= new  Wife();
            
// wife.setHusband(husband);
            wife.setName( " 如花 " );
            wife.setId(husband.getId());
            entityManager.persist(wife);
            transaction.commit();
        } 
catch  (Exception e) {
            e.printStackTrace();
            transaction.rollback();
        }
    }
}
JPAUtil
package  com.jpa.one2one.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( " JPAPU " );
            
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 ="JPAPU"  transaction-type ="RESOURCE_LOCAL" >
        
< provider > org.hibernate.ejb.HibernatePersistence </ provider >
        
< class > com.jpa.one2one.bean.Wife </ class >
        
< class > com.jpa.one2one.bean.Husband </ class >
          
< properties >
            
< property  name  = "hibernate.connection.driver_class"  value  = "com.mysql.jdbc.Driver" />
            
< property  name  = "hibernate.connection.url"  value  = "jdbc:mysql://localhost:3306/JPA" />
            
< 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 >

示例程序

你可能感兴趣的:(JPA主键双向关联一对一映射)