hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="Customer.hbm.xml" />
<mapping resource="Order.hbm.xml" />
</session-factory>
</hibernate-configuration>
Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.messi.model.Customer" table="_customer">
<id name="id" type="int">
<column name="id" />
<generator class="increment">
</generator>
</id>
<property name="name" type="string">
<column name="name" length="20" />
</property>
<set name="orders" cascade="save-update" inverse="true">
<key column="customer_id"></key>
<one-to-many class="com.messi.model.Order" />
</set>
</class>
</hibernate-mapping>
Customer.java
package com.messi.model;
import java.util.Set;
public class Customer {
private int id;
private String name;
private Set<Order> orders;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
Order.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.messi.model.Order" table="_order">
<id name="id" type="int">
<column name="id" />
<generator class="increment" />
</id>
<property name="orderNumber" type="string">
<column name="order_Number" length="20" />
</property>
<many-to-one name="customer" class="com.messi.model.Customer"
column="customer_id"></many-to-one>
</class>
</hibernate-mapping>
Order.java
package com.messi.model;
public class Order {
private int id;
private String orderNumber;
private Customer customer;
public int getId() {
return id;
}
public void setId(int 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;
}
}
Test.java
package com.messi.test;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.messi.model.Customer;
import com.messi.model.Order;
@SuppressWarnings("deprecation")
public class Test {
private static SessionFactory sessionFactory;
static {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static void main(String[] args) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Order order1 = new Order();
order1.setOrderNumber("order1");
Order order2 = new Order();
order2.setOrderNumber("order1");
Order order3 = new Order();
order3.setOrderNumber("order1");
Customer customer = new Customer();
customer.setName("zhangsan");
customer.setOrders(new HashSet<Order>());
customer.getOrders().add(order1);
customer.getOrders().add(order2);
customer.getOrders().add(order3);
order1.setCustomer(customer);
order2.setCustomer(customer);
order3.setCustomer(customer);
session.save(customer);
tx.commit();
} catch (Exception e) {
if (null != tx) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
}