Как выбрать данные из таблицы MySQL, используя Hibernate - PullRequest
1 голос
/ 18 января 2012

У меня есть файл 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>

Mapping

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

4 голосов
/ 18 января 2012

скажи это

from ActMonths вместо from table_name

0 голосов
/ 18 января 2012

Попробуйте

    String SQL_QUERY = "from ActMonths act_months";
0 голосов
/ 18 января 2012

Используйте фактическое имя объекта ActMonths, а не имя таблицы act_months.

Помните, что означает ORM

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...