hibernate------one-to-one(通过外键方式实现)

以学生和电脑为例(Student-Computer)
建表sql语句:

  1. DROP TABLE IF EXISTS `student`;    
  2. CREATE TABLE `student` (    
  3.   `id` int(11) NOT NULL auto_increment,    
  4.   `name` varchar(255) NOT NULL,    
  5.   PRIMARY KEY  (`id`)    
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;    
  7.     
  8. DROP TABLE IF EXISTS `computer`;    
  9. CREATE TABLE `computer` (    
  10.   `id` int(11) NOT NULL auto_increment,    
  11.   `name` varchar(255) NOT NULL,    
  12.   `student_id` int(11) ,    
  13.   foreign key (`student_id`) references student(`id`),    
  14.   PRIMARY KEY  (`id`)    
  15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;    

 

Student.java:

  1. public class Student implements java.io.Serializable {    
  2.     
  3.     private Integer id;    
  4.     
  5.     private String name;    
  6.     
  7.     private Computer computer;    
  8.     
  9.     public Student() {    
  10.     }    
  11.     
  12.     public Student(String name) {    
  13.         this.name = name;    
  14.     }    
  15.     
  16.     public Integer getId() {    
  17.         return this.id;    
  18.     }    
  19.     
  20.     public void setId(Integer id) {    
  21.         this.id = id;    
  22.     }    
  23.     
  24.     public String getName() {    
  25.         return this.name;    
  26.     }    
  27.     
  28.     public void setName(String name) {    
  29.         this.name = name;    
  30.     }    
  31.     
  32.     public Computer getComputer() {    
  33.         return computer;    
  34.     }    
  35.     
  36.     public void setComputer(Computer computer) {    
  37.         this.computer = computer;    
  38.     }    
  39.            
  40. }    

 

Computer.java:

  1. public class Computer implements java.io.Serializable {    
  2.     
  3.     private Integer id;    
  4.     
  5.     private Student student;    
  6.     
  7.     private String name;    
  8.     
  9.     
  10.     public Computer() {    
  11.     }    
  12.     
  13.     
  14.    public Computer(String name) {    
  15.         this.name = name;    
  16.     }    
  17.     
  18.     
  19.     public Computer(Student student, String name) {    
  20.         this.student = student;    
  21.         this.name = name;    
  22.     }    
  23.     
  24.     public Integer getId() {    
  25.         return this.id;    
  26.     }    
  27.     
  28.     public void setId(Integer id) {    
  29.         this.id = id;    
  30.     }    
  31.     
  32.     public Student getStudent() {    
  33.         return this.student;    
  34.     }    
  35.     
  36.     public void setStudent(Student student) {    
  37.         this.student = student;    
  38.     }    
  39.     
  40.     public String getName() {    
  41.         return this.name;    
  42.     }    
  43.     
  44.     public void setName(String name) {    
  45.         this.name = name;    
  46.     }    
  47.     
  48. }    


Student.hbm.xml:

  1.  <class name="com.domain.Student" table="student" catalog="onetoone">    
  2.      <id name="id" type="java.lang.Integer">    
  3.          <column name="id" />    
  4.          <generator class="native" />    
  5.     </id>    
  6.     <property name="name" type="java.lang.String">    
  7.         <column name="name" not-null="true" />    
  8.     </property>    
  9.     <!-- class可以不写,因为根据name的值computer(属性),会通过反射自动找到属于哪个类的 -->    
  10.     <one-to-one cascade="delete,save-update" name="computer" class="com.domain.Computer" property-ref="student"></one-to-one>    
  11. </class>    


Computer.hbm.xml:

  1.  <class name="com.domain.Computer" table="computer" catalog="onetoone">    
  2.      <id name="id" type="java.lang.Integer">    
  3.          <column name="id" />    
  4.          <generator class="native" />    
  5.     </id>    
  6.        
  7.     <property name="name" type="java.lang.String">    
  8.         <column name="name" not-null="true" />    
  9.     </property>    
  10.         
  11.      <!-- many开头的是代表该表持有外键 -->    
  12.     <many-to-one  name="student" class="com.domain.Student"  unique="true">    
  13.         <column name="student_id" />    
  14.     </many-to-one>    
  15. </class>   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值