Hibernate关联关系

Hibernate关联关系,第1张

Hibernate关联关系

1、一对多的关联配置

       数据库:主表从表,通过外键关联

      类:           

 Class A{
        B b=new B();
 }
 Class B{
  }

2.懒加载

3.一对多的自关联

4.多对多的关联

一、一对多的关联配置 1、关联指的是类之间的引用关系。

如果类A与类B关联,那么被引用的类B将被定义为类A的属性。

2、一对多的关联:

例如:订单和订单项,一个订单有多个订单项;

         但是一个订单项只有一个订单;

3、创建实体类和映射文件

①、实体类:

(1)Order 

package com.mwy.entity;
import java.util.HashSet;
import java.util.Set;
public class Order {
//	create table t_hibernate_order
//	(
//	  order_id int primary key auto_increment,
//	  order_no varchar(50) not null
//	);
	private Integer orderId;
	private String orderNo;
	
	//注意:变量属性一定用接口进行接受
	private Set orderItems = new HashSet<>();
	
	private Integer initOrderItems = 0;//0代表懒加载,1代表强制加载
	
	public Integer getInitOrderItems() {
		return initOrderItems;
	}
	public void setInitOrderItems(Integer initOrderItems) {
		this.initOrderItems = initOrderItems;
	}
	public Set getOrderItems() {
		return orderItems;
	}
	public void setOrderItems(Set orderItems) {
		this.orderItems = orderItems;
	}
	public Integer getOrderId() {
		return orderId;
	}
	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}
	public String getOrderNo() {
		return orderNo;
	}
	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}
}

(2)OrderItem 

package com.mwy.entity;
public class OrderItem {
//	create table t_hibernate_order_item
//	(
//	  order_item_id int primary key auto_increment,
//	  product_id int not null,
//	  quantity int not null,
//	  oid int not null,
//	  foreign key(oid) references t_hibernate_order(order_id)
//	);
	private Integer orderItemId;
	private Integer productId;
	private Integer quantity;
	private Integer oid;
	
	private Order order;
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	public Integer getOrderItemId() {
		return orderItemId;
	}
	public void setOrderItemId(Integer orderItemId) {
		this.orderItemId = orderItemId;
	}
	public Integer getProductId() {
		return productId;
	}
	public void setProductId(Integer productId) {
		this.productId = productId;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	public Integer getOid() {
		return oid;
	}
	public void setOid(Integer oid) {
		this.oid = oid;
	}
}

②、映射文件:

(1)Order.hbm.xml





	
		
	
	
	
	
	
	
		
		
	

(2)OrderItem.hbm.xml





	
		
	
	
	
	
	
	
	
	
	

核心配置文件

 
       
         

工具类;

package com.mwy.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionFactoryUtil {
	static SessionFactory sessionFactory;
	static {
		Configuration configure = new Configuration().configure("hibernate.cfg.xml");
		sessionFactory = configure.buildSessionFactory();
	}
	public static Session getSession() {
		Session session = sessionFactory.getCurrentSession();
		if(session == null) {
			session = sessionFactory.openSession();
		}
		return session;
	}
	public static void main(String[] args) {
		Session session = SessionFactoryUtil.getSession();
//		Calling method 'isConnected' is not valid without an active transaction (Current status: NOT_ACTIVE)
		session.beginTransaction();
		System.out.println(session.isConnected());
		session.close();
		System.out.println(session.isConnected());
	}
}
4、dao层

①、OrderDao 

package com.mwy.dao;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.mwy.entity.Order;
import com.mwy.entity.OrderItem;
import com.mwy.util.SessionFactoryUtil;
public class OrderDao {
	//查询
	public Order get(Order order) {
		Session session = SessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		Order o = session.get(Order.class, order.getOrderId());
//		并且想要查询出关联的订单项的数据是,采用强制加载
		if(o != null && new Integer(1).equals(order.getInitOrderItems())) {
			Hibernate.initialize(o.getOrderItems());
		}
		transaction.commit();
		session.close();
		return o;
	}
	//查询所有
	public List list() {
		Session session = SessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		List list = session.createQuery("from Order").list();
		for (Order o: list) {
			Hibernate.initialize(o.getOrderItems());
		}
		transaction.commit();
		session.close();
		return list;
	}
	//删除
	public void delete(Order order) {
		Session session = SessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
		Order o = session.get(Order.class, order.getOrderId());
		for(OrderItem oi:o.getOrderItems()) {
			session.delete(oi);
		}
		session.delete(o);
		
		transaction.commit();
		session.close();
	}
}

②、OrderItemDao 

package com.mwy.dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.mwy.entity.OrderItem;
import com.mwy.util.SessionFactoryUtil;
public class OrderItemDao {
	public OrderItem get(OrderItem orderItem) {
		Session session = SessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		OrderItem oi = session.get(OrderItem.class, orderItem.getOrderItemId());
		transaction.commit();
		session.close();
		return oi;
	}
}
5、测试 
package com.mwy.dao;
import java.util.List;
import org.junit.Test;
import com.mwy.entity.Order;
public class OrderDaoTest {
	private OrderDao orderDao = new OrderDao();
	
	@Test
	public void testGet() {
		Order order = new Order();
		order.setOrderId(7);
//		order.setInitOrderItems(1);
		Order o = this.orderDao.get(order);
		System.out.println(o.getOrderNo());
//		System.out.println(o.getOrderItems());
	}
	
	@Test
	public void testList() {
		List list = this.orderDao.list();
		for (Order o : list) {
			System.out.println(o.getOrderNo());
//			System.out.println(o.getOrderItems().size());
		}
	}
	
	@Test
	public void testDelete() {
		Order order = new Order();
		order.setOrderId(6);
		this.orderDao.delete(order);
	}
}
二、懒加载 1、测试

能查询到为7的订单

public void testGet() {
		Order order = new Order();
		order.setOrderId(7);
		Order o = this.orderDao.get(order);
		System.out.println(o.getOrderNo());
	}

还没写完!!!!!!!!!!!!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4660979.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存