温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

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();
        }
    }
}
posted on 2010-10-14 20:37 雪山飞鹄 阅读(3369) 评论(0)  编辑  收藏 所属分类: JPA

只有注册用户登录后才能发表评论。


网站导航: