【hibernate实例】赵雅智_双向多对一客户与订单增加查找修改

新建持久化类
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();
	}
	

}

增加:




查找:


修改:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值