hibernate空格导致的错误!
下面是我对hibernate中查询进行的测试....
package cn.lwk.dao; import java.util.List; import org.hibernate.classic.Session; import cn.itcast.utils.HibernateUtils; public class EntityDao extends HibernateUtils{ public <T> List<T> find(Class<T> t){ Session session = sessionFactory.openSession(); List userList = session.createQuery("from"+t.getName()).list(); session.close(); return userList; } }
下面是测试数据
package cn.lwk.test; import static org.junit.Assert.*; import java.util.List; import org.junit.Test; import cn.lwk.dao.EntityDao; import cn.lwk.domain.User; public class EntityDaoTest { @Test public void testFind() { EntityDao entityDao=new EntityDao(); List<User> users = entityDao.find(User.class); System.out.println(users.size()); } }
但是运行结果却出乎意料....
java.lang.IllegalArgumentException: node to traverse cannot be null! <span style="white-space:pre"> </span>at org.hibernate.hql.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:63) <span style="white-space:pre"> </span>at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:280) <span style="white-space:pre"> </span>at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182) <span style="white-space:pre"> </span>at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) <span style="white-space:pre"> </span>at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101) <span style="white-space:pre"> </span>at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80) <span style="white-space:pre"> </span>at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98) <span style="white-space:pre"> </span>at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156) <span style="white-space:pre"> </span>at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135) <span style="white-space:pre"> </span>at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760) <span style="white-space:pre"> </span>at cn.lwk.dao.EntityDao.find(EntityDao.java:12) <span style="white-space:pre"> </span>at cn.lwk.test.EntityDaoTest.testFind(EntityDaoTest.java:17) <span style="white-space:pre"> </span>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) <span style="white-space:pre"> </span>at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) <span style="white-space:pre"> </span>at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) <span style="white-space:pre"> </span>at java.lang.reflect.Method.invoke(Method.java:606) <span style="white-space:pre"> </span>at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) <span style="white-space:pre"> </span>at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) <span style="white-space:pre"> </span>at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) <span style="white-space:pre"> </span>at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) <span style="white-space:pre"> </span>at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) <span style="white-space:pre"> </span>at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) <span style="white-space:pre"> </span>at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) <span style="white-space:pre"> </span>at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) <span style="white-space:pre"> </span>at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) <span style="white-space:pre"> </span>at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) <span style="white-space:pre"> </span>at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) <span style="white-space:pre"> </span>at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) <span style="white-space:pre"> </span>at org.junit.runners.ParentRunner.run(ParentRunner.java:236) <span style="white-space:pre"> </span>at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) <span style="white-space:pre"> </span>at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) <span style="white-space:pre"> </span>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) <span style="white-space:pre"> </span>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) <span style="white-space:pre"> </span>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) <span style="white-space:pre"> </span>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)原因:
List userList = session.createQuery("from"+" "+t.getName()).list();from后面加上空格....发现运行出了结果
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。