我在将 struts2 与 Hibernate 集成时陷入困境。
我正在使用 netbeans 7.1.2
我有一个非常简单的 Java EE 项目,它显示 Derby (Java DB) 数据库中表“QUESTION”的所有行。我已经制作了表格并将其映射到 hibernate.cfg.xml 文件。
但是当我右键单击并转到“运行 HQL 查询”并执行查询时
“来自问题”,我收到以下错误:
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
Caused by: java.sql.SQLSyntaxErrorException: Table/View 'RAMBO.QUESTION' does not exist.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:505)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:423)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1547)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
... 9 more
Caused by: org.apache.derby.client.am.SqlException: Table/View 'RAMBO.QUESTION' does not exist.
at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
at org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source)
... 17 more
我的 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.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/Q4U</property>
<property name="hibernate.connection.username">rambo</property>
<property name="hibernate.connection.password">ho</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="hibernate.hbm.xml"/>
<mapping/>
</session-factory>
</hibernate-configuration>
我的 Hibernate.hbm.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="beans.Question" table="RAMBO.QUESTION">
<id name="qid" type="java.lang.Integer">
<column name="QID" />
<generator class="increment" />
</id>
<property name="opc" type="java.lang.Integer">
<column name="OPC" not-null="true"/>
</property>
<property name="uid" type="java.lang.Integer">
<column name="UID" not-null="true"/>
</property>
<property name="cid" type="java.lang.Integer">
<column name="CID" not-null="true"/>
</property>
<property name="abuse" type="java.lang.Integer">
<column name="ABUSE" not-null="true"/>
</property>
<property name="accuracy" type="java.lang.Float">
<column name="ACCURACY" not-null="true"/>
</property>
<property name="poston" type="java.util.Date">
<column name="POSTON" not-null="true"/>
</property>
<property name="body" type="java.lang.String">
<column name="BODY" not-null="true"/>
</property>
<property name="op1" type="java.lang.String">
<column name="OP1" not-null="true"/>
</property>
<property name="op2" type="java.lang.String">
<column name="OP2" not-null="true"/>
</property>
<property name="op3" type="java.lang.String">
<column name="OP3" not-null="true"/>
</property>
<property name="op4" type="java.lang.String">
<column name="OP4" not-null="true"/>
</property>
<property name="op5" type="java.lang.String">
<column name="OP5" not-null="true"/>
</property>
<property name="cname" type="java.lang.String">
<column name="CNAME" not-null="true"/>
</property>
</class>
</hibernate-mapping>
Hibernate.reveng.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-schema="RAMBO"/>
<table-filter match-name="USERS"/>
<table-filter match-name="CATEGORY"/>
<table-filter match-name="TESTS"/>
<table-filter match-name="INBOX"/>
<table-filter match-name="QUESTION"/>
<table-filter match-name="ADMIN"/>
</hibernate-reverse-engineering>
问题.java
package beans;
import java.io.Serializable;
import java.util.Date;
public class Question implements Serializable {
int qid, opc, uid, cid, abuse;
float accuracy;
Date poston;
String body, op1, op2, op3, op4, op5, cname;
// Also public getters and setters
}
查找问题.java
package controller;
import beans.Question;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
public class FindQuestion extends ActionSupport {
private List questionList = new ArrayList();
@Override
public String execute() throws Exception {
try {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
this.questionList = (List<Question>) session.createQuery("from Question");
session.getTransaction().commit();
} catch (Exception e) {
}
return SUCCESS;
}
}