在程序中一张订单将对应多个产品,多个操作者;
在统计所有订单的时候使用左连接会报错,大家请帮我看看?
Java代码
public class OrderInfo implements Serializable{
/**
*
*/
private Long orderId; //订单编号
private String erpCode; //订单编号
private Integer status; //订单的状态
private Set<Product> product; //产品列表
private Set<Customer> customer; //终端、经销商、收货人
//private Accountant accountant; //财务
private Integer closing;//结算方式
private Integer invoiceSend; //发票寄送方式
private Integer invoiceYN ; //是否开发票
private Invoice invoice; //开票信息·
/**
* @hibernate.id generator-class="hilo" column="orderId"
* @hibernate.meta attribute="field-description" value="标识"
*/
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public Set<Product> getProduct() {
return product;
}
public void setProduct(Set<Product> product) {
Set<Product> setPro = new HashSet<Product>();
if(product!=null){
Iterator<Product> it =product.iterator() ;
while(it.hasNext()){
Product pr = it.next();
if(pr.getPrdName()!=null){
String pname = pr.getPrdName().trim();
if(pname.length()!=0){
setPro.add(pr);
}
}
}
this.product = setPro;
}else{
this.product = product;
}
}
/**
* @hibernate.set
* @hibernate.collection-key column="orderId"
* @hibernate.collection-one-to-many class="com.ccdup.order.entity.Customer"
*/
public Set<Customer> getCustomer() {
return customer;
}
public void setCustomer(Set<Customer> customer) {
Set<Customer> setCustomer = new HashSet<Customer>();
if(customer!=null){
Iterator<Customer> it =customer.iterator() ;
while(it.hasNext()){
Customer cu = it.next();
//if(cu!=null&& cu.getContactName()!=null){
String cnamec = cu.getContactName()==null?"":cu.getContactName().trim();
String phone = cu.getPhone()==null?"":cu.getPhone().trim();
String uname = cu.getUnitName()==null?"":cu.getUnitName().trim();
if(!(cnamec.equals("")&&phone.equals("")&&uname.equals(""))){
setCustomer.add(cu);
}
// }
}
this.customer = setCustomer;
}else{
this.customer = customer;
}
} }
下面是查询时用到的HSQL语句,在查询中用到“left join fetch”
Java代码 public List<OrderInfo> findAllOrderInfo() {
//Query query = HibernateUtil.getCurrentSession().createQuery("from OrderInfo ord order by ord.orderDate desc,ord.status asc");
Query query = HibernateUtil.getCurrentSession().createQuery("from OrderInfo ord left join fetch ord.product left join fetch ord.customer left join fetch ord.logistics left join fetch ord.invoice order by ord.orderDate desc,ord.status asc"); List<OrderInfo> list = query.list();
return list;
}
或者在配置文件中启用“fetch="join"”,都会爆下面的错误
Java代码
org.hibernate.PropertyAccessException: Exception occurred inside setter of com.ccdup.order.entity.OrderInfo.product
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:89)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:360)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:221)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3594)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
at org.hibernate.loader.Loader.doQuery(Loader.java:752)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.ccdup.order.dao.OrderInfoDao.findAllOrderInfo(OrderInfoDao.java:25)
at com.ccdup.test.TestOf.getAall(TestOf.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:66)
... 37 more
Caused by: org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:363)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
at com.ccdup.order.entity.OrderInfo.setProduct(OrderInfo.java:173)
... 42 more
在统计所有订单的时候使用左连接会报错,大家请帮我看看?
Java代码
public class OrderInfo implements Serializable{
/**
*
*/
private Long orderId; //订单编号
private String erpCode; //订单编号
private Integer status; //订单的状态
private Set<Product> product; //产品列表
private Set<Customer> customer; //终端、经销商、收货人
//private Accountant accountant; //财务
private Integer closing;//结算方式
private Integer invoiceSend; //发票寄送方式
private Integer invoiceYN ; //是否开发票
private Invoice invoice; //开票信息·
/**
* @hibernate.id generator-class="hilo" column="orderId"
* @hibernate.meta attribute="field-description" value="标识"
*/
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public Set<Product> getProduct() {
return product;
}
public void setProduct(Set<Product> product) {
Set<Product> setPro = new HashSet<Product>();
if(product!=null){
Iterator<Product> it =product.iterator() ;
while(it.hasNext()){
Product pr = it.next();
if(pr.getPrdName()!=null){
String pname = pr.getPrdName().trim();
if(pname.length()!=0){
setPro.add(pr);
}
}
}
this.product = setPro;
}else{
this.product = product;
}
}
/**
* @hibernate.set
* @hibernate.collection-key column="orderId"
* @hibernate.collection-one-to-many class="com.ccdup.order.entity.Customer"
*/
public Set<Customer> getCustomer() {
return customer;
}
public void setCustomer(Set<Customer> customer) {
Set<Customer> setCustomer = new HashSet<Customer>();
if(customer!=null){
Iterator<Customer> it =customer.iterator() ;
while(it.hasNext()){
Customer cu = it.next();
//if(cu!=null&& cu.getContactName()!=null){
String cnamec = cu.getContactName()==null?"":cu.getContactName().trim();
String phone = cu.getPhone()==null?"":cu.getPhone().trim();
String uname = cu.getUnitName()==null?"":cu.getUnitName().trim();
if(!(cnamec.equals("")&&phone.equals("")&&uname.equals(""))){
setCustomer.add(cu);
}
// }
}
this.customer = setCustomer;
}else{
this.customer = customer;
}
} }
下面是查询时用到的HSQL语句,在查询中用到“left join fetch”
Java代码 public List<OrderInfo> findAllOrderInfo() {
//Query query = HibernateUtil.getCurrentSession().createQuery("from OrderInfo ord order by ord.orderDate desc,ord.status asc");
Query query = HibernateUtil.getCurrentSession().createQuery("from OrderInfo ord left join fetch ord.product left join fetch ord.customer left join fetch ord.logistics left join fetch ord.invoice order by ord.orderDate desc,ord.status asc"); List<OrderInfo> list = query.list();
return list;
}
或者在配置文件中启用“fetch="join"”,都会爆下面的错误
Java代码
org.hibernate.PropertyAccessException: Exception occurred inside setter of com.ccdup.order.entity.OrderInfo.product
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:89)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:360)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:221)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3594)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
at org.hibernate.loader.Loader.doQuery(Loader.java:752)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.ccdup.order.dao.OrderInfoDao.findAllOrderInfo(OrderInfoDao.java:25)
at com.ccdup.test.TestOf.getAall(TestOf.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:66)
... 37 more
Caused by: org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:363)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
at com.ccdup.order.entity.OrderInfo.setProduct(OrderInfo.java:173)
... 42 more
ResultSet sets = statments.executeQuery(“sql语句”);
返回结果集
while (sets.net())
{
sets.getString(1),"获得"sql语句中第一个字段的值
sets.getString(2),"获得"sql语句中第二个字段的值}......
ResultSet sets = statments.executeQuery(“sql语句”);
返回结果集
while (sets.net())
{
sets.getString(1),"获得"sql语句中第一个字段的值
sets.getString(2),"获得"sql语句中第二个字段的值}......
+ "' order by R.type desc,R.code ";我是这么用的 没有问题 可以直接用