首页 > 代码库 > 菜鸟求援!!!!急!
菜鸟求援!!!!急!
我做的是hibernate的HQL,出现了错误,请指教:
这是代码:
【1】.这是Employee类的定义:
package qau.edu.ren;
public class Employee {
private int no ;
private String name ;
private float salary ;
private Department dept ;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
}
【2】.这是Department类的定义;
package qau.edu.ren;
import java.util.HashSet;
import java.util.Set;
public class Department {
private int no ;
private String name ;
private Set<Employee> employees = new HashSet<Employee>();
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}}
【3】.下面是.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 name="foo">
<!-- 配置SQL是否在控制台进行打印 -->
<property name="show_sql">true</property>
<!-- 配置SQL是否在输出的时候进行格式化 -->
<property name="format_sql">true</property>
<!-- 配置连接数据库的信息 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<!-- 配置生成数据表的方式 -->
<property name="hbm2ddl.auto">update</property>
<!-- 配置数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 配置关系映射文件加载的路径 -->
<!-- <mapping resource="qau/edu/hibernate/OneToMany/Customer.hbm.xml" />
<mapping resource="qau/edu/hibernate/OneToMany/Order.hbm.xml" /> <mapping
resource="qau/edu/One2One/foreign/Manager.hbm.xml" /> <mapping resource="qau/edu/One2One/foreign/Department.hbm.xml"
/> -->
<mapping resource="qau/edu/ren/Employee.hbm.xml" />
<mapping resource="qau/edu/ren/Department.hbm.xml" />
</session-factory>
</hibernate-configuration>
【4】.这是.hbm.xml文件;
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="qau.edu.ren.Department" table="REN_DEPARTMENT">
<id name="no" type="int">
<column name="DEPT_ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<set name="employees" table="REN_EMPLOYEE" inverse="true" >
<key>
<column name="DEPT_ID" />
</key>
<one-to-many class="qau.edu.ren.Employee" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="qau.edu.ren.Employee" table="REN_EMPLOYEE">
<id name="no" type="int">
<column name="EMP_ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="salary" type="float">
<column name="SALARY" />
</property>
<many-to-one name="dept" class="qau.edu.ren.Department" >
<column name="DEPT_ID" />
</many-to-one>
</class>
</hibernate-mapping>
【5】.这是Test测试类;
package qau.edu.ren;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
Session session = new Configuration().configure().buildSessionFactory()
.openSession();// 获取Session;
Transaction tr = session.beginTransaction();// 开始事务;
// Employee emp = (Employee) session.get(Employee.class, 7369);
//
// Department dept = (Department) session.get(Department.class, 30);
//
// System.out.println(dept.getEmployees().size());
String hql ="FROM REN_EMPLOYEE e WHERE e.salary > ? and e.name like ?";
Query query = session.createQuery(hql);
query.setFloat(0,6000)
.setString(1, "%A%");
List<Employee> employees = query.list();
System.out.println(employees);
}
}
【6】.这是提示的异常,但是就是没有解决;(我是用的Oracle 11g 和plsqldev界面工具)
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: REN_EMPLOYEE is not mapped [FROM REN_EMPLOYEE e WHERE e.salary > ? and e.name like ?]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
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:314)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3355)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3239)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:726)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:294)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:237)
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:98)
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:1760)
at qau.edu.ren.Test.main(Test.java:26)
菜鸟求援!!!!急!