@ManyToOne注解的这端,是多端
1.在注释@ManyToOne(cascade=CascadeType.REFRESH,optional=true)中将属性optional设置为true,这可以使得即使外键为空时仍可以向表中添加数据。
2.假设Person和Book是一对多的关系,其中Person的成员变量为:
private Integer personId;
private String name;
private Short age;
private List
对应在MySql中表的结构为:Person(personId,name,age),不必有bookList一项,在getBookList上方通过注释: @OneToMany(mappedBy="person",cascade=CascadeType.ALL)
@OrderBy(value="bookId ASC")
与Book关系形成一对多的关系。Book的成员变量是:
private int bookId;
private String title;
private double price;
private Person person;
对应在MySql中表的结构为:Book(bookId,title,price,personId),注意要有Person表的主键personId,这样在插入记录时才不会产生异常。在getPerson上方有注释:
@ManyToOne(cascade=CascadeType.REFRESH,optional=true)
@JoinColumn(name="personId")
与@OneToMany形成呼应。
在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
public void addOrder(Order order){
if(orders == null){
orders = new HashSet
}
orders.add(order);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set
return orders;
}
public void setOrders(Set
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"));
}