gpt4 book ai didi

java - Hibernate 没有从数据库中获取最新插入的记录

转载 作者:行者123 更新时间:2023-11-30 09:02:34 25 4
gpt4 key购买 nike

我正在尝试从数据库中获取数据。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com