如何将struts2与Hibernate集成

时间:2012-07-05 14:06:53

标签: hibernate

我很难将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>

Question.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
}

FindQuestion.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;
    }

}

0 个答案:

没有答案