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

Java代码如下:

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.JoinColumn;
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  
    @JoinColumn(name="customer_id")  
	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;
	}
}

package com.lwp.hibernate.Model;

import java.io.Serializable;
import javax.persistence.Column;  
import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;  
import javax.persistence.Id;  
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;

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

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);
		save(order1);
		save(order2);
		save(customer);
	}

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

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 ORDERS (ORDER_NUMBER) values (?)
Hibernate: insert into ORDERS (ORDER_NUMBER) values (?)
Hibernate: insert into Customer (NAME) values (?)
Hibernate: update ORDERS set customer_id=? where ID=?
Hibernate: update ORDERS set customer_id=? where ID=?

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