2016-07-29 72 views
0

我不斷收到一個一個no persistent classes found for query class當我嘗試做一個查詢,我不知道這是爲什麼?我能夠成功建立連接,但我不知道是什麼導致了無持久類的問題?休眠:我得到「找到的查詢類沒有持久化類」

CarProducts列表的大小,則返回0

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 


/** 
* TODO: Enter Javadoc 
*/ 
@Entity 
@Table(name = "car_product") 
public class CarProduct { 

    //~ Instance fields ------------------------------------------------------------------------------------------------ 

    @Column(name = "car_id") 
    private String carid; 

    @Column(name = "product_id") 
    private String productid; 

    @Column(name = "attribute") 
    private String attribute; 

    @Column(name = "value") 
    private String value; 

    //~ Constructors --------------------------------------------------------------------------------------------------- 

    /** 
    * Creates a new CarProduct object. 
    */ 
    public CarProduct() { 
    }  

    //~ Methods -------------------------------------------------------------------------------------------------------- 

    public String getAttribute() { 
     return attribute; 
    } 

    public void setAttribute(String attribute) { 
     this.attribute = attribute; 
    } 

    public String getCarid() { 
     return carid; 
    } 

    public void setCarid(String carid) { 
     this.carid = carid; 
    } 

    public String getProductid() { 
     return productid; 
    } 

    public void setProductid(String productid) { 
     this.productid = productid; 
    } 

    public String getValue() { 
     return value; 
    } 

    public void setValue(String value) { 
     this.value = value; 
    }  
} 

public static void main(String[] args) throws SQLException { 
     SessionFactory sessionFactory; 
     ServiceRegistry serviceRegistry; 

     Configuration config = new Configuration(); 
     config.configure(); 
     serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 
     sessionFactory = config.buildSessionFactory(serviceRegistry);    
     Session session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 

     // I have to specify the package name too and it is really annoying but it throws an error if just do "from CarProduct".  
     List<CarProduct> carProducts = session.createQuery("from com.searchresults.CarProduct").list(); 
     System.out.println("THE SIZE OF THE LIST IS: " + carProducts.size()); 

     session.getTransaction().commit(); 


} 

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
     <property name="connection.url">None of your business</property> 
     <property name="connection.username">None of your business</property> 
     <property name="connection.password">None of your business</property> 

     <!-- JDBC connection pool settings ... using built-in test pool --> 
     <property name="connection.pool_size">1</property> 

     <!--Select our SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 

     <!-- Print our the SQL to console --> 
     <property name="show_sql">true</property> 

     <!-- Set the current session context --> 
     <property name="current_session_context_class">thread</property> 

     <!-- DB schema will be updated if needed --> 
     <!-- <property name="hbm2ddl.auto">update</property> --> 
    </session-factory> 
</hibernate-configuration> 

我得到以下StrackTrace:

HCANN000001: Hibernate Commons Annotations {4.0.2.Final} HHH000412: Hibernate Core {4.2.2.Final} HHH000206: hibernate.properties not found HHH000021: Bytecode provider name : javassist HHH000043: Configuring from resource: /hibernate.cfg.xml HHH000040: Configuration resource: /hibernate.cfg.xml HHH000041: Configured SessionFactory: null HHH000402: Using Hibernate built-in connection pool (not for production use!) HHH000115: Hibernate connection pool size: 1 HHH000006: Autocommit mode: false HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [None of your business] HHH000046: Connection properties: {user=none of your business, password=none of your business} HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect HHH000399: Using default transaction strategy (direct JDBC transactions) HHH000397: Using ASTQueryTranslatorFactory HHH000183: no persistent classes found for query class: from com.searchresults.CarProduct THE SIZE OF THE LIST IS: 0

回答

0

你需要告訴Hibernate在哪裏可以找到實體類。一種方法是在session-factory下明確提及它。請確保您在那裏擁有完全合格的課程名稱。你的例子包含一個默認包,這就是我在這裏放的東西。

<mapping class="CarProduct"/> 
+0

沒錯內休眠-cfg.xml文件 ,是固定的,但現在我無法爲做一個查詢柱。我「從com.searchresults.CarProduct其中carid =‘嗒嗒’」把''但是我在'carid'得到一個錯誤。 – Robben

+0

該查詢是否正確? '(「來自com.searchresults.CarProduct」)'? – Julian

+0

@Robben你需要發佈錯誤來幫助我們理解問題 –

0

你需要註釋的類映射到會話工廠 <mapping class="com.searchresults.CarProduct"/>