前几天已经学了Hibernate简单的配置了,今天我们来学一下Hibernate的一对多、多对一配置。
这里先说几点我在配置的时候遇到的一些问题。
正常的话,我们创建两个实体(也就是两张表,一张表的主键作为另一张表的外键),以前我们没有用到框架的时候,实体都是这样写的的 (简单写两个实体)
public class Demo {
private String aid;
private String aname;
public String getAid() {
return aid;
}
public void setAid(String aid) {
this.aid = aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
}
public class Demo1 {
private String bid;
private String bname;
private String aid;
public String getAid() {
return aid;
}
public void setAid(String aid) {
this.aid = aid;
}
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
}
看起来毫无问题,但是这样配置就错的,因为我们需要配置一对多,多对一的模式。在外键这一方我们就不需要在实体中配置aid(请看清楚了,是在实体中不用这样配置,不表示表中不需要)
下面就开始给出实例了,下面的实例亲测有效。
一的一方实体和配置文件以及表结构
实体
package tourism.domain;
//每条内容实体
public class Blog {
private String bid; //id
private String bimgtitle; //第一张图片地址
private String btitle; //标题
private String bcontext; //内容 (html)
private String babstract; //简介
private int bgood; //点赞数
private int bread; //阅读数
//注意这个地方是以前的配置方式 这是错的 现在不需要的
//如果你还是要取出cid 你完全可以在下面的Set集合中找到
// private String cid; //分类
// public String getCid() {
// return cid;
// }
// public void setCid(String cid) {
// this.cid = cid;
// }
private String bcreatedate; //创建时间
private String bauthor; //作者
private String blocation; //展示的位置
private int bcomment; //评论数
//一个文章有一个相对应的类别
private Categroy categroy;
public Categroy getCategroy() {
return categroy;
}
public void setCategroy(Categroy categroy) {
this.categroy = categroy;
}
public int getBcomment() {
return bcomment;
}
public void setBcomment(int bcomment) {
this.bcomment = bcomment;
}
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public String getBimgtitle() {
return bimgtitle;
}
public void setBimgtitle(String bimgtitle) {
this.bimgtitle = bimgtitle;
}
public String getBtitle() {
return btitle;
}
public void setBtitle(String btitle) {
this.btitle = btitle;
}
public String getBcontext() {
return bcontext;
}
public void setBcontext(String bcontext) {
this.bcontext = bcontext;
}
public String getBabstract() {
return babstract;
}
public void setBabstract(String babstract) {
this.babstract = babstract;
}
public int getBgood() {
return bgood;
}
public void setBgood(int bgood) {
this.bgood = bgood;
}
public int getBread() {
return bread;
}
public void setBread(int bread) {
this.bread = bread;
}
public String getBcreatedate() {
return bcreatedate;
}
public void setBcreatedate(String bcreatedate) {
this.bcreatedate = bcreatedate;
}
public String getBauthor() {
return bauthor;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public String getBlocation() {
return blocation;
}
public void setBlocation(String blocation) {
this.blocation = blocation;
}
}
配置
<?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="tourism.domain">
<class name="Blog" table="blog">
<id name="bid" column="bid">
<generator class="uuid"></generator>
</id>
<property name="bimgtitle" column="bimgtitle"></property>
<property name="btitle" column="btitle"></property>
<property name="bcontext" column="bcontext"></property>
<property name="babstract" column="babstract"></property>
<property name="bgood" column="bgood"></property>
<property name="bread" column="bread"></property>
<!--<property name="c_id" column="c_id"></property> -->
<property name="bcreatedate" column="bcreatedate"></property>
<property name="bauthor" column="bauthor"></property>
<property name="blocation" column="blocation"></property>
<property name="bcomment" column="bcomment"></property>
<many-to-one name="categroy" column="cid" class="Categroy">
</many-to-one>
</class>
</hibernate-mapping>
表结构
多的一方实体,配置和表结构
实体
package tourism.domain;
import java.util.HashSet;
import java.util.Set;
//类别实体
public class Categroy {
private String cid;
private String cname;
//一个类别可能包含多个文章
private Set<Blog> setBlog = new HashSet<Blog>();
public Set<Blog> getSetBlog() {
return setBlog;
}
public void setSetBlog(Set<Blog> setBlog) {
this.setBlog = setBlog;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
配置
<?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="tourism.domain">
<class name="Categroy" table="categroy">
<id name="cid" column="cid">
<generator class="uuid"></generator>
</id>
<property name="cname" column="cname"></property>
<!--
name:属性名
inverse:是否维护
class:实体名
column:外键名称
-->
<set name="setBlog" inverse="true">
<key column="cid"></key>
<one-to-many class="Blog"/>
</set>
</class>
</hibernate-mapping>
配置完这些,我们需要配置外键关系下面我给出我这两张表,添加外键和删除外键的语句
alter table blog add constraint cid foreign key (cid) references categroy(cid);
alter table blog drop foreign key blog_categroy_id;
接下来简单的测试一下(这里使用criteria查询来查询所有的Blog的bid和相对应的Categroy的cid)
如果有些配置或者测试代码看不懂,可以去看我之前写的Hibernate基本配置。
https://blog.csdn.net/tomwildboar/article/details/80695802