//得到Business最大的id
@Override
@Transactional(propagation=Propagation.REQUIRED)
public long findBusinessIdMAX() throws WuliuguanliException {
Session session=null;
session=factory.getCurrentSession();
String hql="select max(b.id) from Business b";
Query query=session.createQuery(hql);
Long count=(Long) query.uniqueResult();
return count;
}
I have a database where the id is of type int, but after the hibernate query return value is a Long, how should I do it will not report the following error:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
at com.wuliuguanli.dao.impl.WuliuguanliDaoImpl.findBusinessIdMAX (WuliuguanliDaoImpl.java: 138)
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: 597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection (AopUtils.java: 307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint (ReflectiveMethodInvocation.java: 182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java: 149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke (TransactionInterceptor.java: 106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java: 171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke (JdkDynamicAopProxy.java: 204)
at $ Proxy9.findBusinessIdMAX (Unknown Source)
at test.Testdao.test7 (Testdao.java: 29)
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: 597)
at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall (FrameworkMethod.java: 44)
at org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java: 15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java: 41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java: 20)
at org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java: 28)
at org.junit.internal.runners.statements.RunAfters.evaluate (RunAfters.java: 31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 46)
at org.junit.runners.ParentRunner.runChildren (ParentRunner.java: 180)
at org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java: 41)
at org.junit.runners.ParentRunner $ 1.evaluate (ParentRunner.java: 173)
at org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java: 28)
at org.junit.internal.runners.statements.RunAfters.evaluate (RunAfters.java: 31)
at org.junit.runners.ParentRunner.run (ParentRunner.java: 220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUnit4TestReference.java: 46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run (TestExecution.java: 38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java: 467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java: 683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.java: 390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java: 197)
------ Solution ------------------------------------ --------
int count = ((Long) query.uniqueResult ()). intValue ();
------ Solution ---- ----------------------------------------
Long count = ( Long) query.uniqueResult ();
changed
Long count = query.uniqueResult ();
------ Solution ------------------------ --------------------
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
This sentence illustrates, is converted from int Long mistake, you do the test, print int value facie, if there is value, indicating that casts a problem
----- - Solution --------------------------------------------
the change the value of one type is OK, ah, or else change the int, or else change the long
------ Solution -------------------- ------------------------
is a type conversion error, a few days ago I have encountered such a mistake, because the database in oracle 10G ; switch to 11G and run on an IBM minicomputer they reported such errors and then adjust what types like
------ Solution ---------- ----------------------------------
3 floor of the method should be able to
--- --- For reference only ---------------------------------------
------ For reference only ---------------------------------- -----
Thank you, problem solved, I was such a change:
//得到Business最大的id
@Override
@Transactional(propagation=Propagation.REQUIRED)
public int findBusinessIdMAX() throws WuliuguanliException {
Session session=null;
session=factory.getCurrentSession();
String hql="select max(id) from Business";
Query query=session.createQuery(hql);
Object count=query.uniqueResult();
Integer i = Integer.parseInt(count.toString());
return i;
}
Thank you for your opinion, there are points .....
so given that it need an authentication, how will the code will be?
回复删除