Hibernate 使用 Annotation 之 OneToMany(双向关联关系)

java代码:

package com.lwp.hibernate.Model;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;  
import javax.persistence.Entity;  
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;  
import javax.persistence.Id;  
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity  
@Table(name="ORDERS")  
public class Order implements Serializable{

	private static final long serialVersionUID = 1L;
	public Order(){}

	public Order(String orderNumber){
		this.orderNumber =orderNumber;
	}
    @Id  
    @GeneratedValue  
    @Column(name="ID")  
	private Long id;
    
    @Column(name="ORDER_NUMBER",length=15)  
	private String orderNumber;
    
    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name="CUSTOMER_ID")
    private Customer customer;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}

	public Customer getCustomer() {
		return customer;
	}

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

package com.lwp.hibernate.Model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;  
import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;  
import javax.persistence.Id;  
import javax.persistence.OneToMany;
import javax.persistence.Table;  

@Entity  
@Table 
public class Customer implements Serializable{
	
	private static final long serialVersionUID = 1L;

	public Customer(){}
	public Customer(String name,Set<Order> orders){
		this.name=name;
		this.orders = orders;
	}

    @Id  
    @GeneratedValue  
    @Column(name="ID")  
	private Long id;
	
    @Column(name="NAME",length=15)  
	private String name;
	
    @OneToMany(mappedBy="customer")
	private Set<Order> orders = new HashSet<Order>();

	public Set<Order> getOrders() {
		return orders;
	}
	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import com.lwp.hibernate.Model.Customer;
import com.lwp.hibernate.Model.Order;

public class CustomerTest {

	public static void main(String[] args) {
		new SchemaExport(new AnnotationConfiguration().configure("/hibernate.cfg.xml")).create(true, false);
		
		Set<Order> orders =  new HashSet<Order>();
		Order order1 = new Order("Tom_Order001");
		Order order2 = new Order("Tom_Order002");
		orders.add(order1);
		orders.add(order2);
		Customer customer = new Customer("Tom",orders);
		order1.setCustomer(customer);
		order2.setCustomer(customer);
		
		//save(customer);
		save(order1);
		save(order2);

		
	}

	public static void save(Object o){
		//Configuration cfg = new Configuration();//xx.hbm.xml形式下的new  
		Configuration cfg = new AnnotationConfiguration(); //Annotation形式下的new 
		SessionFactory sf = cfg.configure().buildSessionFactory();
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(o);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
}


注:这里不用save customer, save order时就会保存customer。因为mappedBy 相当于Inverse=value,既让Many一方来维护。

SQL:

alter table ORDERS drop foreign key FK8B7256E5C1E4F651
drop table if exists Customer
drop table if exists ORDERS
create table Customer (ID bigint not null auto_increment, NAME varchar(15), primary key (ID))
create table ORDERS (ID bigint not null auto_increment, ORDER_NUMBER varchar(15), CUSTOMER_ID bigint, primary key (ID))
alter table ORDERS add index FK8B7256E5C1E4F651 (CUSTOMER_ID), add constraint FK8B7256E5C1E4F651 foreign key (CUSTOMER_ID) references Customer (ID)
Hibernate: insert into Customer (NAME) values (?)
Hibernate: insert into ORDERS (CUSTOMER_ID, ORDER_NUMBER) values (?, ?)
Hibernate: insert into ORDERS (CUSTOMER_ID, ORDER_NUMBER) values (?, ?)
Hibernate: update Customer set NAME=? where ID=?

你可能感兴趣的:(Hibernate,String,session,table,insert,import)