有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate:我得到一个“没有为查询类找到持久类”

当我尝试进行查询时,我总是得到一个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


共 (2) 个答案

  1. # 1 楼答案

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

    <mapping class="CarProduct"/>
    
  2. # 2 楼答案

    您需要将带注释的类映射到hibernate cfg中。xml文件 在会话工厂内部 <mapping class="com.searchresults.CarProduct"/>