2012-01-18 121 views
1

我有我的hibernate.cfg.xml文件作爲mysql的表中選擇數據,如何從使用Hibernate

<hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.bytecode.use_reflection_optimizer">false</property> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.password">123789</property> 
     <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/ofm_mnu_jvs</property> 
     <property name="hibernate.connection.username">user1</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
     <property name="show_sql">true</property> 
     <mapping resource="org/ofm/mnu/mappings/ActMonths.hbm.xml"></mapping> 
    </session-factory> 

映射,

<hibernate-mapping> 
    <class name="org.ofm.mnu.mappings.ActMonths" table="act_months" catalog="ofm_mnu_jvs"> 
     <composite-id name="id" class="org.ofm.mnu.mappings.ActMonthsId"> 
      <key-property name="year" type="int"> 
       <column name="year" /> 
      </key-property> 
      <key-property name="monthNo" type="int"> 
       <column name="month_no" /> 
      </key-property> 
     </composite-id>   

     <property name="lastDay" type="java.lang.Integer"> 
      <column name="last_day" /> 
     </property> 
     <property name="month" type="string"> 
      <column name="month" length="20" /> 
     </property> 
     <property name="user" type="string"> 
      <column name="user" length="20" /> 
     </property> 
     <property name="sysDateTime" type="timestamp"> 
      <column name="sys_date_time" length="19" not-null="false" /> 
     </property> 
     <property name="status" type="boolean"> 
      <column name="status" not-null="true" /> 
     </property> 
    </class> 
</hibernate-mapping> 

當我試圖從數據庫中選擇數據如,

Session session = HibernateUtil.getSessionFactory().openSession(); 
String SQL_QUERY = "from act_months"; 

     Query query = session.createQuery(SQL_QUERY); 
     session.getTransaction().commit(); 
     for (Iterator it = query.iterate(); it.hasNext();) { 
      Object[] row = (Object[]) it.next(); 
      System.out.println("ID: " + row[0]); 

     } 

我獲得以下錯誤,

INFO: Not binding factory to JNDI, no JNDI name configured 
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: act_months is not mapped [from act_months] 
    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.<init>(HQLQueryPlan.java:77) 
    at org.hibernate.engine.query.HQLQueryPlan.<init>(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) 

,請告訴我,我錯了。

+0

你錯過添加或者它只是複製粘貼錯誤? – Reddy 2012-01-18 05:50:23

回答

4

說,而不是將其

from ActMonthsfrom table_name

+0

> @Jigar喬希:感謝名單了很多frinend – Harsha 2012-01-18 05:51:51

+0

歡迎您:) – 2012-01-18 05:56:11

0

使用實際的對象名ActMonths不表名act_months

記得ORM代表

+0

啊@Jigar打我吧!愚蠢的IM分心! – gotomanners 2012-01-18 05:53:52

0

在配置文件的末尾嘗試這個

String SQL_QUERY = "from ActMonths act_months";