小弟使用hibernate来进行ORM映射,
其中要映射两个对象的1对多关系,其中customer代码为
package model;import java.util.HashSet;
import java.util.Set;/**
*
* @author lucafuji
*/
public class Customer extends BaseModel{ private String name;
private String phone;
private String address;
private String email;
private String state;
private int debt;
private String password;
private Set<Order> orders=new HashSet();; /**
* @return the name
*/
public String getName() {
return name;
} /**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
} /**
* @return the phone
*/
public String getPhone() {
return phone;
} /**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
} /**
* @return the address
*/
public String getAddress() {
return address;
} /**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
} /**
* @return the email
*/
public String getEmail() {
return email;
} /**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
} /**
* @return the state
*/
public String getState() {
return state;
} /**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
} /**
* @return the debt
*/
public int getDebt() {
return debt;
} /**
* @param debt the debt to set
*/
public void setDebt(int debt) {
this.debt = debt;
} /**
* @return the password
*/
public String getPassword() {
return password;
} /**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
} /**
* @return the orders
*/
public Set<Order> getOrders() {
return orders;
} /**
* @param orders the orders to set
*/
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public void clone(Customer c)
{
c.setId(getId());
c.setAddress(address);
c.setDebt(debt);
c.setEmail(email);
c.setName(name);
c.setOrders(orders);
c.setPassword(password);
c.setPhone(phone);
c.setState(state);
}}Order代码为:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package model;import java.sql.Date;/**
*
* @author Chenkovsky
*/
public class Order extends BaseModel{ private Date startDate;
private Date endDate;
private String state;
private String type;
private int money;
//TODO 可以添加送货员的信息
private Customer customer;
private String name;
private String phone;
private String address;
private String email;
/**
* Get the value of money
*
* @return the value of money
*/
public int getMoney() {
return money;
} /**
* Set the value of money
*
* @param money new value of money
*/
public void setMoney(int money) {
this.money = money;
}
/**
* Get the value of endDate
*
* @return the value of endDate
*/
public Date getEndDate() {
return endDate;
} /**
* Set the value of endDate
*
* @param endDate new value of endDate
*/
public void setEndDate(Date endDate) {
this.endDate = endDate;
} /**
* Get the value of startDate
*
* @return the value of startDate
*/
public Date getStartDate() {
return startDate;
} /**
* Set the value of startDate
*
* @param startDate new value of startDate
*/
public void setStartDate(Date startDate) {
this.startDate = startDate;
} /**
* @return the name
*/
public String getName() {
return name;
} /**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
} /**
* @return the phone
*/
public String getPhone() {
return phone;
} /**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
} /**
* @return the address
*/
public String getAddress() {
return address;
} /**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
} /**
* @return the email
*/
public String getEmail() {
return email;
} /**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
} /**
* @return the state
*/
public String getState() {
return state;
} /**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
} /**
* @return the type
*/
public String getType() {
return type;
} /**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
} /**
* @return the customer
*/
public Customer getCustomer() {
return customer;
} /**
* @param customer the customer to set
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}}
customer的hbm。xml为
<hibernate-mapping package="model"> <class name="Customer" table="customer">
<id name="id" column="ID" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="name" column="name"/>
<property name="phone" column="phone" />
<property name="address" column="address"/>
<property name="email" column="email" />
<property name="password" column="password" />
<property name="state" column="state" />
<property name="debt" column="debt" />
<set name="orders" table="order" inverse="true" cascade="all" >
<key column="customerId"/>
<one-to-many class="Order"/>
</set>
</class></hibernate-mapping>
Order。hbm。xml为
<hibernate-mapping package="model"> <class name="Order" table="Order">
<id name="id" column="ID" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="startDate" column="startDate"/>
<property name="endDate" column="endDate" />
<property name="state" column="state" /> <property name="type" column="goodType"/> <property name="money" column="money" /> <property name="name" column="receiverName" />
<property name="phone" column="receiverPhone" />
<property name="address" column="receiverAddress" />
<property name="email" column="receiverEmail" /> <many-to-one name="customer" class="Customer" column="customerId" cascade="all"/>
</class>
</hibernate-mapping>
,然后执行报错
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [model.Customer.orders#1]
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.loader.Loader.loadCollection(Loader.java:2001)
org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:63)
org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:109)
org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:146)
action.orderAction.listOrder(orderAction.java:89)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)弱问一下是什么问题。。?貌似我判断这个是否是null时并不是false啊,真是诡异
其中要映射两个对象的1对多关系,其中customer代码为
package model;import java.util.HashSet;
import java.util.Set;/**
*
* @author lucafuji
*/
public class Customer extends BaseModel{ private String name;
private String phone;
private String address;
private String email;
private String state;
private int debt;
private String password;
private Set<Order> orders=new HashSet();; /**
* @return the name
*/
public String getName() {
return name;
} /**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
} /**
* @return the phone
*/
public String getPhone() {
return phone;
} /**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
} /**
* @return the address
*/
public String getAddress() {
return address;
} /**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
} /**
* @return the email
*/
public String getEmail() {
return email;
} /**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
} /**
* @return the state
*/
public String getState() {
return state;
} /**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
} /**
* @return the debt
*/
public int getDebt() {
return debt;
} /**
* @param debt the debt to set
*/
public void setDebt(int debt) {
this.debt = debt;
} /**
* @return the password
*/
public String getPassword() {
return password;
} /**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
} /**
* @return the orders
*/
public Set<Order> getOrders() {
return orders;
} /**
* @param orders the orders to set
*/
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public void clone(Customer c)
{
c.setId(getId());
c.setAddress(address);
c.setDebt(debt);
c.setEmail(email);
c.setName(name);
c.setOrders(orders);
c.setPassword(password);
c.setPhone(phone);
c.setState(state);
}}Order代码为:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package model;import java.sql.Date;/**
*
* @author Chenkovsky
*/
public class Order extends BaseModel{ private Date startDate;
private Date endDate;
private String state;
private String type;
private int money;
//TODO 可以添加送货员的信息
private Customer customer;
private String name;
private String phone;
private String address;
private String email;
/**
* Get the value of money
*
* @return the value of money
*/
public int getMoney() {
return money;
} /**
* Set the value of money
*
* @param money new value of money
*/
public void setMoney(int money) {
this.money = money;
}
/**
* Get the value of endDate
*
* @return the value of endDate
*/
public Date getEndDate() {
return endDate;
} /**
* Set the value of endDate
*
* @param endDate new value of endDate
*/
public void setEndDate(Date endDate) {
this.endDate = endDate;
} /**
* Get the value of startDate
*
* @return the value of startDate
*/
public Date getStartDate() {
return startDate;
} /**
* Set the value of startDate
*
* @param startDate new value of startDate
*/
public void setStartDate(Date startDate) {
this.startDate = startDate;
} /**
* @return the name
*/
public String getName() {
return name;
} /**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
} /**
* @return the phone
*/
public String getPhone() {
return phone;
} /**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
} /**
* @return the address
*/
public String getAddress() {
return address;
} /**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
} /**
* @return the email
*/
public String getEmail() {
return email;
} /**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
} /**
* @return the state
*/
public String getState() {
return state;
} /**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
} /**
* @return the type
*/
public String getType() {
return type;
} /**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
} /**
* @return the customer
*/
public Customer getCustomer() {
return customer;
} /**
* @param customer the customer to set
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}}
customer的hbm。xml为
<hibernate-mapping package="model"> <class name="Customer" table="customer">
<id name="id" column="ID" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="name" column="name"/>
<property name="phone" column="phone" />
<property name="address" column="address"/>
<property name="email" column="email" />
<property name="password" column="password" />
<property name="state" column="state" />
<property name="debt" column="debt" />
<set name="orders" table="order" inverse="true" cascade="all" >
<key column="customerId"/>
<one-to-many class="Order"/>
</set>
</class></hibernate-mapping>
Order。hbm。xml为
<hibernate-mapping package="model"> <class name="Order" table="Order">
<id name="id" column="ID" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="startDate" column="startDate"/>
<property name="endDate" column="endDate" />
<property name="state" column="state" /> <property name="type" column="goodType"/> <property name="money" column="money" /> <property name="name" column="receiverName" />
<property name="phone" column="receiverPhone" />
<property name="address" column="receiverAddress" />
<property name="email" column="receiverEmail" /> <many-to-one name="customer" class="Customer" column="customerId" cascade="all"/>
</class>
</hibernate-mapping>
,然后执行报错
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [model.Customer.orders#1]
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.loader.Loader.loadCollection(Loader.java:2001)
org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:63)
org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:109)
org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:146)
action.orderAction.listOrder(orderAction.java:89)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)弱问一下是什么问题。。?貌似我判断这个是否是null时并不是false啊,真是诡异
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货