关闭

注解配置一对一

298人阅读 评论(0) 收藏 举报
分类:
Hibernate一对一外键双向关联(Annotation配置)
如上图所示:一个学生有一个学生证号,一个学生证号对应一名学生。在Hibernate中怎么用Annotation来实现呢?
学生类,主键是id;学生证的主键也是Id;
Student.java
package edu.xaut.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Student {
 
   private intid;
    privateString name;
    privateString gender;
    private intage;
    privateStuIdCard stuIdCard;

    @Id
   @GeneratedValue
    public intgetId() {
       returnid;
    }

    @Column(name= "name", length = 20)
    publicString getName() {
       returnname;
     

    @Column(name= "gender", length = 6)
    publicString getGender() {
       returngender;
    }

    public intgetAge() {
       returnage;
    }

   @OneToOne
   @JoinColumn(name = "StuIdCard")
    publicStuIdCard getStuIdCard() {
       returnstuIdCard;
    }

    public voidsetId(int id) {
       this.id =id;
    }

    public voidsetName(String name) {
       this.name =name;
    }
   
    public voidsetGender(String gender) {
       this.gender= gender;
    }
   
    public voidsetAge(int age) {
       this.age =age;
    }

    public voidsetStuIdCard(StuIdCard stuIdCard) {
      this.stuIdCard = stuIdCard;
    }
}

package edu.xaut.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class StuIdCard {
    private intid;
    privateString num;
    privateStudent student;

    @Id
   @GeneratedValue
    public intgetId() {
       returnid;
    }

    public voidsetId(int id) {
       this.id =id;
    }

   @Column(length = 20)
    publicString getNum() {
       returnnum;
    }

    public voidsetNum(String num) {
       this.num =num;
    }

   @OneToOne(mappedBy="stuIdCard")
    publicStudent getStudent() {
       returnstudent;
    }

    public voidsetStudent(Student student) {
       this.student= student;
    }
}

导出的SQL语句如下:
create table StuIdCard (
       id integer not null auto_increment,
       num varchar(20),
       primary key (id)
    )

    create tableStudent (
       id integer not null auto_increment,
       age integer not null,
       gender varchar(6),
       name varchar(20),
       StuIdCard integer,
       primary key (id)
    )

    alter tableStudent
       add index FKF3371A1BFDFA0D4 (StuIdCard),
       add constraint FKF3371A1BFDFA0D4
       foreign key (StuIdCard)
       references StuIdCard (id)

测试代码如下:
package edu.xaut.hibernate;

import org.hibernate.Session;
import org.hibernate.cfg.*;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import edu.xaut.wuqiang.hibernate.util.HibernateUtil;

public class HibernateORMappingTest {

    @Test
    public voidtestStudentSave() {
       Sessionsession =HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
       StuIdCardstuIdCard = new StuIdCard();
      stuIdCard.setNum("1008120672");
      session.save(stuIdCard);
      
       Student stu= new Student();
      stu.setName("Lily");
      stu.setGender("Female");
      stu.setAge(22);
      stu.setStuIdCard(stuIdCard);
      session.save(stu);      

      session.getTransaction().commit();
      HibernateUtil.getSessionFactory().close();
    }

    @Test
    public voidtestQueryStuInfo() {
       Sessionsession =HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
       StuIdCardstuIdCard = (StuIdCard) session.get(StuIdCard.class, 1);
      System.out.println(stuIdCard.getNum());
      System.out.println(stuIdCard.getStudent().getName() + "\t"
             +stuIdCard.getNum() + "\t"
             +stuIdCard.getStudent().getGender() + "\t"
             +stuIdCard.getStudent().getAge());
      session.getTransaction().commit();
      HibernateUtil.getSessionFactory().close();
    }

    @Test
    public voidtestSchemaExport() {
       newSchemaExport(newAnnotationConfiguration().configure()).create(
             true,true);
     
}

运行结果如下:
    select
       stuidcard0_.id as id0_1_,
       stuidcard0_.num as num0_1_,
       student1_.id as id1_0_,
       student1_.age as age1_0_,
       student1_.gender as gender1_0_,
       student1_.name as name1_0_,
       student1_.StuIdCard as StuIdCard1_0_
    from
       StuIdCard stuidcard0_
    left outerjoin
       Student student1_
           on stuidcard0_.id=student1_.StuIdCard
    where
       stuidcard0_.id=?
1008120672
Lily   1008120672   Female   22
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:109467次
    • 积分:1937
    • 等级:
    • 排名:千里之外
    • 原创:57篇
    • 转载:170篇
    • 译文:3篇
    • 评论:11条
    文章分类
    最新评论