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

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

请告诉我哪里错了。

最佳答案

说出来

来自 ActMonths 而不是 来自 table_name

关于java - 如何使用 Hibernate 从 mysql 表中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8905738/

相关文章:

java - 如何删除字符串中的前10个字符?

java - 检查 5x6 矩阵中的相邻单元格

mysql - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : YES) - hibernate

java - Hibernate 多态查询

java - 使用枚举作为鉴别器值的 SINGLE_TABLE 继承策略

java - 基于前提条件创建新实例的最佳方法

Java - 停止 ExecutorService 中的所有任务

java - JTable 自动建议抛出 .IllegalComponentStateException

java - jpa/hibernate 的实体更新问题

java - 如何在 JPA 中映射复合主键,其中主键的一部分是外键