【问题标题】:Hibernate shows error, class is not mappedHibernate 显示错误,类未映射
【发布时间】:2016-07-06 16:34:13
【问题描述】:

我已经创建了一个 DAO 类、实体和所有 Hibernate 配置,但是 Hibernate 一直显示相同的错误:

QuerySyntaxException: StudentEntity 未映射 [SELECT s FROM StudentEntity s]

我的错误在哪里?

表格脚本

CREATE TABLE student(
  id int(3) NOT NULL,
  firstName varchar(20) NOT NULL,
  age int(2) NOT NULL,
  CONSTRAINT id_pk PRIMARY KEY (id)
);

INSERT INTO student VALUES ('101','yashik','23');

SELECT * FROM student;

实体类

package com.demo.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class StudentEntity {
  @Id
  private int id;
  private String firstName;
  private int age;
  //getter and setter

DAO 类

package com.demo.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import com.demo.entity.StudentEntity;
import com.demo.resources.HibernateUtility;
public class StudentDAOImpl implements StudentDAO {
  @Override
  public StudentEntity getStudent(Integer studentID) {
    SessionFactory sessionFactory=HibernateUtility.createSessionFactory();
    Session s1=null;
    StudentEntity stu=null;
    s1=sessionFactory.openSession();
    s1.beginTransaction();
    String st1="SELECT s FROM StudentEntity s";
    Query q1=s1.createQuery(st1);
    List<StudentEntity> l1=q1.list();
    stu.setAge(l1.get(0).getAge());
    stu.setId(l1.get(0).getId());
    stu.setFirstName(l1.get(0).getFirstName());
    if (s1 != null) {
        s1.close();
    }
    return stu;
  }
}

休眠实用程序

package com.demo.resources;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtility {

    private static final String CONFIGURATION_LOCATION="com/demo/resources/hibernate.cfg.xml";
    private static SessionFactory sessionFactory=getSessionFactory();

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            // loads configuration and mappings
            Configuration configuration = new Configuration().configure(CONFIGURATION_LOCATION);
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            // builds a session factory from the service registry
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }

        return sessionFactory;
    }
    public static SessionFactory createSessionFactory(){
        return getSessionFactory();
    }
    public static void closeSessionFactory(){
        if(!sessionFactory.isClosed()){
            sessionFactory.close();
        }
    }
}

Hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/rmc</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">******</property>
        <property name="show_sql">true</property>
        <property name="connection.pool_size">1</property>

        <mapping class="com.demo.entity.StudentEntity"></mapping>
    </session-factory>
</hibernate-configuration>

错误信息

Jul 06, 2016 10:06:18 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jul 06, 2016 10:06:18 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 06, 2016 10:06:18 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 06, 2016 10:06:19 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/rmc]
Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jul 06, 2016 10:06:19 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Wed Jul 06 22:06:19 IST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Jul 06, 2016 10:06:19 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jul 06, 2016 10:06:19 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: StudentEntity is not mapped [SELECT s FROM StudentEntity s]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:131)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:633)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:101)
    at com.demo.dao.StudentDAOImpl.getStudent(StudentDAOImpl.java:22)
    at com.demo.userInterface.UserInterface.main(UserInterface.java:9)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: StudentEntity is not mapped [SELECT s FROM StudentEntity s]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:152)
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:523)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:625)
    ... 3 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: StudentEntity is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:321)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3704)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3593)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:718)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:574)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:311)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:259)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
    ... 9 more

【问题讨论】:

  • 你能改变 String st1="SELECT s FROM StudentEntity s";到字符串 st1="FROM StudentEntity";并尝试
  • 不是这不起作用,它会产生相同的错误。谢谢
  • 欢迎来到 Stack Overflow!我对您的问题进行了一些编辑,以便一开始就将重点更多地放在完整的错误消息上。所以我删除了你的一些介绍性文字,这些文字并不能真正帮助读者快速理解你的问题。

标签: java mysql hibernate


【解决方案1】:

String st1="SELECT s FROM StudentEntity s";

如果我是正确的,您将尝试获取该实体中的所有数据。

我经常用

String st1="来自 StudentEntity";

就是这样,告诉它是否有效。

【讨论】:

    猜你喜欢
    • 2020-04-20
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多