Hibernate中的一对一多映射和一对一映射的创建
1.首先创建Main类,它里面包含了两种映射,和Second的一对多以及和Forrum的一对一
package com.ch.entity; import java.util.Date; import java.util.HashSet; import java.util.Set; public class Main { private int id; //主键 private String main_title; //标题 private String main_content; //内容 private Date main_createtime; //发帖的时间 private String main_createuser; //发帖的用户 private Forum forum; //主贴的信息 private Set<Second> seconds=new HashSet<Second>(); //跟帖 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getMain_title() { return main_title; } public void setMain_title(String main_title) { this.main_title = main_title; } public String getMain_content() { return main_content; } public void setMain_content(String main_content) { this.main_content = main_content; } public Date getMain_createtime() { return main_createtime; } public void setMain_createtime(Date main_createtime) { this.main_createtime = main_createtime; } public String getMain_createuser() { return main_createuser; } public void setMain_createuser(String main_createuser) { this.main_createuser = main_createuser; } public Forum getForum() { return forum; } public void setForum(Forum forum) { this.forum = forum; } public Set<Second> getSeconds() { return seconds; } public void setSeconds(Set<Second> seconds) { this.seconds = seconds; } }
2.创建映射文件Main.hbm.xml
<?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="com.ch.entity"> <class name="Main" table="my_main"> <id name="id" column="main_id"> <generator class="native"></generator> </id> <property name="main_title"></property> <property name="main_content"></property> <property name="main_createtime"></property> <property name="main_createuser"></property> <!-- 一对一,没有外键--> <one-to-one name="forum" class="Forum" cascade="all"></one-to-one> <!-- 多对一,没有外键--> <set name="seconds" cascade="all" > <key column="main_id"></key> <one-to-many class="Second"/> </set> </class> </hibernate-mapping>
3.创建Forum类
package com.ch.entity; import java.util.Date; public class Forum { private int id; //主键 private Main main; //所属的帖子 private int info_reply; //回复的数量 private int info_see; //查看的数量 private String info_lastuser; //最后回复的用户 private Date info_lastime; //最后回复的时间 private Dictionary dict; //板块 private int dict_id; //自己设置的板块id private String main_id; //自己设置的外键 public String getMain_id() { return main_id; } public void setMain_id(String main_id) { this.main_id = main_id; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getDict_id() { return dict_id; } public void setDict_id(int dict_id) { this.dict_id = dict_id; } public Main getMain() { return main; } public void setMain(Main main) { this.main = main; } public int getInfo_reply() { return info_reply; } public void setInfo_reply(int info_reply) { this.info_reply = info_reply; } public int getInfo_see() { return info_see; } public void setInfo_see(int info_see) { this.info_see = info_see; } public String getInfo_lastuser() { return info_lastuser; } public void setInfo_lastuser(String info_lastuser) { this.info_lastuser = info_lastuser; } public Date getInfo_lastime() { return info_lastime; } public void setInfo_lastime(Date info_lastime) { this.info_lastime = info_lastime; } public Dictionary getDict() { return dict; } public void setDict(Dictionary dict) { this.dict = dict; } }
4.创建一对一的唯一外键约束-------Forum.hbm.xml
<?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="com.ch.entity"> <class name="Forum" table="forum_info"> <id name="id" column="info_id"> <generator class="native"></generator> </id> <property name="info_reply"></property> <property name="info_see"></property> <property name="info_lastuser"></property> <property name="info_lastime"></property> <!-- 一对一映射,有外键方 unique="true" 给外键字段添加唯一约束 --> <many-to-one name="main" unique="true" column="main_id" class="Main" cascade="save-update"></many-to-one> <!-- 多对一映射,由外建方 --> <many-to-one name="dict" class="Dictionary" column="dict_id" ></many-to-one> </class> </hibernate-mapping>
5.创建Second类
package com.ch.entity; import java.util.Date; public class Second { private int id; //主键 private Main main; //主贴 private String sec_content; //内容 private Date sec_createtime; //发帖时间 private String sec_createuser; //发帖人 public int getId() { return id; } public void setId(int id) { this.id = id; } public Main getMain() { return main; } public void setMain(Main main) { this.main = main; } public String getSec_content() { return sec_content; } public void setSec_content(String sec_content) { this.sec_content = sec_content; } public Date getSec_createtime() { return sec_createtime; } public void setSec_createtime(Date sec_createtime) { this.sec_createtime = sec_createtime; } public String getSec_createuser() { return sec_createuser; } public void setSec_createuser(String sec_createuser) { this.sec_createuser = sec_createuser; } }
6.创建Second的映射文件Second.hbm.xml
<?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="com.ch.entity"> <class name="Second" table="my_second"> <id name="id" column="sec_id"> <generator class="native"></generator> </id> <property name="sec_content"></property> <property name="sec_createtime"></property> <property name="sec_createuser"></property> <!-- 多对一的映射 由外键方 --> <many-to-one name="main" column="main_id" class="Main" /> </class> </hibernate-mapping>