찾다

 >  Q&A  >  본문

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 logVersion
INFO: 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 resolveEntity
WARN: 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 resolveEntity
WARN: 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 configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
三月 29, 2017 4:49:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: 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 buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
三月 29, 2017 4:49:55 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: 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 getIsolatedConnection
INFO: 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 initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: 
    select
        service0_.ID as ID1_1_,
        service0_.TITLE as TITLE2_1_ 
    from
        SERVICE 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

这是怎么回事啊?

怪我咯怪我咯2803일 전557

모든 응답(2)나는 대답할 것이다

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:55:11

    목록<연락처> 목록 = null;

    쿼리 query = s.createQuery("from Service");
    list = query.list();

    목록에 연락처가 포함되어 있지만 유형 불일치로 인해 서비스를 쿼리하고 있습니다. 쿼리가 서비스인 것을 확인한 경우 목록<서비스>로 변경하세요.

    회신하다
    0
  • 怪我咯

    怪我咯2017-04-18 10:55:11

    컬렉션 일반 List는 삭제 후 유형이 없습니다. 따라서 여기에 정의된 컬렉션 유형은 결과 유형과 일치해야 합니다

    회신하다
    0
  • 취소회신하다