2012-07-05 4 views
0

나는 Struts와 Hibernate를 통합하고 싶다. 나는이 기술에 익숙하지 않다.struts2를 최대 절전 모드로 통합하는데 심하게 갇혀있다

지난 4 일 동안 유감스럽게도이 테이블에 심하게 붙어서 테이블을 매핑 할 수 없습니다. 도와주세요. netbeans 7.1.2에서 작업하고 있습니다. Derby (Java DB)에서 데이터베이스를 만들었습니다. 그리고 나는 그것에 몇 테이블을 만들었습니다.

데이터베이스에서 전체 데이터를 가져 와서 JSP 페이지에 표시하기 만하면되는 Java EE의 간단한 프로젝트에서. 여기

내 파일입니다 : HelloWorld.jsp를

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>  

<%@ 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

0,123,973,142,385 그것은

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

이잖아. 프로젝트를 실행하면 실행되지만 데이터베이스의 데이터는 표시되지 않습니다. 추가 파일 Hibernate.cfg.xml을 마우스 오른쪽 단추로 클릭하고 'Run HQL Query'를 선택하고 'QUESTION에서'쿼리를 실행하면 다음 오류가 발생합니다.

org.hibernate.hql.ast.QuerySyntaxException : QUESTION이 (가) 매핑되지 않았습니다. [질문에서] 012.htm 네임 스페이스에 .htp. 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.org.hibernate.hql.antlr.HqlSqlBaseWalker.statement에서 org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement (HqlSqlBaseWalker.java:281) 에서 hibernate.hql.antlr.HqlSqlBaseWalker.query (HqlSqlBaseWalker.java:544) (HqlSqlBaseWalker.java:229) 조직에서 org.hibernate.hql.ast.QueryTranslatorImpl.doCompile (QueryTranslatorImpl.java:160) 에서 org.hibernate.hql.ast.QueryTranslatorImpl.analyze (QueryTranslatorImpl.java:228) 에서 . (HQLQueryPlan.java:77) at hibernate.hql.ast.QueryTranslatorImpl.compile java : 56) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan (QueryPlanCache.java:72) at org.hibernate.impl.AttributeSessionImpl.getHQLQueryPlan (AbstractSessionImpl.java:133) at org.hibernate.impl.AbstractSessionImpl.createQuery (AbstractSessionImpl.java:112) at org.hibernate.impl.SessionImpl.createQuery (SessionImpl. java : 1623)

제발 나를 도와주세요. 통합 struts와 Hibernate에서 브라우저의 데이터베이스 데이터를 표시하는 간단한 응용 프로그램입니다. HQL에서

+0

누군가가 내 aother 문제를 해결할 수있다 ..? http://stackoverflow.com/questions/11487435/how-to-setup-an-action-link-to-delete-the-row-from-the-table-loaded-from-the-tab – codeofnode

답변

2

는 논리 엔티티 이름이 아닌 테이블 이름을 사용하고, 그들이 경우를 구분 조회 :

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

감사합니다. 너무 많이 감사합니다. 너의 도움으로. 이제 여전히 다른 오류가 발생합니다 : org.hibernate.InvalidMappingException : C : \ Documents 및 Settings \ ROMO \ My Documents \ NetBeansProjects \ ATest \ src \ java \ hibernate.hbm.xml 파일의 매핑 문서를 구문 분석 할 수 없습니다 – codeofnode

+0

다시 도움이 .. – codeofnode

+1

@Rambo 위의 [정답을 수락] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)을 클릭하고 별도의 질문 게시를 고려해보십시오. 당신은 계속 문제가 있습니다. – nmc