- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从数据库中获取数据。 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());
没有从数据库中获取数据并且并给予异常(exception)
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)
账单明细 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
最佳答案
我认为HibernateTransactionManager
是你的解决方案
This transaction manager is appropriate for applications that use a single Hibernate SessionFactory for transactional data access, but it also supports direct DataSource access within a transaction (i.e. plain JDBC code working with the same DataSource).
基本上,在某些时候,您必须使用 setDataSource(DataSource dataSource)
注册您的数据源。
Set the JDBC DataSource that this instance should manage transactions for. The DataSource should match the one used by the Hibernate SessionFactory: for example, you could specify the same JNDI DataSource for both.
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();
// [...]
关于java - Hibernate 没有从数据库中获取最新插入的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25931634/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!