如何使用Hibernate从mysql表中选择数据

时间:2012-01-18 05:46:16

标签: java hibernate

我的hibernate.cfg.xml文件为

<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)

请告诉我哪里错了。

3 个答案:

答案 0 :(得分:4)

说出来

from ActMonths代替from table_name

答案 1 :(得分:0)

使用实际对象名ActMonths而不是表名act_months

记住ORM代表什么

答案 2 :(得分:0)

试试这个

    String SQL_QUERY = "from ActMonths act_months";