hibernate 建表一对一 就是一对多,多的一方外键唯一unique
Person.java
1 package cn.itcast.hiberate.sh.domain.onetoone; 2 3 import java.io.Serializable; 4 import java.util.Set; 5 6 public class Person implements Serializable{ 7 private Long cid; 8 private String cname; 9 private String description; 10 11 public Long getCid() { 12 return cid; 13 } 14 15 public void setCid(Long cid) { 16 this.cid = cid; 17 } 18 19 public String getCname() { 20 return cname; 21 } 22 23 public void setCname(String cname) { 24 this.cname = cname; 25 } 26 27 public String getDescription() { 28 return description; 29 } 30 31 public void setDescription(String description) { 32 this.description = description; 33 } 34 35 public Address getAddress() { 36 return address; 37 } 38 39 public void setAddress(Address address) { 40 this.address = address; 41 } 42 43 private Address address; 44 }
Person.hbm.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <class name="cn.itcast.hiberate.sh.domain.Classes"> 6 <id name="cid" length="5" type="java.lang.Long"> 7 <generator class="increment"></generator> 8 </id> 9 <property name="cname" length="20" type="java.lang.String"></property> 10 11 <property name="description" length="100" type="java.lang.String"></property> 12 <!-- 13 set元素对应类中的set集合 14 通过set元素使classes表与student表建立关联 15 key是通过外键的形式让两张表建立关联 16 one-to-many是通过类的形式让两个类建立关联 17 18 cascade 级联 19 save-update 20 1、当 保存班级的时候,对学生进行怎么样的操作 21 如果学生对象在数据库中没有对应的值,这个时候会执行save操作 22 如果学生对象在数据库中有对应的值,这个时候会执行update操作 23 delete 24 all 25 inverse 维护关系 26 true 不维护关系 27 false 维护关系 28 default false 29 --> 30 <set name="students" cascade="save-update" inverse="true"> 31 <!-- 32 key是用来描述外键 33 --> 34 <key> 35 <column name="cid"></column> 36 </key> 37 <one-to-many class="cn.itcast.hiberate.sh.domain.Student"/> 38 </set> 39 </class> 40 </hibernate-mapping>
Address.java
1 package cn.itcast.hiberate.sh.domain.onetoone; 2 3 import java.io.Serializable; 4 5 public class Address implements Serializable{ 6 private Long sid; 7 private String sname; 8 9 private Person person; 10 11 12 public Person getPerson() { 13 return person; 14 } 15 public void setPerson(Person person) { 16 this.person = person; 17 } 18 public Long getSid() { 19 return sid; 20 } 21 public void setSid(Long sid) { 22 this.sid = sid; 23 } 24 public String getSname() { 25 return sname; 26 } 27 public void setSname(String sname) { 28 this.sname = sname; 29 } 30 public String getDescription() { 31 return description; 32 } 33 public void setDescription(String description) { 34 this.description = description; 35 } 36 private String description; 37 }
Address.hbm.xml 外键唯一
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping> 5 <class name="cn.itcast.hiberate.sh.domain.Student"> 6 <id name="sid" length="5"> 7 <generator class="increment"></generator> 8 </id> 9 <property name="sname" length="20"></property> 10 <property name="description" length="100"></property> 11 <!-- 12 多对一 13 column 外键 14 --> 15 <many-to-one name="classes" class="cn.itcast.hiberate.sh.domain.Classes" column="cid" cascade="save-update" unique="true"></many-to-one> 16 </class> 17 </hibernate-mapping>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。