Hibernate version: 3.2
Name and version of the database you are using:Mysql 5.0.22
src:
DDL
I create a database and insert data like this
Code:
drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB;
create table CUSTOMERS (
ID bigint not null,
NAME varchar(15),
AGE int,
primary key (ID)
);
create table ORDERS (
ID bigint not null,
ORDER_NUMBER varchar(15),
PRICE DECIMAL(10,2),
CUSTOMER_ID bigint,
primary key (ID)
);
alter table ORDERS add index FK8B7256E516B4891C (CUSTOMER_ID), add constraint FK8B7256E516B4891C foreign key (CUSTOMER_ID) references CUSTOMERS (ID);
insert into CUSTOMERS(ID,NAME,AGE) values(1,'Tom',21);
insert into CUSTOMERS(ID,NAME,AGE) values(2,'Mike',24);
insert into CUSTOMERS(ID,NAME,AGE) values(3,'Jack',30);
insert into CUSTOMERS(ID,NAME,AGE) values(4,'Linda',25);
insert into CUSTOMERS(ID,NAME,AGE) values(5,'Tom',25);
insert into ORDERS(ID,ORDER_NUMBER,PRICE,CUSTOMER_ID) values(1,'Tom_Order001',100,1);
insert into ORDERS(ID,ORDER_NUMBER,PRICE,CUSTOMER_ID) values(2,'Tom_Order002',200,1);
insert into ORDERS(ID,ORDER_NUMBER,PRICE,CUSTOMER_ID) values(3,'Tom_Order003',300,1);
insert into ORDERS(ID,ORDER_NUMBER,PRICE,CUSTOMER_ID) values(4,'Mike_Order001',100,2);
insert into ORDERS(ID,ORDER_NUMBER,PRICE,CUSTOMER_ID) values(5,'Jack_Order001',200,3);
insert into ORDERS(ID,ORDER_NUMBER,PRICE,CUSTOMER_ID) values(6,'Linda_Order001',100,4);
insert into ORDERS(ID,ORDER_NUMBER,PRICE,CUSTOMER_ID) values(7,'UnknownOrder',200,null);
Customer.java
Code:
package ergal;
// Generated 2006-8-21 16:07:58 by Hibernate Tools 3.2.0.beta6a
import java.util.HashSet;
import java.util.Set;
/**
* Customer generated by hbm2java
*/
public class Customer implements java.io.Serializable {
// Fields
private Long id;
private String name;
private Integer age;
private Set<Order> orders = new HashSet<Order>(0);
// Constructors
/** default constructor */
public Customer() {
}
/** full constructor */
public Customer(String name, Integer age, Set<Order> orders) {
this.name = name;
this.age = age;
this.orders = orders;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public Set<Order> getOrders() {
return this.orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
Order.java
Code:
package ergal;
// Generated 2006-8-21 16:07:58 by Hibernate Tools 3.2.0.beta6a
import java.math.BigDecimal;
/**
* Order generated by hbm2java
*/
public class Order implements java.io.Serializable {
// Fields
private Long id;
private String orderNumber;
private BigDecimal price;
private Customer customer;
// Constructors
/** default constructor */
public Order() {
}
/** full constructor */
public Order(String orderNumber, BigDecimal price, Customer customer) {
this.orderNumber = orderNumber;
this.price = price;
this.customer = customer;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderNumber() {
return this.orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public BigDecimal getPrice() {
return this.price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
Mapping documents:Customer.hbm.xml
Code:
<?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="ergal.Customer" table="customers" lazy="true">
<id name="id" type="java.lang.Long" column="ID">
<generator class="increment" />
</id>
<property
name="name"
type="java.lang.String"
column="NAME"
length="15"
/>
<property
name="age"
type="java.lang.Integer"
column="AGE"
length="11"
/>
<set
name="orders"
lazy="true"
inverse="true"
cascade="save-update"
>
<key>
<column name="CUSTOMER_ID" />
</key>
<one-to-many class="ergal.Order" />
</set>
</class>
</hibernate-mapping>
Order.hbm.xml
Code:
<?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="ergal.Order"
table="orders">
<id
name="id"
type="java.lang.Long"
column="ID">
<generator class="increment" />
</id>
<property
name="orderNumber"
type="java.lang.String"
column="ORDER_NUMBER"
length="15"
/>
<property
name="price"
type="java.math.BigDecimal"
column="PRICE"
length="10"
/>
<many-to-one
name="customer"
class="ergal.Customer"
not-null="true"
cascade="save-update">
<column name="CUSTOMER_ID" />
</many-to-one>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session session=sessionFactory.openSession();
Transaction tx=null;
try
{
tx=session.beginTransaction();
List result=session.createCriteria(Order.class)
.add(Restrictions.isNull("customer")).list();
for(Iterator it=result.iterator(); it.hasNext();)
{
Order o =(Order)it.next();
System.out.println("Order's number :" + o.getOrderNumber());
System.out.println("Order's price :" + o.getPrice());
}
tx.commit();
}
catch(Exception e)
{
if(tx!=null)
{
tx.rollback();
}
throw e;
}
finally
{
session.close();
}
There are 2 problem nonplus me
1
I use a method to find a Order object that it's CUSTOMER_ID is null and print it
but when I run this application and method there is no result printed out
and no exception threw
2
After I run this application in Ant
the table CUSTOMERS is the same as when I created and inserted data into it
but the table ORDERS is empty that there is no data in it