java - 为什么hibernate查询表集报错?
问题描述
import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import qiye.Impl.ServiceDaoImpl;import qiye.entity.Contact;import qiye.entity.Service;public class TestJava { private static SessionFactory sessionFactory; static{Configuration configuraction = new Configuration().configure();sessionFactory = configuraction.buildSessionFactory(); } public static Session getSession(){return sessionFactory.openSession(); } public static List<Contact> getContactList(){Session s = null;List<Contact> list = null;try{ s = TestJava.getSession(); Query query = s.createQuery('from Service'); list = query.list(); for(Contact contact : list){System.out.println(contact.getAddress()); }}catch(Exception e){ e.printStackTrace();}finally{ if(s!=null){ s.close(); }}return list; }public static void main(String[] arg){TestJava.getContactList();System.out.println('hello'); }}
报错信息:
三月 29, 2017 4:49:54 下午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {5.2.7.Final}三月 29, 2017 4:49:54 下午 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found三月 29, 2017 4:49:54 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}三月 29, 2017 4:49:54 下午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntityWARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time.三月 29, 2017 4:49:55 下午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntityWARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time.三月 29, 2017 4:49:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureWARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)三月 29, 2017 4:49:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/qiye?useUnicode=true&characterEncoding=UTF-8]三月 29, 2017 4:49:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH10001001: Connection properties: {user=root, password=****}三月 29, 2017 4:49:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH10001003: Autocommit mode: false三月 29, 2017 4:49:55 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>INFO: HHH000115: Hibernate connection pool size: 20 (min=1)三月 29, 2017 4:49:55 下午 org.hibernate.dialect.Dialect <init>INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect三月 29, 2017 4:49:56 下午 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnectionINFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@345c5f] for (non-JTA) DDL execution was not in auto-commit mode; the Connection ’local transaction’ will be committed and the Connection will be set into auto-commit mode.三月 29, 2017 4:49:56 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateServiceINFO: HHH000397: Using ASTQueryTranslatorFactoryHibernate: selectservice0_.ID as ID1_1_,service0_.TITLE as TITLE2_1_ fromSERVICE service0_java.lang.ClassCastException: qiye.entity.Service cannot be cast to qiye.entity.Contact at TestJava.getContactList(TestJava.java:33) at TestJava.main(TestJava.java:47)hello
这是怎么回事啊?
问题解答
回答1:List<Contact> list = null;
Query query = s.createQuery('from Service');list = query.list();
你的list里面是Contact,但是你查询的是Service,类型不匹配造成的,如果你确认查询的就是Service,改成List<Service>就行了
回答2:集合泛型List<Contact>,在擦除之后就没有类型了,因此,在这里定义的集合类型需要与结果类型一致,泛型之间不存在多态
相关文章:
1. python2.7 - 在Eclipse开发Python,怎么去掉控制台输出的其他消息??2. css - div内部滚动,安卓没有滚动条(非body滚动),有什么办法可以显示吗?3. 数据库安全 - 在公网上部署mysql数据库,安全方面需要做到哪些注意事项4. 求大神帮我看看是哪里写错了 感谢细心解答5. python - django里GROUP BY 如何写这样的mysql语句?6. css - 定位为absolute的父元素中的子元素 如何设置在父元素的下面?7. html5 - ajax post请求后如何渲染到页面上8. python - 如何使用jsom.dumps获得完整的json数据?9. 如何判断数据库的库和表是否存在?10. javascript - 使用 vuex-router-sync寄存路由信息,this.$router params 不能赋值?
