hibernate 的多对多的关联和一对多的关联

数据库的多对多
1.1 数据库中不能直接映射多对多
处理:创建一个桥接表(中间表),将一个多对多关系转换成两个一对多

注1:数据库多表联接查询
永远就是二个表的联接查询

A   B   C  D
   t1   C
        t2 D
           t3


A	B	AB
select * from A,B,AB WHERE A.aID=AB.aID and b.bid = AB.bid
where 
在hibernate中,你只管查询当前表对象即可,
hibernate会字段关联桥表以及关联表查询出关联对象

1
2
3
4
5
6
7
8
9
10
11
这是多对多的一个例子

查询一本书为例
session .get(Book.class, 1)
select * from t_hibernate_book where book_id = ?(1)
resultSet -> 1	西游记	50
Book b = class.forname ("com.zking.five.entity.Book").new Instance();
b.setBook_id(1);
b.setBook_name(西游记);
b.getPrice(50);

hibernate 处理关联关系
1、通过 set 标签  找到桥接表(table 属性),
2、找到当前实体类对应表的主键在桥接表中的外键(key 标签中的column属性中定义)
3、查出关联表 (t_hibernate_category)中的主键(category_id )

select  cid  from t_hibernate_book_category where bid = ?(1)
 resultSet -> 关注最后的一列
 			 8	1	1
			 9	1	2
list<String > 1/ 2

 4、查出来的外键关联的实体类(class = com.zking.five.entity.Category),它可以与
 这个类的映射文件(class 标签中name  =com.zking.five.entity.Category),从而
 找到了对应的实体类对应的表的主键id标签中的colum字段 -》category_id
 
 select  *  from t_hibernate_category where category_id = ?(1)
 select  *  from t_hibernate_category where category_id = ?(2)
list<String >
1	古典
2	神话
class.forname ("com.zking.five.entity.Category").new Instance();
参照EntityBaseDao -> list<c> categories

5.b.setCategories(categories);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#关键点都在数据库中的外键上面,请好好理解下面这二句SQL和一对多及多对一的关系
#select * from Orders where cid=? //这条SQL返回客户对应的0-n个订单
#select * from Customers where customerId=? //这条SQL返回订单对应的1个客户
#通过这二条SQL不难看出,外键在这里有二个用途:查找客户的订单,查找订单对应的客户
2. 案例:如何建立客户和订单一对多双向关联
2.1 先不建立客户和订单的关联关系,定义实体及映射文件,单独执行保存操作
2.2 建立客户到订单的一对多关联关系

2.3 建立订单到客户的多对一关联关系

1 2 3 2.4 注意:在Hibernate当中定义实体对象的集合属性时,只能使用接口而不能使用类

ps :这是多对多的关联查询
实体类的代码:
1.这是书籍的代码:

public class Book {

private Integer bookId;
private String bookName;
private Float price ;
private Set<Category>  categories = new HashSet<>();
private Integer initCategorys = 0;

public Integer getInitCategorys() {
	return initCategorys;
}
public void setInitCategorys(Integer initCategorys) {
	this.initCategorys = initCategorys;
}
public Set<Category> getCategories() {
	return categories;
}
public void setCategories(Set<Category> categories) {
	this.categories = categories;
}
public Integer getBookId() {
	return bookId;
}
public void setBookId(Integer bookId) {
	this.bookId = bookId;
}
public String getBookName() {
	return bookName;
}
public void setBookName(String bookName) {
	this.bookName = bookName;
}
public Float getPrice() {
	return price;
}
public void setPrice(Float price) {
	this.price = price;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
这是书籍类型的代码:

public class Category {

private Integer categoryId;
private String categoryName;
private Set<Book> books = new HashSet<>();
private Integer initBooks = 0;

public Integer getInitBooks() {
	return initBooks;
}
public void setInitBooks(Integer initBooks) {
	this.initBooks = initBooks;
}
public Set<Book> getBooks() {
	return books;
}
public void setBooks(Set<Book> books) {
	this.books = books;
}
public Integer getCategoryId() {
	return categoryId;
}
public void setCategoryId(Integer categoryId) {
	this.categoryId = categoryId;
}
public String getCategoryName() {
	return categoryName;
}
public void setCategoryName(String categoryName) {
	this.categoryName = categoryName;
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
这是配置实体类的xml文件

配置书籍的xml:

<?xml version="1.0" encoding="UTF-8"?>
<class table="t_hibernate_book" name="com.zking.five.entity.Book">
	<id name="bookId" type="java.lang.Integer" column="book_id">
		<generator class="increment"></generator>
	</id>

	<property name="bookName" type="java.lang.String" column="book_name"></property>
	<property name="price" type="java.lang.Float" column="price"></property>

	<set table="t_hibernate_book_category" name="categories" cascade="save-update"
		inverse="false">
		<!-- one -->
		<key column="bid"></key>
		<!-- many 一个书对应多个类型 -->
		<many-to-many column="cid" class="com.zking.five.entity.Category" />
	</set>
</class>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 配置书籍类型的xml文件 <?xml version="1.0" encoding="UTF-8"?>
	<property name="categoryName" type="java.lang.String" column="category_name"></property>
	
	<set  table="t_hibernate_book_category" name="books" cascade="delete" inverse="true">
	<!-- one -->
	<key column="cid"></key>
	<!--  many  一个类别对应多本书-->
	<many-to-many  column="bid" class="com.zking.five.entity.Book"/>
	</set>
</class>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 注意:写完以后不要忘了 在主配置xml加上 实体类相对应的xml dao方法

//这是书籍的查询
public Book get(Book book) {
Configuration configure = new Configuration().configure(“hibernate.cfg.xml”);
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Book b = session.get(Book.class, book.getBookId());
if (b != null && new Integer(1).equals(book.getInitCategorys())) {
Hibernate.initialize(b.getCategories());
}
transaction.commit();
session.close();
return b;
}

//这是书籍类型的查询方法
public Category get(Category category) {
Configuration configure = new Configuration().configure(“hibernate.cfg.xml”);
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

	 Category c = session.get(Category.class, category.getCategoryId());
	 if(c != null && new Integer(1).equals(category.getInitBooks())) {
		 Hibernate.initialize(c.getBooks());
	 }
	transaction.commit();
	session.close();
	return c;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值