多对多双向关联
程序员文章站
2022-03-02 15:25:30
...
多对多双向关联
描述:书籍和类别的关系,多方持有多方的集合
- 创建持久化类
`public class Book {
/**
-
– 书本表
create table t_hibernate_book
(
book_id int primary key auto_increment,
book_name varchar(50) not null,
price float not null
);
*/private Integer book_id;
private String book_name;
private Set categories = new HashSet<>();public Book() {
}
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 Set getCategories() {
return categories;
}public void setCategories(Set categories) {
this.categories = categories;
}
}
`**
public class Category {
/**
* -- 书本类别表
create table t_hibernate_category
(
category_id int primary key auto_increment,
category_name varchar(50) not null
);
*/
private Integer category_id;
private String category_name;
private Set<Book> books = new HashSet<>();
public Category() {
}
public Integer getCategory_id() {
return category_id;
}
public void setCategory_id(Integer category_id) {
this.category_id = category_id;
}
public String getCategory_name() {
return category_name;
}
public void setCategory_name(String category_name) {
this.category_name = category_name;
}
public Set<Book> getBooks() {
return books;
}
public void setBooks(Set<Book> books) {
this.books = books;
}
}
- 创建持久化类的映射文件
<hibernate-mapping>
<class name="com.zking.five.hibernate.Book" table="t_hibernate_book">
<id name="book_id" type="java.lang.Integer">
<column name="book_id" />
<generator class="assigned" />
</id>
<property name="book_name" type="java.lang.String">
<column name="book_name" />
</property>
<set name="categories" inverse="false">
<key>
<column name="bid" />
</key>
<many-to-many class="com.zking.five.hibernate.Category" column="cid"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.zking.five.hibernate.Category" table="t_hibernate_category">
<id name="category_id" type="java.lang.Integer">
<column name="category_id" />
<generator class="assigned" />
</id>
<property name="category_name" type="java.lang.String">
<column name="category_name" />
</property>
<set name="books" inverse="false" table="t_hibernate_book_category">
<key>
<column name="cid" />
</key>
<!-- class 多方的全类名
column 中间表字段
-->
<many-to-many class="com.zking.five.hibernate.Book" column="bid" />
</set>
</class>
</hibernate-mapping>
- 创建配置文件
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">1920</property>
<!-- 属性方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- 本地事务(jdbc)getCurrentSession -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 全局事务 (jta)-->
<!-- <property name="hibernate.current_session_context_class">jta</property> -->
<!-- 引入实体类的映射文件 -->
<!--一对一单向外键关联 -->
<!-- <mapping class="oto_fk.Student"/>
<mapping class="oto_fk.IdCard"/> -->
<!-- 一对一双向外键关联-->
<!-- <mapping class="oto_bfk.Student"/>
<mapping class="oto_bfk.IdCard"/> -->
<!--多对一单向外键 -->
<!-- <mapping class="mto_fk.Student"/>
<mapping class="mto_fk.ClassRoom"/> -->
<!--多对一双向外键 -->
<!-- <mapping class="mto_bfk.Student"/>
<mapping class="mto_bfk.ClassRoom"/> -->
<!--多对多单向外键 -->
<!-- <mapping class="mtm_fk.Student"/>
<mapping class="mtm_fk.Teacher"/> -->
<!-- 一对多自向关联
<mapping resource="com/zking/five/hibernate/TreeNode.hbm.xml"/> -->
<!--多对多双向关联 -->
<mapping resource="com/zking/five/hibernate/Book.hbm.xml"/>
<mapping resource="com/zking/five/hibernate/Category.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 创建dao层
public class BookDao {
public Book get(Book book) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
Book book2 = session.get(book.getClass(), book.getBook_id());
// Book book3 = session.load(book.getClass(), book.getBook_id());
Hibernate.initialize(book2.getCategories());
transaction.commit();
return book2;
}
}
- 创建junit测试
public class TestTreeNode {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
private TreeNode s = null;
private TreeNodeDao dao = null;
private BookDao bookDao = null;
@Before
public void setUp() throws Exception {
Configuration config = new Configuration().configure();
sessionFactory =config.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
dao = new TreeNodeDao();
bookDao = new BookDao();
}
@After
public void tearDown() throws Exception {
// //提交事务
transaction.commit();
//关闭会话
session.close();
//关闭会话工厂
sessionFactory.close();
}
@Test
public void testOpenSession() {
}
@Test
public void testGetStudent() {
Book book = new Book();
book.setBook_id(4);
Book book2 = this.bookDao.get(book );
System.out.println(book2.getBook_name());
System.out.println(book2.getCategories());
for (Category c : book2.getCategories()) {
System.out.println(c.getCategory_name());
}
}
}
- 控制台打印语句
Hibernate:
create table t_hibernate_book (
book_id integer not null,
book_name varchar(255),
primary key (book_id)
) type=MyISAM
Hibernate:
create table t_hibernate_book_category (
cid integer not null,
bid integer not null,
primary key (cid, bid)
) type=MyISAM
Hibernate:
create table t_hibernate_category (
category_id integer not null,
category_name varchar(255),
primary key (category_id)
) type=MyISAM
Hibernate:
alter table Book_categories
add constraint FK59her4186gqvlvc91teyw1htw
foreign key (cid)
references t_hibernate_category (category_id)
Hibernate:
alter table Book_categories
add constraint FKf3ce8n8hcp7t5tq9bu01tbbru
foreign key (bid)
references t_hibernate_book (book_id)
Hibernate:
alter table t_hibernate_book_category
add constraint FKsatshu6s7jyjp1o4pbxgsou67
foreign key (bid)
references t_hibernate_book (book_id)
Hibernate:
alter table t_hibernate_book_category
add constraint FKen5rbwne9ce4o6r850p4j6fn8
foreign key (cid)
references t_hibernate_category (category_id)
十一月 25, 2018 8:58:23 下午 org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hiber[email protected]2291d9a0'
十一月 25, 2018 8:58:23 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]
上一篇: 双向关联多对多