我正在尝试从数据库中获取数据。方法insert()
正在调用fetch()
。
在insert()
中,我在插入数据库billNo
之后获得(Long) data.get("billNo")
获得了帐单号。
在System.out.println("no is "+(Long) data.get("billNo"));
中,我正在打印显示正确值的值。
public void insert(){
Map data = dao.insertBill(bd);
String ss = (String) data.get("status");
if (ss.equalsIgnoreCase("success")) {
List<Object[]> aa = new ArrayList<Object[]>();
System.out.println("no is " + (Long) data.get("billNo"));//op 56
aa = dao.fetch((Long) data.get("billNo"), lc.getClient().getId());
System.out.println(" aa length is " + aa.size());//0
Object[] bdAndSum = aa.get(0);//line 122 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
.....
}
}
获取方法
public List<Object[]> fetch(Long id, long cid) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
List<Object[]> obj = null;
System.out.println("id " + id + " cid " + cid);
try {
String hql = "select distinct bd,sum(bpds.amount) from BillDetails as bd "
+ "left join fetch bd.customerDetails as cd "
+ "left join fetch bd.billProductSet as bpd "
+ "left join fetch bpd.product as pd "
+ "left join fetch bd.billPaidDetails as bpds "
+ "where bd.billNo=:id "
+ "and bd.client.id=:cid "
+ "group by bpd.id";
Query query = session.createQuery(hql);
query.setParameter("id", id);
query.setParameter("cid", cid);
System.out.println("id " + id + " cid " + cid);//id 56 cid 1
obj = query.list();
System.out.println("id " + id + " cid " + cid);//id 56 cid 1
System.out.println("Printing size of obj ");
System.out.println(" " + obj.size());//0
} catch (HibernateException e) {
if (tx != null) {
e.printStackTrace();
}
} finally {
session.close();
}
return obj;
}
我的问题是dao.fetch((Long) data.get("billNo"), lc.getClient().getId());
没有从数据库中获取数据
并给予例外
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
java.util.ArrayList.rangeCheck(ArrayList.java:638)
java.util.ArrayList.get(ArrayList.java:414)
iland.bill.BillNewAction.insert(BillNewAction.java:122)
但如果我打电话给dao.fetch(56, lc.getClient().getId());
它从数据库中获取数据。
如何解决此问题。
编辑: 服务器控制台数据
no is 82
id 82 cid 1
id 82 cid 1
Hibernate:
/* select
distinct bd,
sum(bpds.amount)
from
BillDetails as bd
left join
fetch bd.customerDetails as cd
left join
fetch bd.billProductSet as bpd
left join
fetch bpd.product as pd
left join
fetch bd.billPaidDetails as bpds
where
bd.billNo=:id
and bd.client.id=:cid
group by
bpd.id */ select
distinct billdetail0_.bill_no as col_0_0_,
sum(billpaidde4_.amount) as col_1_0_,
customerde1_.id as id10_1_,
billproduc2_.product_id as product1_15_2_,
billproduc2_.bill_no as bill2_15_2_,
product3_.id as id4_3_,
billpaidde4_.id as id8_4_,
billdetail0_.bill_no as bill1_0_0_,
customerde1_.id as id10_1_,
billproduc2_.product_id as product1_15_2_,
billproduc2_.bill_no as bill2_15_2_,
product3_.id as id4_3_,
billpaidde4_.id as id8_4_,
billdetail0_.customer_id as customer2_0_0_,
billdetail0_.cid as cid0_0_,
billdetail0_.sub_total as sub4_0_0_,
billdetail0_.vat as vat0_0_,
billdetail0_.total as total0_0_,
billdetail0_.invoice_no as invoice7_0_0_,
billdetail0_.invoice_dt as invoice8_0_0_,
billdetail0_.status as status0_0_,
billdetail0_.description as descrip10_0_0_,
billdetail0_.add_date as add11_0_0_,
customerde1_.cid as cid10_1_,
customerde1_.name as name10_1_,
customerde1_.address as address10_1_,
customerde1_.city as city10_1_,
customerde1_.state as state10_1_,
customerde1_.country as country10_1_,
customerde1_.mobno as mobno10_1_,
customerde1_.type as type10_1_,
customerde1_.dob as dob10_1_,
customerde1_.anniversery as anniver11_10_1_,
customerde1_.age_range as age12_10_1_,
customerde1_.add_date as add13_10_1_,
customerde1_.status as status10_1_,
billproduc2_.cid as cid15_2_,
billproduc2_.qty as qty15_2_,
billproduc2_.unit_price as unit5_15_2_,
billproduc2_.selling_price as selling6_15_2_,
billproduc2_.discount_percent as discount7_15_2_,
billproduc2_.description as descript8_15_2_,
billproduc2_.bill_no as bill2_0_0__,
billproduc2_.product_id as product1_0__,
billproduc2_.bill_no as bill2_0__,
product3_.cid as cid4_3_,
product3_.barcode as barcode4_3_,
product3_.name as name4_3_,
product3_.description as descript5_4_3_,
product3_.quntity as quntity4_3_,
product3_.unit_price as unit7_4_3_,
product3_.selling_price as selling8_4_3_,
product3_.discount_percent as discount9_4_3_,
product3_.add_date as add10_4_3_,
product3_.status as status4_3_,
billpaidde4_.cid as cid8_4_,
billpaidde4_.bill_no as bill3_8_4_,
billpaidde4_.amount as amount8_4_,
billpaidde4_.payment_mode as payment5_8_4_,
billpaidde4_.dt as dt8_4_,
billpaidde4_.adddate as adddate8_4_,
billpaidde4_.bill_no as bill3_0_1__,
billpaidde4_.id as id1__
from
xrcwrn_retail_shop.bill_details billdetail0_
left outer join
xrcwrn_retail_shop.customer_details customerde1_
on billdetail0_.customer_id=customerde1_.id
left outer join
xrcwrn_retail_shop.bill_product billproduc2_
on billdetail0_.bill_no=billproduc2_.bill_no
left outer join
xrcwrn_retail_shop.product product3_
on billproduc2_.product_id=product3_.id
left outer join
xrcwrn_retail_shop.bill_paid_details billpaidde4_
on billdetail0_.bill_no=billpaidde4_.bill_no
where
billdetail0_.bill_no=?
and billdetail0_.cid=?
group by
billproduc2_.product_id,
Sep 21, 2014 3:09:28 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
SEVERE: Exception occurred during processing request: Index: 0, Size: 0
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at iland.bill.BillNewAction.insert(BillNewAction.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
billproduc2_.bill_no
id 82 cid 1
Printing size of obj
0
aa length is 0
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
Bill Detail POJO
public class BillDetails implements java.io.Serializable {
private Long billNo;
private Client client;
//getter and setter
}
public class Client implements java.io.Serializable {
private Long id;
//getter and setter
}
编辑:我使用sql插入记录并从此处返回最新插入的ID,这没关系。 我发现hibernate无法使用插入的id获取最新记录。
我在多个地方遇到同样的问题,我在同样的情况下使用。
我的数据库插入代码是:http://pastebin.com/LfP6Hfwc
答案 0 :(得分:1)
我想你可能没有用insertBill()
方法刷新你的交易,比如
session.flush();
刷新会话强制Hibernate将Session的内存状态与数据库同步(即将更改写入数据库)
我想你正在插入账单并在单笔交易中获取相同的账单。因此,当您尝试获取帐单时,事务尚未提交,因此获取将无法获取预期的帐单记录。 session.flush()
将适用于您。
我希望它可以解决你的问题。
答案 1 :(得分:1)
我认为HibernateTransactionManager
是您的解决方案
此事务管理器适用于使用单个Hibernate SessionFactory进行事务数据访问的应用程序,但它也支持事务中的直接DataSource访问(即使用相同DataSource的纯JDBC代码)。
基本上,在某些时候,您必须使用setDataSource(DataSource dataSource)
设置此实例应管理事务的JDBC DataSource。 DataSource应该与Hibernate SessionFactory使用的数据源匹配:例如,您可以为两者指定相同的JNDI DataSource。
DataSource source = ...;
hibernateTransactionManager.setDataSource(dataSource);
// [...]
Connection con = source.getConnection();
// Do your inserts and stuff
然后,使用hibernateTransactionManager
获取您的对象
public List<Object[]> fetch(Long id, long cid)
{
Session session = hibernateTransactionManager.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
// [...]
答案 2 :(得分:0)
HQL子句组不会automatically expand all properties of a given entity:
Hibernate目前还没有扩展 分组实体,因此如果所有属性都不能用cat编写分组 猫是非聚集的。您必须列出所有非聚合属性 明确。
所以查询:
select distinct bd, sum(bpds.amount)
from BillDetails as bd
...
group by bpd.id
应更改为:
select distinct bd.id, sum(bpds.amount)
from BillDetails as bd
...
group by bpd.id
这样您就可以获取实体ID和金额总和。要获取实体,您必须在此查询后加载它。
答案 3 :(得分:0)
不使用弹簧,我们可以使用doWork()和doReturningWork。 这可以用于简单的Jdbc和hibernate