Hibernate关系映射1:单向N-1关联
程序员文章站
2024-02-19 23:49:06
...
Hibernate关系映射1:单向N-1关联
单向N-1关联
单向N-1关系,比如多个人对应一个地址,只需从人实体端可以找到对应的地址实体,无须关系某个地址的全部住户。
单向 n-1 关联只需从 n 的一端可以访问 1 的一端。
域模型
从 Book到 Book_Category的多对一单向关联需要在Book类中定义一个 Book_Category属性, 而在 Book_Category类中无需定义存放 Book对象的集合属性
关系数据模型
Demo
Hibernate 使用 元素来映射多对一关联关系<many-to-one name="customer" class="Customer" column="CUSTOMER_ID" cascade="all" />
- name: 设定待映射的持久化类的属性的名字
- column: 设定和持久化类的属性对应的表的外键
- class:设定待映射的持久化类的属性的类型
- cascade:意味着系统将先自动级联插入主表记录,也就是说先持久化Customer对象,再持久化Person对象。开发时不建议使用该属性,建议使用手工的方式。
数据库代码:
下面是实体对象,分为Book(书本)和Book_Category(书本类别),其中书本和书本类别是N-1关系。
Book实体类:
package com.liuyongqi.MavenHibernateDemo3.entity;
/**
* 书本实体类
* @author Administrator
* @data 2018年8月2日
* @time 下午7:43:54
*/
import java.util.Date;
public class Book {
private Integer book_id ;
private String book_name ;
private Float book_price ;
private String book_auther ;
private Date book_date ;
private Book_Category book_category;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String book_name, Float book_price, String book_auther, Date book_date) {
super();
this.book_name = book_name;
this.book_price = book_price;
this.book_auther = book_auther;
this.book_date = book_date;
}
public Book(Integer book_id, String book_name, Float book_price, String book_auther, Date book_date,
Book_Category book_category) {
super();
this.book_id = book_id;
this.book_name = book_name;
this.book_price = book_price;
this.book_auther = book_auther;
this.book_date = book_date;
this.book_category = book_category;
}
public Integer getBook_id() {
return book_id;
}
public void setBook_id(Integer book_id) {
this.book_id = book_id;
}
public String getBook_name() {
return book_name;
}
public void setBook_name(String book_name) {
this.book_name = book_name;
}
public Float getBook_price() {
return book_price;
}
public void setBook_price(Float book_price) {
this.book_price = book_price;
}
public String getBook_auther() {
return book_auther;
}
public void setBook_auther(String book_auther) {
this.book_auther = book_auther;
}
public Date getBook_date() {
return book_date;
}
public void setBook_date(Date book_date) {
this.book_date = book_date;
}
public Book_Category getBook_category() {
return book_category;
}
public void setBook_category(Book_Category book_category) {
this.book_category = book_category;
}
@Override
public String toString() {
return "Book [book_id=" + book_id + ", book_name=" + book_name + ", book_price=" + book_price + ", book_auther="
+ book_auther + ", book_date=" + book_date + ", book_category=" + book_category + "]";
}
}
Book_Category实体类:
package com.liuyongqi.MavenHibernateDemo3.entity;
import java.util.HashSet;
import java.util.Set;
/**
* 书本类别实体类
* @author Administrator
* @data 2018年8月2日
* @time 下午7:46:08
*/
public class Book_Category {
private Integer book_category_id;
private String book_category_name;
public Book_Category() {
super();
// TODO Auto-generated constructor stub
}
public Book_Category(String book_category_name) {
super();
this.book_category_name = book_category_name;
}
public Book_Category(Integer book_category_id, String book_category_name) {
super();
this.book_category_id = book_category_id;
this.book_category_name = book_category_name;
}
public Integer getBook_category_id() {
return book_category_id;
}
public void setBook_category_id(Integer book_category_id) {
this.book_category_id = book_category_id;
}
public String getBook_category_name() {
return book_category_name;
}
public void setBook_category_name(String book_category_name) {
this.book_category_name = book_category_name;
}
@Override
public String toString() {
return "Book_Category [book_category_id=" + book_category_id + ", book_category_name=" + book_category_name
+ "]";
}
}
Book_Category实体类映射文件Book_Category.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-8-4 9:12:09 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.liuyongqi.MavenHibernateDemo3.entity.Book_Category" table="book_category">
<id name="book_category_id" type="java.lang.Integer">
<column name="book_category_id" />
<generator class="native" />
</id>
<property name="book_category_name" type="java.lang.String">
<column name="book_category_name" />
</property>
</class>
</hibernate-mapping>
Book实体类映射文件Book.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-8-4 9:12:09 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.liuyongqi.MavenHibernateDemo3.entity.Book" table="book">
<id name="book_id" type="java.lang.Integer">
<column name="book_id" />
<generator class="native" />
</id>
<property name="book_name" type="java.lang.String">
<column name="book_name" />
</property>
<property name="book_price" type="java.lang.Float">
<column name="book_price" />
</property>
<property name="book_auther" type="java.lang.String">
<column name="book_auther" />
</property>
<property name="book_date" type="java.util.Date">
<column name="book_date" />
</property>
<many-to-one name="book_category" class="com.liuyongqi.MavenHibernateDemo3.entity.Book_Category" fetch="join" cascade="all">
<column name="book_category_id" />
</many-to-one>
</class>
</hibernate-mapping>
测试类:
package com.liuyongqi.MavenHibernateDemo3.test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.liuyongqi.MavenHibernateDemo3.entity.Book;
import com.liuyongqi.MavenHibernateDemo3.entity.Book_Category;
import com.liuyongqi.MavenHibernateDemo3.util.SessionFactoryUtil;
/**
* 测试类
* @author Administrator
* @data 2018年8月2日
* @time 下午8:13:18
*/
public class Test {
public static void main(String[] args) {
Session session = SessionFactoryUtil.openSession();
Transaction transaction = session.beginTransaction();
Book_Category book_category=new Book_Category();
book_category.setBook_category_name("文学");
Book_Category book_category1=new Book_Category();
book_category1.setBook_category_name("科幻");
Book_Category book_category2=new Book_Category();
book_category2.setBook_category_name("历史");
Book_Category book_category3=new Book_Category();
book_category3.setBook_category_name("言情");
Book_Category book_category4=new Book_Category();
book_category4.setBook_category_name("漫画");
Book_Category book_category5=new Book_Category();
book_category5.setBook_category_name("悬疑");
Book book=new Book();
book.setBook_name("读者");
book.setBook_price(21.5F);
book.setBook_auther("读者出版传媒有限公司");;
book.setBook_date(new Date());
book.setBook_category(book_category);
Book book1=new Book();
book1.setBook_name("蓝血人");
book1.setBook_price(50F);
book1.setBook_auther("卫斯理");
book1.setBook_date(new Date());
book1.setBook_category(book_category1);
Book book2=new Book();
book2.setBook_name("傲慢与偏见");
book2.setBook_price(80F);
book2.setBook_auther("简.奥斯丁");
book2.setBook_date(new Date());
book2.setBook_category(book_category3);
Book book3=new Book();
book3.setBook_name("中国历史");
book3.setBook_price(21.5F);
book3.setBook_auther("人民");
book3.setBook_date(new Date());
book3.setBook_category(book_category2);
Book book4=new Book();
book4.setBook_name("七龙珠");
book4.setBook_price(21.5F);
book4.setBook_auther("不知道");
book4.setBook_date(new Date());
book4.setBook_category(book_category4);
Book book5=new Book();
book5.setBook_name("名侦探柯南");
book5.setBook_price(50F);
book5.setBook_auther("宫崎骏");
book5.setBook_date(new Date());
book5.setBook_category(book_category5);
session.save(book);
session.save(book1);
session.save(book2);
session.save(book3);
session.save(book4);
session.save(book5);
transaction.commit();
SessionFactoryUtil.closeSession();
}
}
由于在Order.hbm.xml中配置了cascade="all"
,所以只需要保存order对象即可,会首先自动保存级联的Customer对象。
运行后的结果:
控制台结果:
Hibernate:
insert
into
book_category
(book_category_name)
values
(?)
Hibernate:
insert
into
book
(book_name, book_price, book_auther, book_date, book_category_id)
values
(?, ?, ?, ?, ?)
Hibernate:
insert
into
book_category
(book_category_name)
values
(?)
Hibernate:
insert
into
book
(book_name, book_price, book_auther, book_date, book_category_id)
values
(?, ?, ?, ?, ?)
Hibernate:
insert
into
book_category
(book_category_name)
values
(?)
Hibernate:
insert
into
book
(book_name, book_price, book_auther, book_date, book_category_id)
values
(?, ?, ?, ?, ?)
Hibernate:
insert
into
book_category
(book_category_name)
values
(?)
Hibernate:
insert
into
book
(book_name, book_price, book_auther, book_date, book_category_id)
values
(?, ?, ?, ?, ?)
Hibernate:
insert
into
book_category
(book_category_name)
values
(?)
Hibernate:
insert
into
book
(book_name, book_price, book_auther, book_date, book_category_id)
values
(?, ?, ?, ?, ?)
Hibernate:
insert
into
book_category
(book_category_name)
values
(?)
Hibernate:
insert
into
book
(book_name, book_price, book_auther, book_date, book_category_id)
values
(?, ?, ?, ?, ?)
希望大家自己测试一下,才能自己真正的掌握。
如果期待我的下一篇文章,请留言!