溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/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 雪山飛鵠 閱讀(3380) 評論(0)  編輯  收藏 所屬分類: JPA
          主站蜘蛛池模板: 五莲县| 海口市| 芒康县| 龙山县| 阿鲁科尔沁旗| 翁牛特旗| 龙口市| 卢湾区| 信丰县| 二连浩特市| 永平县| 长垣县| 上饶县| 武义县| 巩留县| 岐山县| 平阴县| 伊宁市| 夏津县| 屏东市| 荆州市| 通道| 曲阳县| 泰宁县| 保定市| 同江市| 新乡市| 中山市| 于田县| 道真| 深州市| 朝阳县| 郯城县| 洪雅县| 连平县| 什邡市| 获嘉县| 襄城县| 黎川县| 海门市| 壶关县|