我是hibernate的新手。我试图使用查询类获取数据,但面临遭遇问题。我已经在hibernate论坛上查了但没有找到答案。
实体类:
package com.base.test;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="DBUSER")
public class Users implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name="USERID")
private String userId;
@Column(name="USERNAME")
private String userName;
@Column(name="CREATEDBY")
private String createdBy;
@Column(name="CREATEDDATE")
private Date createdDate;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
}
测试类:
package com.base.test;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.base.util.HibernateUtil;
public class Test {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session=HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Query query = session.createQuery("from Users");
List<Users> usersList = query.list();
for(Users user:usersList){
System.out.println("Id: " + user.getUserId());
System.out.println("Name: " + user.getUserName());
System.out.println("Created By: " + user.getCreatedBy());
System.out.println("Created Date: " + user.getCreatedDate());
}
}
}
HibernateUtil类:
package com.base.util;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
} catch (HibernateException ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
错误日志:
3157 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@983d95
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [clob] overrides previous : org.hibernate.type.ClobType@f30494
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@f30494
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@b1cc87
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@eaf40c
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [blob] overrides previous : org.hibernate.type.BlobType@13c6641
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@13c6641
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@5d391d
3160 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@50a649
Dec 14, 2017 3:08:29 PM net.sf.ehcache.config.ConfigurationFactory parseConfiguration
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/Suhel's%20Folder/Work/POC/jars/ehcache.jar!/ehcache-failsafe.xml
3288 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Users is not mapped [from Users]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:327)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3441)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3325)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:733)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
at com.base.test.Test.main(Test.java:16)
表格结构:
desc DBUSER
Name Null Type
----------- ---- ------------
USERID VARCHAR2(15)
USERNAME VARCHAR2(15)
CREATEDBY VARCHAR2(15)
CREATEDDATE DATE
这是hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1522/DevDB</property>
<property name="hibernate.connection.username">devWork</property>
<property name="hibernate.connection.password">dev$123</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- <property name="cache.provider_class">org.hibernate.cache.EhCache</property> -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<mapping class="com.base.test.Users"></mapping>
</session-factory>
</hibernate-configuration>
有人可以帮我看看我哪里错了。
答案 0 :(得分:1)
我找到了解决方案。
我使用的HQL是:Query query = session.createQuery("from Users");
相反它将是
Query query = session.createQuery("from com.base.test.Users");
那就是我们必须给予实体的完全合格的路径以及它。这与使用它的文件的位置无关。也就是说,它所使用的文件可以与Entity对象位于不同/相同的包中。
当我们使用@NamedQueries时会出现同样的问题。所以下面是错误的:
@NamedQueries({
@NamedQuery(
name = "findUsers",
query = "from Users "
)
})
以下是正确的:
@NamedQueries({
@NamedQuery(
name = "findUsers",
query = "from com.base.test.Users "
)
})