This is the code I run.
jdk 1.5
The latest Hibernate viersion downloaded last week
My sql 5.0 without connection pool.
Code:
public static void loadData(String id,String id2) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
// Customer customer1 = (Customer) session.get(Customer.class, id);// System.out.println("customer1 = " + customer1);
CustomerOrder order = (CustomerOrder)session.get(CustomerOrder.class,id2) ;
System.out.println("order = " + order);
System.out.println("order.getCustomer() = " + order.getCustomer());
// System.out.println("order3.getCustomer() .getName() = " + order3.getCustomer().getName());
session.getTransaction().commit();
}
If I load a Customer class befor load the order, following code runs ok,
but if i comment the code "Customer customer1 = (Customer) session.get(Customer.class, id);" there will throw a exception
Code:
Hibernate: select customeror0_.id as id1_0_, customeror0_.orderNumber as orderNum2_1_0_, customeror0_.customer as customer1_0_ from CustomerOrder customeror0_ where customeror0_.id=?
Exception in thread "main" org.hibernate.HibernateException: Javassist Enhancement failed: com.yishi2009.learn2.Customer
Code:
Caused by: java.lang.InstantiationException: com.yishi2009.learn2.Customer_$$_javassist_1
at java.lang.Class.newInstance0(Class.java:335)
at java.lang.Class.newInstance(Class.java:303)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:139)
... 39 more
code for the Customer , and order class are as following
Code:
package com.yishi2009.learn2;
/**
* Created by IntelliJ IDEA.
* User: fayan li
* Date: 2009-1-17
* Time: 22:22:57
* To change this template use File | Settings | File Templates.
*/
public class Customer {
private String id;
private String name;
private Customer() {
}
public Customer(String str) {
this.name = str;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
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
package="com.yishi2009.learn2">
<class name="Customer">
<id name="id" >
<generator class="uuid"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
Code:
package com.yishi2009.learn2;
/**
* Created by IntelliJ IDEA.
* User: fayan li
* Date: 2009-1-17
* Time: 22:23:43
* To change this template use File | Settings | File Templates.
*/
public class CustomerOrder {
private String id;
private String orderNumber;
private Customer customer;
private CustomerOrder() {
}
public CustomerOrder(String s, Customer customer) {
this.customer = customer;
this.orderNumber = s;
}
public String getId() {
return id;
}
public void setId(String 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;
}
}
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
package="com.yishi2009.learn2">
<class name="com.yishi2009.learn2.CustomerOrder">
<id name="id" >
<generator class="uuid"/>
</id>
<property name="orderNumber"/>
<many-to-one name="customer" not-null="true"/>
</class>
</hibernate-mapping>