应用场景:
在电子购物模型中,一个顾客可以发出多个订单,而每个订单却只能属于一个顾客,现在要建立从订单到顾客的多对一的单向关联。
Customer :顾客类
Order:订单类
该多对一关系在javabean中表现为:
在Order类中有一个customer的顾客属性;在Customer中没有Order的信息,不建立从Customer到Order的关联
该多对一关系在数据库表现为:
在Order对应的表中有Customer对应的行的id作为foreign key
package oneToMany;
import java.io.Serializable;
public class Order implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String orderNumber;
private Customer customer;
public Order() {}
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 oneToMany;
import java.io.Serializable;
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String name;
public Customer() {}
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;
}
}
以下是oracle的数据库模型:
create sequence customers_sequence;
create table customers(
id number(20) primary key,
name varchar2(50)
)
create sequence orders_sequence;
create table orders(
id number(20) primary key,
order_number varchar2(100),
customer_id number(20) references customers(id)
)
下面是hibernate的映射文件:
Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="oneToMany">
<class name="Customer" table="customers">
<id name="id" column="id">
<generator class="sequence">
<param name="sequence">customers_sequence</param>
</generator>
</id>
<property name="name" column="name"/>
</class>
</hibernate-mapping>
Order.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="oneToMany">
<class name="Order" table="orders">
<id name="id" column="id">
<generator class="sequence">
<param name="sequence">orders_sequence</param>
</generator>
</id>
<property name="orderNumber" column="order_number"/>
<many-to-one name="customer"
column="customer_id"
class="Customer"
not-null="true"></many-to-one>
</class>
</hibernate-mapping>
下面是hibernate的session工厂类:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("Error Occurs When Creating SessionFactory ");
e.printStackTrace();
}
}
private HibernateSessionFactory() {}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("Error Occurs When Creating SessionFactory");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
利用hibernate的session来添加一下order和customer对象:
package oneToMany;
import org.hibernate.Session;
import com.thomas.util.HibernateSessionFactory;
public class Test Save{
public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
Customer customer = new Customer();
customer.setName("thomas");
session.save(customer);
Order order1 = new Order();
order1.setOrderNumber("thomas1");
order1.setCustomer(customer);
Order order2 = new Order();
order2.setOrderNumber("thomas2");
order2.setCustomer(customer);
session.save(order1);
session.save(order2);
session.getTransaction().commit();
session.close();
}
}