hibernate 建表一对一 就是一对多,多的一方外键唯一unique

时间:2021-10-03 16:25:48

Person.java

 package cn.itcast.hiberate.sh.domain.onetoone;

 import java.io.Serializable;
import java.util.Set; public class Person implements Serializable{
private Long cid;
private String cname;
private String description; public Long getCid() {
return cid;
} public void setCid(Long cid) {
this.cid = cid;
} public String getCname() {
return cname;
} public void setCname(String cname) {
this.cname = cname;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
} private Address address;
}

Person.hbm.xml

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.hiberate.sh.domain.Classes">
<id name="cid" length="5" type="java.lang.Long">
<generator class="increment"></generator>
</id>
<property name="cname" length="20" type="java.lang.String"></property> <property name="description" length="100" type="java.lang.String"></property>
<!--
set元素对应类中的set集合
通过set元素使classes表与student表建立关联
key是通过外键的形式让两张表建立关联
one-to-many是通过类的形式让两个类建立关联 cascade 级联
save-update
1、当 保存班级的时候,对学生进行怎么样的操作
如果学生对象在数据库中没有对应的值,这个时候会执行save操作
如果学生对象在数据库中有对应的值,这个时候会执行update操作
delete
all
inverse 维护关系
true 不维护关系
false 维护关系
default false
-->
<set name="students" cascade="save-update" inverse="true">
<!--
key是用来描述外键
-->
<key>
<column name="cid"></column>
</key>
<one-to-many class="cn.itcast.hiberate.sh.domain.Student"/>
</set>
</class>
</hibernate-mapping>

Address.java

 package cn.itcast.hiberate.sh.domain.onetoone;

 import java.io.Serializable;

 public class Address implements Serializable{
private Long sid;
private String sname; private Person person; public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Long getSid() {
return sid;
}
public void setSid(Long sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
private String description;
}

Address.hbm.xml  外键唯一

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.hiberate.sh.domain.Student">
<id name="sid" length="5">
<generator class="increment"></generator>
</id>
<property name="sname" length="20"></property>
<property name="description" length="100"></property>
<!--
多对一
column 外键
-->
<many-to-one name="classes" class="cn.itcast.hiberate.sh.domain.Classes" column="cid" cascade="save-update" unique="true"></many-to-one>
</class>
</hibernate-mapping>