色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

java - 為什么hibernate查詢表集報錯?

瀏覽:147日期:2024-01-09 13:11:04

問題描述

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>,在擦除之后就沒有類型了,因此,在這里定義的集合類型需要與結果類型一致,泛型之間不存在多態

標簽: java
相關文章:
主站蜘蛛池模板: 久久99亚洲精品久久久久网站 | 老司机亚洲精品影院在线 | 日韩精品永久免费播放平台 | 久久视频6免费观看视频精品 | 欧美在线精品一区二区三区 | 国产性生交xxxxx免费 | 美女大片高清特黄a大片 | 亚洲综合中文 | 日韩欧美一区二区三区在线 | 九九视频在线观看6 | 九九九热在线精品免费全部 | 好吊操这里只有精品 | 她也啪在线视频 | 国产午夜在线观看视频播放 | 99精品国产一区二区三区 | 手机在线毛片 | 国产精品久久久免费视频 | 精品厕拍| 妖精www成人动漫在线观看 | 久草视频福利在线 | 俄罗斯a级毛片 | 国产成人精品久久综合 | 日韩欧美一级毛片精品6 | 国产蜜汁tv福利在线 | 国产孕妇孕交大片孕 | 一级毛片牲交大片 | 毛片免费观看的视频在线 | 视频日韩 | 免费一级特黄 | 免费v片视频在线观看视频 免费v片在线观看 | 国产成人精品免费视频大全五级 | 久久国产精品高清一区二区三区 | 国产精品自拍亚洲 | 亚洲欧美日韩精品久久久 | 在线视频观看免费视频18 | 给我一个可以看片的www日本 | 国产人做人爱视频精品 | 免费韩国一级毛片 | 美女黄色在线看 | 久久精品二三区 | 99精品视频在线在线视频观看 |