首页 > 代码库 > 利用hibernate实现数据库对象关联(多对一、一对多、一对一、多对多)
利用hibernate实现数据库对象关联(多对一、一对多、一对一、多对多)
核心思想:
数据库表对应java类(表-类),表的字段对应类的属性(字段-属性)
关联:把数据库中的外键映射,变成对象的关联关系。
多对一,一对多,一对一,多对多
单向,双向
一、单向,多对一(Info,Nation多个人有同一个民族)
准备两个基本类和映射文件
1.给类加关联对象
import java.util.Date; public class Info { private String code; private String name; private Boolean sex; private Date birthday; private Nation nationObj;//给类加关联对象。添加一个成员nationObj,并生成getter和setter public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Nation getNationObj() { return nationObj; } public void setNationObj(Nation nationObj) { this.nationObj = nationObj; } }
public class Nation { private String code; private String name; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.给hbm.xml文件加关联映射
给Info.hbm.xml加上<many-to-one>
<many-to-one name="nationObj" column="nation"></many-to-one>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="maya.model"> <class name="Info" table="Info"> <id name="code" type="java.lang.String"> <column name="code" /> <generator class="assigned"></generator> </id> <property name="name" type="java.lang.String"> <column name="name" /> </property> <property name="sex" type="java.lang.Boolean"> <column name="sex" /> </property> <property name="birthday" type="java.util.Date"> <column name="birthday" /> </property> <!--加上<many-to-one> --> <!--column:列,class:类名, cascade="save-update",加这个配置属性目的是,many表和one表一起更新 --> <many-to-one name="nationObj" column="nation" class="Nation" cascade="save-update"> </many-to-one> </class> </hibernate-mapping>
注意:
1.主要针对的是哪个对象? --- Info
2.主表是谁?从表是谁?主表是约束别人的,从表是被约束的。 外键在从表上。
主表是nation,从表是info
3.在实体类中,外键属性与关联对象,留一个就可以。在hbm.xml文件中,外键属性与<many-to-one>也留一个就可以。
如果两个都保留,记得在<many-to-one>上加上两个属性insert=false,update=false。
4.一般需要加上cascade="save-update",加这个配置业属性目的是,many表和one表一起更新
二、单向,一对一(Login和Info)
准备两个一对一的表和类
1.给类加关联对象
给Login加上infoObj属性
public class Login { private int ids; private String userName; private String password; private Info infoObj; public int getIds() { return ids; } public void setIds(int ids) { this.ids = ids; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Info getInfoObj() { return infoObj; } public void setInfoObj(Info infoObj) { this.infoObj = infoObj; } }
2.给Login.hbm.xml加上<many-to-one>配置
<many-to-one name="infoObj" column="infoCode" unique="true"/>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-4 12:27:40 by Hibernate Tools 5.2.1.Final --> <hibernate-mapping> <class name="maya.model.Login" table="Login"><!-- 上面的hibernate-mapping不带包的路径,class就得带类的路径,下同 --> <id name="ids" type="int"> <column name="ids" /> <generator class="native"></generator> </id> <property name="userName" type="java.lang.String"> <column name="userName" /> </property> <property name="password" type="java.lang.String"> <column name="password" /> </property> <!-- unique的意思是唯一性 --> <many-to-one name="infoObj" column="InfoCode" class="maya.model.Info" unique="true"> </many-to-one> </class> </hibernate-mapping>
三、单向,一对多(Info,Work)
1.给类加关联对象(集合对象)
private Set<Work> works;
import java.util.Date; import java.util.HashSet; import java.util.Set; public class Info { private String code; private String name; private Boolean sex; private Date birthday; private Nation nationObj; private Set<Work> works = new HashSet<Work>(0);//给类加关联对象(集合对象) public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Nation getNationObj() { return nationObj; } public void setNationObj(Nation nationObj) { this.nationObj = nationObj; } public Set<Work> getWorks() { return works; } public void setWorks(Set<Work> works) { this.works = works; } }
import java.util.Date; public class Work { private int ids; private String infoCode; private Date startDate; private Date endDate; private String firm; private String depart; private int orders; private Info infoObj;// public int getIds() { return ids; } public void setIds(int ids) { this.ids = ids; } public String getInfoCode() { return infoCode; } public void setInfoCode(String infoCode) { this.infoCode = infoCode; } public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } public Date getEndDate() { return endDate; } public void setEndDate(Date endDate) { this.endDate = endDate; } public String getFirm() { return firm; } public void setFirm(String firm) { this.firm = firm; } public String getDepart() { return depart; } public void setDepart(String depart) { this.depart = depart; } public int getOrders() { return orders; } public void setOrders(int orders) { this.orders = orders; } public Info getInfoObj() { return infoObj; } public void setInfoObj(Info infoObj) { this.infoObj = infoObj; } }
2.给Info.hbm.xml配置
<set name="实体类中集合成员的变量名(works)">
<one-to-many class="Work">
<key column="infoCode">
</set>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="maya.model"> <class name="Info" table="Info"> <id name="code" type="java.lang.String"> <column name="code" /> <generator class="assigned"></generator> </id> <property name="name" type="java.lang.String"> <column name="name" /> </property> <property name="sex" type="java.lang.Boolean"> <column name="sex" /> </property> <property name="birthday" type="java.util.Date"> <column name="birthday" /> </property> <set name="works" table="Work" cascade="save-update" ><!-- name="实体类中集合成员的变量名(works)"--> <key> <column name="infoCode" /> </key> <one-to-many class="Work" /> </set> </class> </hibernate-mapping>
四、多对多(Student和Course,中间表Score)
数据库中没有多对多的关系。只有程序里面有。数据库中是使用两个一对多和多对一来实现多对多的。典型的是:中间表
在程序中,如果中间表中没有其它信息,那就可以不用写中间表的实体类。
多对多,还可以用在:用户和角色的关系上。
1.给类加关联对象(集合对象)
import java.util.Date; import java.util.Set; public class Student { private String sno; private String sname; private String ssex; private Date sbirthday; private String sclass; private Set<Course> courses; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public Date getSbirthday() { return sbirthday; } public void setSbirthday(Date sbirthday) { this.sbirthday = sbirthday; } public String getSclass() { return sclass; } public void setSclass(String sclass) { this.sclass = sclass; } public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } }
import java.util.Set; public class Course { private String cno; private String cname; private String tno; private Set<Student> students; public String getCno() { return cno; } public void setCno(String cno) { this.cno = cno; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public String getTno() { return tno; } public void setTno(String tno) { this.tno = tno; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
2.给hbm.xml配置
<set name="当前类的集合属性" table="中间表">
<key column="中间表中与此表对应的外键"></key>
<many-to-many class="另外多方对象的类名" column="另外多方对象在中间表中的外键"></many-to-many>
</set>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-4 12:27:40 by Hibernate Tools 5.2.1.Final --> <hibernate-mapping package="maya.model"> <class name="Course" table="Course"> <id name="cno" type="java.lang.String"> <generator class="assigned"></generator> </id> <property name="cname"/> <property name="tno" /> <set name="students" table="score"> <key column="cno"></key> <many-to-many class="Student" column="sno"></many-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-4 12:27:40 by Hibernate Tools 5.2.1.Final --> <hibernate-mapping package="com.itnba.maya.model"> <class name="Student" table="Student"> <id name="sno" type="java.lang.String"> <generator class="assigned"></generator> </id> <property name="sname"/> <property name="ssex" /> <property name="sbirthday" /> <property name="sclass" column="class" /> <set name="courses" table="score"> <key column="sno"></key> <many-to-many class="Course" column="cno"></many-to-many> </set> </class> </hibernate-mapping>
利用hibernate实现数据库对象关联(多对一、一对多、一对一、多对多)