Note that there are some explanatory texts on larger screens.

plurals
  1. POGot badly stuck in integrating struts2 with hibernate
    primarykey
    data
    text
    <p>i want to integrate Struts with Hibernate. I am new to these technologies.</p> <p>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.</p> <p>In my Java EE simple project which simply need to fetch whole data from database and display on the JSP page.</p> <p>Here is My files :</p> <p>web.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;filter&gt; &lt;filter-name&gt;struts2&lt;/filter-name&gt; &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;struts2&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;session-config&gt; &lt;session-timeout&gt; 30 &lt;/session-timeout&gt; &lt;/session-config&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;example/HelloWorld.jsp&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;/web-app&gt; </code></pre> <p>Helloworld.jsp</p> <pre><code>&lt;%@ page contentType="text/html; charset=UTF-8" %&gt; &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Hello&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;CNAME&lt;/th&gt; &lt;th&gt;BODY&lt;/th&gt; &lt;/tr&gt; &lt;s:iterator value="questionList" var="question"&gt; &lt;tr&gt; &lt;td&gt;&lt;s:property value="cname"/&gt;&lt;/td&gt; &lt;td&gt;&lt;s:property value="body"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/s:iterator&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>example.xml :</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"&gt; &lt;struts&gt; &lt;package name="example" namespace="/example" extends="struts-default"&gt; &lt;action name="HelloWorld" class="example.HelloWorld"&gt; &lt;result&gt;/example/HelloWorld.jsp&lt;/result&gt; &lt;/action&gt; &lt;/package&gt; &lt;/struts&gt; </code></pre> <p>Hibernate.cfg.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; &lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.DerbyDialect&lt;/property&gt; &lt;property name="hibernate.connection.driver_class"&gt;org.apache.derby.jdbc.ClientDriver&lt;/property&gt; &lt;property name="hibernate.connection.url"&gt;jdbc:derby://localhost:1527/Q4U&lt;/property&gt; &lt;property name="hibernate.connection.username"&gt;rambo&lt;/property&gt; &lt;property name="hibernate.connection.password"&gt;**&lt;/property&gt; &lt;mapping resource="hibernate.hbm.xml"/&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p>Hibernate.hbm.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;class name="beans.Question" table="QUESTION"&gt; &lt;!-- &lt;id name="qid" type="java.lang.Integer"&gt; &lt;column name="QID" /&gt; &lt;generator class="increment" /&gt; &lt;/id&gt; &lt;property name="opc" type="java.lang.Integer"&gt; &lt;column name="OPC" not-null="true"/&gt; &lt;/property&gt; &lt;property name="uid" type="java.lang.Integer"&gt; &lt;column name="UID" not-null="true"/&gt; &lt;/property&gt; &lt;property name="cid" type="java.lang.Integer"&gt; &lt;column name="CID" not-null="true"/&gt; &lt;/property&gt; &lt;property name="abuse" type="java.lang.Integer"&gt; &lt;column name="ABUSE" not-null="true"/&gt; &lt;/property&gt; &lt;property name="accuracy" type="java.lang.Float"&gt; &lt;column name="ACCURACY" not-null="true"/&gt; &lt;/property&gt; &lt;property name="poston" type="java.util.Date"&gt; &lt;column name="POSTON" not-null="true"/&gt; &lt;/property&gt; &lt;property name="body" type="java.lang.String"&gt; &lt;column name="BODY" not-null="true"/&gt; &lt;/property&gt; &lt;property name="op1" type="java.lang.String"&gt; &lt;column name="OP1" not-null="true"/&gt; &lt;/property&gt; &lt;property name="op2" type="java.lang.String"&gt; &lt;column name="OP2" not-null="true"/&gt; &lt;/property&gt; &lt;property name="op3" type="java.lang.String"&gt; &lt;column name="OP4" not-null="true"/&gt; &lt;/property&gt; &lt;property name="op4" type="java.lang.String"&gt; &lt;column name="OP4" not-null="true"/&gt; &lt;/property&gt; &lt;property name="op5" type="java.lang.String"&gt; &lt;column name="OP5" not-null="true"/&gt; &lt;/property&gt; &lt;property name="cname" type="java.lang.String"&gt; &lt;column name="CNAME" not-null="true"/&gt; &lt;/property&gt; --&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>Hibernate.reverse.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd"&gt; &lt;hibernate-reverse-engineering&gt; &lt;schema-selection match-schema="RAMBO"/&gt; &lt;table-filter match-name="TESTS"/&gt; &lt;table-filter match-name="USERS"/&gt; &lt;table-filter match-name="QUESTION"/&gt; &lt;table-filter match-name="INBOX"/&gt; &lt;table-filter match-name="ADMIN"/&gt; &lt;table-filter match-name="CATEGORY"/&gt; &lt;/hibernate-reverse-engineering&gt; </code></pre> <p>struts.xml:</p> <pre><code>&lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"&gt; &lt;struts&gt; &lt;include file="example.xml"/&gt; &lt;!-- Configuration for the default package. --&gt; &lt;package name="default" extends="struts-default"&gt; &lt;/package&gt; &lt;/struts&gt; </code></pre> <p>Question.java</p> <pre><code>/* * 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; } } </code></pre> <p>HelloWorld.java</p> <pre><code>/* * $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; /** * &lt;code&gt;Set welcome message.&lt;/code&gt; */ public class HelloWorld extends ActionSupport { private Question question; private List&lt;Question&gt; 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; } } </code></pre> <p>HiberTest.java</p> <pre><code>/* * 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&lt;Question&gt; list() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List&lt;Question&gt; contacts = null; try { contacts = (List&lt;Question&gt;)session.createQuery("from QUESTION").list(); } catch (HibernateException e) { e.printStackTrace(); session.getTransaction().rollback(); } session.getTransaction().commit(); return contacts; } } </code></pre> <p>HibernateUtil.java</p> <pre><code>/* * 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; } } </code></pre> <p>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:</p> <p>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)</p> <p>Please Please Please help me out. Its a simple application to display data of database on the browser in Integrated struts and Hibernate.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload