hibernate annotations OneToMany与ManyToOne 分析 以及延迟加

在EJB3.0 规范中 多对一与一对多的双向关系, 多对一(就是@ManyToOne注解的这端,是多端哦 不要搞混了)这端总是双向关联端的主题(owner)端, 而一对多端的关联注解为 @OneToMany(mappedBy=" " )其值是:多对一端的属性

demo:

被动方:其实也就是一方 或者说(OneToMany方)

@Entity
public class Customer extends AbstractEntity {

private String name;

@OneToMany(mappedBy="customer",cascade=CascadeType.ALL)
private Set<Order> orders;

public void addOrder(Order order){
   if(orders == null){
    orders = new HashSet<Order>();
   }
   orders.add(order);
}

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;
}

}

主动方:1.关系的维护方2.ManyToOne方3.多方

@Entity
@Table(name="orders")
public class Order extends AbstractEntity {

private String name;

@ManyToOne(cascade=CascadeType.ALL)
private Customer   customer;

public Customer getCustomer() {
   return customer;
}

public void setCustomer(Customer customer) {
   this.customer = customer;
}

public String getName() {
   return name;
}

public void setName(String name) {
   this.name = name;
}
}

 

以上是实体

下面是测试用列哦

public void testCRUD() {
   // 第一种情况: 调用的被动方的Dao 绑定主动方关系,但主动方没有绑定被动方
   Customer entity = new Customer();
   entity.setName("customer1");
   Order order = new Order();
   order.setName("order1");
   entity.addOrder(order);
   entity = customerDao.create(entity); // 这种情况下 orders.customer_id == null
   //控制台的信息
   //Hibernate: insert into Customer (name, id) values (?, ?)
   //这里的customer_id 为null
   //Hibernate: insert into orders (name, customer_id, id) values (?, ?, ?)
   System.out.println("entity id = " + entity.getId());
   System.out.println("order id = "+ order.getId());
   System.out.println("1111********************** over");

// 第二种情况: 调用的被动方的Dao 绑定主动方关系,并且主动方也绑定被动方
   entity = new Customer();
   entity.setName("customer2");
   order = new Order();
   order.setName("order2");
   entity.addOrder(order);
   order.setCustomer(entity); //这里进行双向关联
   entity = customerDao.create(entity);
   //
   //Hibernate: insert into Customer (name, id) values (?, ?)
   //这里的customer_id 有值哦
   //Hibernate: insert into orders (name, customer_id, id) values (?, ?, ?)
   System.out.println("entity id = " + entity.getId());
   System.out.println("order id = "+ order.getId());
   System.out.println("2222********************** over");
   // 第三种情况: 调用的主动方的Dao 绑定被动方关系,但是被东方不绑定主动方
   entity = new Customer();
   entity.setName("customer3");
   order = new Order();
   order.setName("order3");
   order.setCustomer(entity); //绑定被动方
   orderDao.create(order);
   //Hibernate: insert into Customer (name, id) values (?, ?)
   //Hibernate: insert into orders (name, customer_id, id) values (?, ?, ?)
   System.out.println("entity id = " + entity.getId());
   System.out.println("order id = "+ order.getId());
   System.out.println("3333********************** over");
  
// 第四种情况: 调用的主动方的Dao 绑定被动方关系,并且被东方也绑定主动方
   entity = new Customer();
   entity.setName("customer4");
   order = new Order();
   order.setName("order4");
   order.setCustomer(entity); //绑定被动方
   orderDao.create(order);
   System.out.println("entity id = " + entity.getId());
   System.out.println("order id = "+ order.getId());
   System.out.println("4444********************** over");
   //Hibernate: insert into Customer (name, id) values (?, ?)
   //Hibernate: insert into orders (name, customer_id, id) values (?, ?, ?)
  
   //总结: 经测验二三四种方法结果都是一样都能持久化到数据库,并且关系也建立好了
   //       也就说只要主动方绑定了被动方关系就维护好了
}

 

 

 

*****************************************做级联删除测试************************************************************

public void testCascade(){
  
   //1. 做个级联删除吧 测试删除主动方 是否删除关联方
   // 这里会级联删除主动方的关联对象,以及该关联对象(被动方)所关联的所有主动方都会被级联删除
   orderDao.delete("order_id_1");
           //Hibernate: delete from orders where id=?
     // Hibernate: delete from orders where id=?
     // Hibernate: delete from orders where id=?
     // Hibernate: delete from orders where id=?
     // Hibernate: delete from orders where id=?
     // Hibernate: delete from Customer where id=?
   assertNull( orderDao.findById("orderDao"));
  
   //2. 做个级联删除吧 测试删除被动方 是否删除关联方
   //删除该被动方,以及所关联的所有主动方(维护关系方or多方)与上面的调用主动方的结果一样
        //Hibernate: delete from orders where id=?
   // Hibernate: delete from orders where id=?
   // Hibernate: delete from orders where id=?
   // Hibernate: delete from orders where id=?
   // Hibernate: delete from orders where id=?
   // Hibernate: delete from Customer where id=?
   customerDao.delete("2");
   assertNull( customerDao.findById("2"));
  
}

****************************************************l延迟加载************************************************************

@ManyToOne Default: FetchType.EAGER   默认是即时抓取   做连接 如果,fetch=FetchType.LAZY不是左联接 在需要One的时候直接select

@OneToMany Default: FetchType.LAZY   默认是延迟抓取   不需要左联接 如果是FetchType.EAGER     即时抓取 就会是左联接查询

如果 order(ManyToOne)设置了 @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
private Customer   customer;   查找Order是直接查找不需要左连接 customer

Hibernate: select order0_.id as id5_0_, order0_.name as name5_0_, order0_.customer_id as customer3_5_0_ from orders order0_ where order0_.id=?

如果 order(ManyToOne)设置了 @ManyToOne(cascade=CascadeType.ALL) //没有设置延迟加载
private Customer   customer;    查找Order是直接查找需要左连接 customer

Hibernate: select order0_.id as id5_1_, order0_.name as name5_1_, order0_.customer_id as customer3_5_1_, customer1_.id as id2_0_, customer1_.name as name2_0_ from orders order0_ left outer join Customer customer1_ on order0_.customer_id=customer1_.id where order0_.id=?

你可能感兴趣的:(DAO,Hibernate)