Hibernate:没有为查询类找到持久化类,但映射是正确的

时间:2017-10-10 08:31:12

标签: java mysql hibernate ejb-3.0

这个问题已被多次询问,但我没有遇到问题的解决方案,因为一见钟情似乎一切正常。使用此Hibernate设置时出现配置问题:

  • hibernate-core:5.2.11.Final
  • hibernate-jpa-2.1-api:1.0.0.Final
  • hibernate-commons-annotations:5.0.1.Final

当我尝试执行查询时,我在控制台中收到一条警告:没有为查询类找到持久类,但没有抛出错误,也没有从数据库中检索数据,尽管它应该返回超过30个条目。

09:56:58,203 INFO  [org.hibernate.Version] (default task-46) HHH000412: Hibernate Core {5.2.11.Final}
09:56:58,206 INFO  [org.hibernate.cfg.Environment] (default task-46) HHH000206: hibernate.properties not found
09:56:58,348 INFO  [org.hibernate.annotations.common.Version] (default task-46) HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
09:56:58,397 WARN  [org.hibernate.orm.connections.pooling] (default task-46) HHH10001002: Using Hibernate built-in connection pool (not for production use!)
09:56:58,398 INFO  [org.hibernate.orm.connections.pooling] (default task-46) HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/db_motor?useSSL=false&autoReconnect=true]
09:56:58,398 INFO  [org.hibernate.orm.connections.pooling] (default task-46) HHH10001001: Connection properties: {user=admin, password=****}
09:56:58,398 INFO  [org.hibernate.orm.connections.pooling] (default task-46) HHH10001003: Autocommit mode: false
09:56:58,400 INFO  [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (default task-46) HHH000115: Hibernate connection pool size: 10 (min=1)
09:56:58,647 INFO  [org.hibernate.dialect.Dialect] (default task-46) HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
09:56:58,958 WARN  [org.hibernate.hql.internal.QuerySplitter] (default task-46) HHH000183: no persistent classes found for query class: SELECT respuestas FROM mr.hibernate.beans.MR_RespuestasHBean respuestas
09:56:58,959 INFO  [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (default task-46) HHH000397: Using ASTQueryTranslatorFactory

如果我正确理解在本网站上发现的信息,那么关于 hibernate.properties not found 的警告似乎没有任何影响。

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="session_factory">
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.password">admin</property>
        <property name="connection.pool_size">10</property> <!-- not for production -->
        <property name="connection.url">jdbc:mysql://localhost:3306/db_motor?useSSL=false&amp;autoReconnect=true</property>
        <property name="connection.username">admin</property>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.use_sql_comments">true</property>
        <mapping class="mr.hibernate.beans.MR_RespuestasHBean"/>
    </session-factory>
</hibernate-configuration>

映射的类:

package mr.hibernate.beans;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "t_respuestas")
public class MR_RespuestasHBean implements Serializable {

    private static final long serialVersionUID = 3892055031093758454L;

    @Id
    @Column(name = "COD_RESPUESTA", nullable = false, length = 11)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long codigoRespuesta;

    public Long getCodigoRespuesta() {
        return codigoRespuesta;
    }

    public void setCodigoRespuesta(Long codigoRespuesta) {
        this.codigoRespuesta = codigoRespuesta;
    }
}

表格定义:

CREATE TABLE t_respuestas
(
   COD_RESPUESTA int PRIMARY KEY NOT NULL
)
;
CREATE UNIQUE INDEX PRIMARY ON t_respuestas(COD_RESPUESTA)
;

用于获取查询的代码是:

    session = getSession(HibernateUtil.configurationFileMySql);
    session.beginTransaction();

    StringBuffer hql = new StringBuffer();

    StringBuffer select = new StringBuffer();
    select.append("SELECT respuestas");

    StringBuffer from = new StringBuffer();
    from.append(" FROM "+ MR_RespuestasHBean.class.getName() +" respuestas");

    hql.append(select);
    hql.append(from);

    @SuppressWarnings("unchecked")
    Query<MR_RespuestasHBean> query = session.createQuery(hql.toString());

    listado = query.list();

如果我通过 MR_RespuestasHBean.class.getSimpleName()更改 MR_RespuestasHBean.class.getName(),则会引发错误:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: MR_RespuestasHBean is not mapped

有关环境的一些信息:

  • JBoss EAP7
  • JDK8
  • MySql connector 6.0.6
  • Hibernate部分位于EJB3项目中

但是,使用此设置时,一切正常(逻辑上,会话加载和查询执行必须不同):

  • hibernate-core:4.2.0.Final
  • hibernate-jpa-2.0-api:1.0.1.Final
  • hibernate-commons-annotations:4.0.1.Final

非常感谢任何导致问题的线索。

0 个答案:

没有答案