严重阻碍了将struts2与hibernate集成 [英] Got badly stuck in integrating struts2 with hibernate

查看:112
本文介绍了严重阻碍了将struts2与hibernate集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Struts与Hibernate集成在一起。我对这些技术很陌生。



不幸的是,在过去的4天里,我陷入了困境,我的表格无法映射。
请帮助。
我正在使用netbeans 7.1.2。我已经在德比(Java DB)上创建了数据库。我在其中创建了几个表。



在我的Java EE简单项目中,它只需从数据库获取整个数据并显示在JSP页面上。 b
$ b

以下是我的档案:

web.xml

 <?xml version =1.0encoding =UTF-8?> 
< web-app version =3.0xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\">
< filter>
< filter-name> struts2< / filter-name>
< filter-class> org.apache.struts2.dispatcher.FilterDispatcher< / filter-class>
< / filter>
< filter-mapping>
< filter-name> struts2< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
< session-config>
< session-timeout>
30
< / session-timeout>
< / session-config>
< welcome-file-list>
< welcome-file>示例/ HelloWorld.jsp< / welcome-file>
< / welcome-file-list>
< / web-app>

Helloworld.jsp

 <%@ page contentType =text / html; charset = UTF-8%> 

<%@ taglib prefix =suri =/ struts-tags%>

< html>
< head>
< title> Hello< / title>
< / head>

< body>
< table>
< tr>
< th> CNAME< / th>
BODY< / th>
< / tr>
< s:iterator value =questionListvar =question>
< tr>
< td>< s:属性值=cname/>< / td>
< td>< s:属性值=body/>< / td>
< / tr>
< / s:iterator>
< / table>
< / body>
< / html>

example.xml:

 <?xml version =1.0encoding =UTF-8?> 

<!DOCTYPE struts PUBLIC
- // Apache Software Foundation // DTD Struts Configuration 2.0 // EN
http://struts.apache.org/的DTD /支杆-2.0.dtd>

< struts>
< package name =examplenamespace =/ exampleextends =struts-default>
< action name =HelloWorldclass =example.HelloWorld>
< result> /example/HelloWorld.jsp< / result>
< / action>
< / package>
< / struts>

Hibernate.cfg.xml

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate Configuration DTD 3.0 // ENhttp://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> **< / property>
< mapping resource =hibernate.hbm.xml/>
< / session-factory>
< / hibernate-configuration>

Hibernate.hbm.xml:

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping>
< class name =beans.Questiontable =QUESTION>
<! - < id name =qidtype =java.lang.Integer>
< column name =QID/>
< generator class =increment/>
< / id>
< property name =opctype =java.lang.Integer>
< column name =OPCnot-null =true/>
< / property>
< property name =uidtype =java.lang.Integer>
< column name =UIDnot-null =true/>
< / property>
< property name =cidtype =java.lang.Integer>
< column name =CIDnot-null =true/>
< / property>
< property name =abusetype =java.lang.Integer>
< column name =ABUSEnot-null =true/>
< / property>
< property name =accuracytype =java.lang.Float>
< column name =ACCURACYnot-null =true/>
< / property>
< property name =postontype =java.util.Date>
< column name =POSTONnot-null =true/>
< / property>
< property name =bodytype =java.lang.String>
< column name =BODYnot-null =true/>
< / property>
< property name =op1type =java.lang.String>
< column name =OP1not-null =true/>
< / property>
< property name =op2type =java.lang.String>
< column name =OP2not-null =true/>
< / property>
< property name =op3type =java.lang.String>
< column name =OP4not-null =true/>
< / property>
< property name =op4type =java.lang.String>
< column name =OP4not-null =true/>
< / property>
< property name =op5type =java.lang.String>
< column name =OP5not-null =true/>
< / property>
< property name =cnametype =java.lang.String>
< column name =CNAMEnot-null =true/>
< / property> - >
< / class>
< / hibernate-mapping>

Hibernate.reverse.xml

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-reverse-engineering PUBLIC - // Hibernate / Hibernate反向工程DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd >
< hibernate-reverse-engineering>
< schema-selection match-schema =RAMBO/>
< table-filter match-name =TESTS/>
< table-filter match-name =USERS/>
< table-filter match-name =QUESTION/>
< table-filter match-name =INBOX/>
< table-filter match-name =ADMIN/>
< table-filter match-name =CATEGORY/>
< / hibernate-reverse-engineering>

struts.xml:

 <!DOCTYPE struts PUBLIC 
- // Apache Software Foundation // DTD Struts Configuration 2.0 // EN
http://struts.apache.org/dtds /struts-2.0.dtd\">

< struts>
< include file =example.xml/>
<! - - 默认包的配置。 - >
< package name =defaultextends =struts-default>
< / package>
< / struts>

Question.java

  / * 
*要更改此模板,请选择工具|模板
*并在编辑器中打开模板。
* /
包bean;

import java.util.Date;
import java.io.Serializable;
import javax.persistence。*;
/ **
*
* @author ROMO
* /

@Entity
@Table(name =QUESTION)
public class Question实现Serializable {

int qid,opc,uid,cid,abuse;
浮动精度;
日期poston;
字符串正文,op1,op2,op3,op4,op5,cname;

@Column(name =CNAME)
public String getCname(){
return cname;
}

public void setCname(String cname){
this.cname = cname;


@Column(name =Abuse)
public int getAbuse(){
return abuse;
}

public void setAbuse(int abuse){
this.abuse = abuse;


@Column(name =ACCURACY)
public float getAccuracy(){
return accuracy;
}

public void setAccuracy(float precision){
this.accuracy = accuracy;
}

@Column(name =BODY)
public String getBody(){
return body;
}

public void setBody(String body){
this.body = body;

$ b $ @Column(name =CID)
public int getCid(){
return cid;
}

public void setCid(int cid){
this.cid = cid;


@Column(name =OP1)
public String getOp1(){
return op1;
}

public void setOp1(String op1){
this.op1 = op1;


@Column(name =OP2)
public String getOp2(){
return op2;
}

public void setOp2(String op2){
this.op2 = op2;

$ b @Column(name =OP3)
public String getOp3(){
return op3;
}

public void setOp3(String op3){
this.op3 = op3;


@Column(name =OP4)
public String getOp4(){
return op4;
}

public void setOp4(String op4){
this.op4 = op4;

$ b @Column(name =OP5)
public String getOp5(){
return op5;
}

public void setOp5(String op5){
this.op5 = op5;


@Column(name =OPC)
public int getOpc(){
return opc;
}

public void setOpc(int opc){
this.opc = opc;


@Column(name =POSTON)
@Temporal(javax.persistence.TemporalType.DATE)
public Date getPoston(){
返回poston;
}

public void setPoston(Date poston){
this.poston = poston;

$ b $ @ $
@GeneratedValue
@Column(name =QID)
public int getQid(){
return qid ;
}

public void setQid(int qid){
this.qid = qid;

$ b $ @Column(name =UID)
public int getUid(){
return uid;
}

public void setUid(int uid){
this.uid = uid;
}

}

HelloWorld.java

  / * 
* $ Id:HelloWorld.template,v 1.2 2008-03-27 05:47:21 ub3rsold4t Exp $
*
*根据
*或更多贡献者许可协议授权给Apache软件基金会(ASF)。请参阅此作品发行的NOTICE文件
*以了解有关版权所有权的其他信息
*。 ASF根据Apache许可证2.0版(
*许可证)将此文件
*许可给您;除了符合
*的许可证外,您不得使用此文件。您可以在
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*处获得许可证副本*除非适用法律要求或以书面形式同意,根据许可证分发的
*软件以原样基础以
*的形式发行,不附有任何明示或暗示的任何
*种类的保证或条件。请参阅许可证以了解许可证下的
*特定语言管理权限和限制
*。
* /

包示例;

import java.util.List;
import beans.Question;

import com.opensymphony.xwork2.ActionSupport;

/ **
*< code>设置欢迎信息。< / code>
* /
公共类HelloWorld扩展ActionSupport {

私人问题问题;
私人列表<问题> questionList;
私人HiberTest hiberTest;

public String execute()throws Exception {
this.questionList = hiberTest.list();
System.out.println(execute called);
setMessage(getText(MESSAGE));
返回SUCCESS;
}

/ **
*提供Message属性的默认值。
* /
public static final String MESSAGE =HelloWorld.message;

/ **
*消息属性的字段。
* /
私人字符串消息;

/ **
*返回消息属性。
*
* @return消息属性
* /
public String getMessage(){
return message;
}

/ **
*设置消息属性。
*
* @param message要显示在HelloWorld页面上的文本。
* /
public void setMessage(String message){
this.message = message;


$ / code>

HiberTest.java

  / * 
*要更改此模板,请选择工具|模板
*并在编辑器中打开模板。
* /
包示例;

import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

导入beans.Question;

public class HiberTest extends HibernateUtil {

public List< Question> list(){

Session session = HibernateUtil.getSessionFactory()。getCurrentSession();
session.beginTransaction();
列表<问题> contacts = null;
try {

contacts =(List< Question>)session.createQuery(from QUESTION)。

catch(HibernateException e){
e.printStackTrace();
session.getTransaction()。rollback();
}
session.getTransaction()。commit();
返回联系人;


$ / code>

HibernateUtil.java

  / * 
*要更改此模板,请选择工具|模板
*并在编辑器中打开模板。
* /
包示例;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/ **
* Hibernate Utility类有一个方便的方法来获得Session Factory
*对象。
*
* @author ROMO
* /
public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
//从标准(hibernate.cfg.xml)
// config文件创建SessionFactory。
sessionFactory = new AnnotationConfiguration()。configure()。buildSessionFactory();
} catch(Throwable ex){
//记录异常。
System.err.println(Initial SessionFactory creation failed。+ ex);
抛出新的ExceptionInInitializerError(ex);



public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}

就是这样。当我运行该项目时,它运行,但它不显示任何数据库的数据。
进一步当我右键单击文件Hibernate.cfg.xml并选择运行HQL查询并运行查询:'QUESTION'时,出现以下错误:

org.hibernate.hql.ast.QuerySyntaxException:QUESTION未映射[来自QUESTION]
在org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
在org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
at org。 hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
位于org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
位于org.hibernate.hql。 antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker .java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229 )
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
在org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
在org.hibernate.engine.query.HQLQueryPlan。(HQLQueryPlan.java:77)
在org.hibernate .engine.query.HQLQueryPlan。(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl。 getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)


请请请帮助我。它是一个简单的应用程序,用于在集成的struts和Hibernate中的浏览器上显示数据库数据。

解决方案

名称,而不是表名,并且区分大小写:

  contacts =(List< Question>)session.createQuery(from问题)名单()。 


i want to integrate Struts with Hibernate. I am new to these technologies.

Infortunately i get badly stuck in these for last 4 days and my table is not able to get mapped. Please help. I am working on netbeans 7.1.2. i have created database on derby (Java DB). and i created few tables in it.

In my Java EE simple project which simply need to fetch whole data from database and display on the JSP page.

Here is My files :

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>example/HelloWorld.jsp</welcome-file>
    </welcome-file-list>
</web-app>    

Helloworld.jsp

<%@ page contentType="text/html; charset=UTF-8" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
    <head>
        <title>Hello</title>
    </head>

    <body>
        <table>
            <tr>
                <th>CNAME</th>
                <th>BODY</th>
            </tr>
            <s:iterator value="questionList" var="question">
                <tr>
                    <td><s:property value="cname"/></td>
                    <td><s:property value="body"/></td>
                </tr>   
            </s:iterator>
        </table>
    </body>
</html>

example.xml :

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="example" namespace="/example" extends="struts-default">
        <action name="HelloWorld" class="example.HelloWorld">
            <result>/example/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

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">**</property>
    <mapping resource="hibernate.hbm.xml"/>
  </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="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="OP4" 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.reverse.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="TESTS"/>
  <table-filter match-name="USERS"/>
  <table-filter match-name="QUESTION"/>
  <table-filter match-name="INBOX"/>
  <table-filter match-name="ADMIN"/>
  <table-filter match-name="CATEGORY"/>
</hibernate-reverse-engineering>

struts.xml:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="example.xml"/>
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
    </package>
</struts>

Question.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.util.Date;
import java.io.Serializable;
import javax.persistence.*;
/**
 *
 * @author ROMO
 */

@Entity
@Table (name="QUESTION")
public class Question implements Serializable {

    int qid, opc, uid, cid, abuse;
    float accuracy;
    Date poston;
    String body, op1, op2, op3, op4, op5, cname;

    @Column(name="CNAME")
    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Column(name="ABUSE")
    public int getAbuse() {
        return abuse;
    }

    public void setAbuse(int abuse) {
        this.abuse = abuse;
    }

    @Column(name="ACCURACY")
    public float getAccuracy() {
        return accuracy;
    }

    public void setAccuracy(float accuracy) {
        this.accuracy = accuracy;
    }

    @Column(name="BODY")
    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Column(name="CID")
    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    @Column(name="OP1")
    public String getOp1() {
        return op1;
    }

    public void setOp1(String op1) {
        this.op1 = op1;
    }

    @Column(name="OP2")
    public String getOp2() {
        return op2;
    }

    public void setOp2(String op2) {
        this.op2 = op2;
    }

    @Column(name="OP3")
    public String getOp3() {
        return op3;
    }

    public void setOp3(String op3) {
        this.op3 = op3;
    }

    @Column(name="OP4")
    public String getOp4() {
        return op4;
    }

    public void setOp4(String op4) {
        this.op4 = op4;
    }

    @Column(name="OP5")
    public String getOp5() {
        return op5;
    }

    public void setOp5(String op5) {
        this.op5 = op5;
    }

    @Column(name="OPC")
    public int getOpc() {
        return opc;
    }

    public void setOpc(int opc) {
        this.opc = opc;
    }

    @Column(name="POSTON")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getPoston() {
        return poston;
    }

    public void setPoston(Date poston) {
        this.poston = poston;
    }

    @Id
    @GeneratedValue
    @Column(name="QID")
    public int getQid() {
        return qid;
    }

    public void setQid(int qid) {
        this.qid = qid;
    }

    @Column(name="UID")
    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

}

HelloWorld.java

/*
 * $Id: HelloWorld.template,v 1.2 2008-03-27 05:47:21 ub3rsold4t Exp $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package example;

import java.util.List;
import beans.Question;

import com.opensymphony.xwork2.ActionSupport;

/**
 * <code>Set welcome message.</code>
 */
public class HelloWorld extends ActionSupport {

    private Question question;
    private List<Question> questionList;
    private HiberTest hiberTest;

    public String execute() throws Exception {
        this.questionList = hiberTest.list();
    System.out.println("execute called");
        setMessage(getText(MESSAGE));
        return SUCCESS;
    }

    /**
     * Provide default valuie for Message property.
     */
    public static final String MESSAGE = "HelloWorld.message";

    /**
     * Field for Message property.
     */
    private String message;

    /**
     * Return Message property.
     *
     * @return Message property
     */
    public String getMessage() {
        return message;
    }

    /**
     * Set Message property.
     *
     * @param message Text to display on HelloWorld page.
     */
    public void setMessage(String message) {
        this.message = message;
    }
}

HiberTest.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package example;

import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import beans.Question;

public class HiberTest extends HibernateUtil {

    public List<Question> list() {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Question> contacts = null;
        try {

            contacts = (List<Question>)session.createQuery("from QUESTION").list();

        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return contacts;
    }
}

HibernateUtil.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package example;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author ROMO
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Thats it. When i run the project, it run, but it doesn't show any data of database. Further When i right click on file Hibernate.cfg.xml and select 'Run HQL Query' and run my query : 'from QUESTION', I get the following error:

org.hibernate.hql.ast.QuerySyntaxException: QUESTION is not mapped [from QUESTION] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158) at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87) at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70) at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688) at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111) at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:77) at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:56) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133) at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112) at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)

Please Please Please help me out. Its a simple application to display data of database on the browser in Integrated struts and Hibernate.

解决方案

In HQL queries you use logical entity names, not table names, and they are case sensitive:

contacts = (List<Question>)session.createQuery("from Question").list();  

这篇关于严重阻碍了将struts2与hibernate集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆