zoukankan      html  css  js  c++  java
  • Hibernate框架:一对一映射

     //配置一对一关系,先选好主控方,也就是Student控制StudentLogin
    Student.java
    StuentLogin.java 
    1. package com.qingke.easyjava.hibernate.bean;
      import java.util.Date;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.OneToOne;
      import javax.persistence.PrimaryKeyJoinColumn;
      import javax.persistence.Table;
      @Table(name ="studentt")
      @Entity
      publicclassStudent{
      @Id
      @Column(name ="id")
      privateint id;
      @Column(name ="sid")
      privateint sid;
      @Column(name ="gender")
      privateString gender;
      @Column(name ="first_name")
      privateString firstname;
      @Column(name ="last_name")
      privateString lastname;
      @Column(name ="phone")
      privateString phone;
      @Column(name ="height")
      privateint height;
      @Column(name ="date_of_birth")
      privateDate date_of_birth;
      @OneToOne
      @PrimaryKeyJoinColumn
      privateStudentLogin login;
      publicStudent(){
      super();
      }
      publicint getId(){
      return id;
      }
      publicDate getDate_of_birth(){
      return date_of_birth;
      }
      publicvoid setDate_of_birth(Date date_of_birth){
      this.date_of_birth = date_of_birth;
      }
      publicvoid setId(int id){
      this.id = id;
      }
      publicint getSid(){
      return sid;
      }
      publicvoid setSid(int sid){
      this.sid = sid;
      }
      publicString getGender(){
      return gender;
      }
      publicvoid setGender(String gender){
      this.gender = gender;
      }
      publicString getFirstname(){
      return firstname;
      }
      publicvoid setFirstname(String firstname){
      this.firstname = firstname;
      }
      publicString getPhone(){
      return phone;
      }
      publicvoid setPhone(String phone){
      this.phone = phone;
      }
      publicint getHeight(){
      return height;
      }
      publicvoid setHeight(int height){
      this.height = height;
      }
      publicString getLastname(){
      return lastname;
      }
      publicvoid setLastname(String lastname){
      this.lastname = lastname;
      }
      publicStudentLogin getLogin(){
      return login;
      }
      publicvoid setLogin(StudentLogin login){
      this.login = login;
      }
      @Override
      publicString toString(){
      return"Student [id: "+ id +", sid: "+ sid +", gende: "+ gender +", firstname: "+ firstname +", lastname:"
      + lastname +", phone: "+ phone +", height: "+ height +", date_of_birth: "+ date_of_birth +"]";
      }
      }
      

        

     
     1 package com.qingke.easyjava.hibernate.bean;
     2 import javax.persistence.Column;
     3 import javax.persistence.Entity;
     4 import javax.persistence.Id;
     5 import javax.persistence.OneToOne;
     6 import javax.persistence.Table;
     7 @Table(name ="student_login")
     8 @Entity
     9 publicclassStudentLogin{
    10 @Id
    11 @Column(name ="id")
    12 privateint id;
    13 @Column(name ="username")
    14 privateString username;
    15 @Column(name ="password")
    16 privateString password;
    17 @OneToOne(mappedBy ="login")
    18 privateStudent student;
    19 publicStudentLogin(){
    20 }
    21 publicint getId(){
    22 return id;
    23 }
    24 publicvoid setId(int id){
    25 this.id = id;
    26 }
    27 publicString getUsername(){
    28 return username;
    29 }
    30 publicvoid setUsername(String username){
    31 this.username = username;
    32 }
    33 publicString getPassword(){
    34 return password;
    35 }
    36 publicvoid setPassword(String password){
    37 this.password = password;
    38 }
    39 publicString toString(){
    40 return"[id:"+ id +", username: "+ username +", password: "+ password +"]";
    41 }
    42 }
     
     1 package com.qingke.easyjava.hibernate;
     2 import org.hibernate.Session;
     3 import org.hibernate.SessionFactory;
     4 import org.hibernate.cfg.Configuration;
     5 import com.qingke.easyjava.hibernate.bean.Student;
     6 import com.qingke.easyjava.hibernate.dao.StudentDao;
     7 publicclassHibernateMappingApp{
     8 publicstaticvoid main(String[] args){
     9 SessionFactory sf =
    10 newConfiguration().configure().buildSessionFactory();
    11 Session session = sf.openSession();
    12 StudentDao dao =newStudentDao(session);
    13 //配置一对一关系,先选好主控方,也就是Student控制StudentLogin
    14 Student student = dao.getStudentById(5);
    15 System.out.println("Student:"+ student);
    16 System.out.println("Student_Login:"+ student.getLogin());
    17 session.close();
    18 sf.close();
    19 }
    20 }
    21  
  • 相关阅读:
    C语言I博客作业03
    经度和纬度
    TUXEDO client端示例程序 TUXEDO自带 方便查看
    关于网络字节序和主机字节序
    TUXEDO server端示例程序 TUXEDO自带 方便查看
    TUXEDO 通讯缓冲区类型
    Android中的五大布局
    Android四大组件之Activity(活动)及其布局的创建与加载布局
    Java的内部类
    Android单元测试
  • 原文地址:https://www.cnblogs.com/piaoandpiao/p/ba4270a8a6a858865728cb83ce83bffc.html
Copyright © 2011-2022 走看看