标签:
package com.atguigu.hibernate.n2n; import java.util.HashSet; import java.util.Set; public class Category { private Integer id; private String name; private Set<Item> items = new HashSet<>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Item> getItems() { return items; } public void setItems(Set<Item> items) { this.items = items; } }
package com.atguigu.hibernate.n2n; import java.util.HashSet; import java.util.Set; public class Item { private Integer id; private String name; private Set<Category> categories = new HashSet<>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Category> getCategories() { return categories; } public void setCategories(Set<Category> categories) { this.categories = categories; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.atguigu.hibernate.n2n"> <class name="Category" table="CATEGORIES"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <!-- table: 指定中间表 --> <set name="items" table="CATEGORIES_ITEMS"> <key> <column name="C_ID" /> </key> <!-- 使用 many-to-many 指定多对多的关联关系. column 运行 Set 集合中的持久化类在中间表的外键列的名称 --> <many-to-many class="Item" column="I_ID"></many-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.atguigu.hibernate.n2n.Item" table="ITEMS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <set name="categories" table="CATEGORIES_ITEMS" inverse="true"> <key column="I_ID"></key> <many-to-many class="com.atguigu.hibernate.n2n.Category" column="C_ID"></many-to-many> </set> </class> </hibernate-mapping>
package com.atguigu.hibernate.n2n; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.SQLException; import java.util.Date; import java.util.Set; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.jdbc.Work; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; public class HibernateTest { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void init(){ Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); session = sessionFactory.openSession(); transaction = session.beginTransaction(); } @After public void destroy(){ transaction.commit(); session.close(); sessionFactory.close(); } @Test public void testGet(){ Category category = (Category) session.get(Category.class, 1); System.out.println(category.getName()); //须要连接中间表 Set<Item> items = category.getItems(); System.out.println(items.size()); } @Test public void testSave(){ Category category1 = new Category(); category1.setName("C-AA"); Category category2 = new Category(); category2.setName("C-BB"); Item item1 = new Item(); item1.setName("I-AA"); Item item2 = new Item(); item2.setName("I-BB"); //设定关联关系 category1.getItems().add(item1); category1.getItems().add(item2); category2.getItems().add(item1); category2.getItems().add(item2); item1.getCategories().add(category1); item1.getCategories().add(category2); item2.getCategories().add(category1); item2.getCategories().add(category2); //运行保存操作 session.save(category1); session.save(category2); session.save(item1); session.save(item2); } }
版权声明:本文博主原创文章。博客,未经同意,不得转载。
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/4758279.html