Hibernate annotation 一对一关系共享主键配置

Hibernate annotation 一对一关系共享主键配置

作者 武汉科技大学 周剑华

引言

在这个例子中.分为学生基本信息表(student_basic)和学生详细信息表(student_detail).两个表共享主键.利用hibernate annotation 的@one2one映射.为了共享主键,需要用到hibernate的@PrimaryKeyJoinColumn来完成.

 

数据库schema

    第一.        数据库的关系图

      (该文章书写时在word中完成.但是由于javaeye的编辑目前不支持图片拷贝.暂时无法显示该图片.该图片是一个ER图.描述student_basic和student_detail的关系)

    第二.ddl语句

 

 

 create table student_detail

(

student_idintnotnull,

home_addressvarchar(100),

home_phomevarchar(13)

);

/*==============================================================*/

/*Table:studnet_basic*/

/*==============================================================*/

createtablestudnet_basic

(

student_idintnotnull,

studnet_namevarchar(20),

sexvarchar(10),

primarykey(student_id)

);

alter table student_detail add constraint FK_R_Student_basic_detail foreign key (student_id)      references studnet_basic (student_id) on delete restrict on update restrict;

  

实体类配置

第一. StudentBasic类配置

package org.jenfer.interview.ch07.entity;

 

import java.io.Serializable;

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

 

@Entity(name="StudentBasic")

@Table(name="student_basic")

public class StudentBasic implements Serializable{

         private static final long serialVersionUID = 691350277643811847L;

 

         @Id

         @GeneratedValue(strategy=GenerationType.IDENTITY)

         @Column(name="student_id")

         private Integer studentId;

        

         @Column(length=10)

         private String studentName;

        

         @Column(length=10)

         private String sex;

        

         public StudentBasic() {

         }

 

         public Integer getStudentId() {

                   return studentId;

         }

 

         public void setStudentId(Integer studentId) {

                   this.studentId = studentId;

         }

 

         public String getStudentName() {

                   return studentName;

         }

 

         public void setStudentName(String studentName) {

                   this.studentName = studentName;

         }

 

         public String getSex() {

                   return sex;

         }

 

         public void setSex(String sex) {

                   this.sex = sex;

         }

}

 

第二 . StudentDetail配置

package org.jenfer.interview.ch07.entity;

 

import java.io.Serializable;

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.OneToOne;

import javax.persistence.PrimaryKeyJoinColumn;

import javax.persistence.Table;

 

import org.hibernate.annotations.GenericGenerator;

import org.hibernate.annotations.Parameter;

 

@Entity(name="StudentDetail")

@Table(name="student_detail")

public class StudentDetail implements Serializable{

         private static final long serialVersionUID = 2038888147029653370L;

        

         @Id

         @Column(name="student_id")

         @GeneratedValue(generator="foreigner")

         @GenericGenerator(name="foreigner",strategy="foreign",parameters={

                            @Parameter(name = "property",value="studentBasic")

         })

         private Integer stuId;

        

         @OneToOne

         @PrimaryKeyJoinColumn

         private StudentBasic studentBasic;

        

         @Column(length=50)

         private String homeAddress;

        

         @Column(length=100)

         private String homePhomeNumber;

 

         public StudentDetail() {

         }

 

         public StudentBasic getStudentBasic() {

                   return studentBasic;

         }

 

         public void setStudentBasic(StudentBasic studentBasic) {

                   this.studentBasic = studentBasic;

         }

 

         public String getHomeAddress() {

                   return homeAddress;

         }

 

         public void setHomeAddress(String homeAddress) {

                   this.homeAddress = homeAddress;

         }

 

         public String getHomePhomeNumber() {

                   return homePhomeNumber;

         }

 

         public void setHomePhomeNumber(String homePhomeNumber) {

                   this.homePhomeNumber = homePhomeNumber;

         }

 

         public Integer getStuId() {

                   return stuId;

         }

 

         public void setStuId(Integer stuId) {

                   this.stuId = stuId;

         }

}

 

小结

         由于JPA没有定义怎么共享主键的方法.所以必须得借助hibernate的属性来设置.在

@Id

         @Column(name="stu_id")

         @GeneratedValue(generator="foreigner")

         @GenericGenerator(name="foreigner",strategy="foreign",parameters={

                            @Parameter(name = "property",value="studentBasic")

         })

语句块中, @Parameter(name = "property", value="studentBasic")属性指出,从类StudentDetail的主键要共享studentBasic属性的主键值.

相关推荐