当我尝试进行查询时,我一直收到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 :(得分:0)
您需要告诉Hibernate在哪里找到Entity类。一种方法是在session-factory
下明确提及它。请确保您拥有完全合格的班级名称。您的示例包含一个默认包,这就是我放在这里的内容。
<mapping class="CarProduct"/>
答案 1 :(得分:0)
您需要将带注释的类映射到hibernate-cfg.xml文件中
在session-factory里面
<mapping class="com.searchresults.CarProduct"/>