新建持久化类
Order.java
- package www.hbsi.net.one2manyboth;
- public class Order {
- private int id;
- private String no;
- private Customer customer;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNo() {
- return no;
- }
- public void setNo(String no) {
- this.no = no;
- }
- public Customer getCustomer() {
- return customer;
- }
- public void setCustomer(Customer customer) {
- this.customer = customer;
- }
- }
Customer.java
- package www.hbsi.net.one2manyboth;
- import java.util.Set;
- public class Customer {
- private int id;
- private String name;
- private Set<Order> orders;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set<Order> getOrders() {
- return orders;
- }
- public void setOrders(Set<Order> orders) {
- this.orders = orders;
- }
- }
新建持久化类配置文件
Order.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">
- <hibernate-mapping package="www.hbsi.net.one2manyboth">
- <class name="Order" table="orders">
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <property name="no" column="no" type="string"/>
- <many-to-one name="customer" column="customer_id" cascade="save-update"/>
- </class>
- </hibernate-mapping>
Customer.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">
- <hibernate-mapping package="www.hbsi.net.one2manyboth">
- <class name="Customer" table="customer">
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <property name="name" column="name" type="string"/>
- <set name="orders" inverse="true" cascade="save-update">
- <key column="customer_id"/>
- <one-to-many class="Order"/>
- </set>
- </class>
- </hibernate-mapping>
更改hibernate配置文件
hibernate.cfg.xml
创建测试类
App.java
- package www.hbsi.net.one2manyboth;
- import java.util.Set;
- import org.hibernate.Session;
- import org.junit.Test;
- import www.hbsi.net.util.HibernateSessionFactory;
- public class App {
- @Test
- public void add(){
- Session session = HibernateSessionFactory.getSession();
- session.beginTransaction();
- Customer customer = new Customer();
- customer.setName("客户2");
- Order order = new Order();
- order.setNo("1234");
- order.setCustomer(customer);
- Order order1 = new Order();
- order1.setNo("213");
- order1.setCustomer(customer);
- //order维护保存order
- session.save(order);
- session.save(order1);
- session.getTransaction().commit();
- HibernateSessionFactory.closeSession();
- }
- //查询1号客户的所有订单
- @Test
- public void find(){
- Session session = HibernateSessionFactory.getSession();
- session.beginTransaction();
- Customer cust = (Customer) session.get(Customer.class,1);
- Set<Order> orders = cust.getOrders();
- for(Order o:orders){
- System.out.println(o.getNo());
- }
- session.getTransaction().commit();
- HibernateSessionFactory.closeSession();
- }
- //改变订单的所有者 將4号订单给1号客户
- @Test
- public void change(){
- Session session = HibernateSessionFactory.getSession();
- session.beginTransaction();
- //查找4号订单
- Order o4 = (Order) session.get(Order.class,4);
- //查找1号客户
- Customer c1 = (Customer) session.get(Customer.class,1);
- //建立关联关系
- o4.setCustomer(c1);
- c1.getOrders().add(o4);
- session.getTransaction().commit();
- HibernateSessionFactory.closeSession();
- }
- }
增加:
查找:
修改: