【问题标题】:Hibernate: I get a "no persistent classes found for query class"Hibernate:我得到一个“没有为查询类找到持久类”
【发布时间】:2016-07-29 19:30:35
【问题描述】:

当我尝试进行查询时,我不断收到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

【问题讨论】:

    标签: java xml hibernate


    【解决方案1】:

    您需要告诉 Hibernate 在哪里可以找到实体类。一种方法是在session-factory 下明确提及它。请确保您在其中具有完全限定的类名。您的示例包含一个默认包,所以这就是我放在这里的内容。

    <mapping class="CarProduct"/>
    

    【讨论】:

    • 是的,修复了它,但现在我无法查询列。我输入了"from com.searchresults.CarProduct where carid='blah'",但在carid 下出现错误。
    • 这个查询对吗? ("from com.searchresults.CarProduct")?
    • @Robben 您需要发布错误以帮助我们了解问题
    • @Julian 是的,它是有效的 HQL 语法。
    • @Julian 是的,你是对的,这很奇怪,但如果我不指定包名,它就不起作用。 @Pradeep ,它不会给出错误,但它只是在carid 下突出显示红色。
    【解决方案2】:

    需要将注解的类映射到hibernate-cfg.xml文件中 在会话工厂内部 &lt;mapping class="com.searchresults.CarProduct"/&gt;

    【讨论】:

      猜你喜欢
      • 2017-06-24
      • 2014-04-02
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多