Hibernate 5 "is not mapped" 异常

Hibernate 5 "is not mapped" exception

Whosebug 用户!

我在使用 Hibernate 5 时遇到了一个众所周知的错误,我无法用我已经在此处找到的答案解决问题,希望您能找到我的答案我错了,你能告诉我我的 Hibernate conf 是否正常吗?

这是我的 pom:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.9.Final</version>
        </dependency>

这里是cnf:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.connection.password">pwd</property>
        <property name="hibernate.connection.url">jdbc:mysql://host:port/db</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <property name="show_sql">true</property> 
        <property name="connection.pool_size">1</property>

        <mapping class="fr.neio.swagdium.beans.PodiumDo"></mapping>
        <mapping class="fr.neio.swagdium.beans.ItemDo"></mapping>

    </session-factory>
</hibernate-configuration>

这里是 class:

package fr.neio.swagdium.beans;

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


@Entity
@Table(name = "podium")
public class PodiumDo{

    @Id
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;
//getters/setters
}

休眠工具:

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    private static final ServiceRegistry serviceRegistry;

    static {
        Configuration conf = new Configuration();
        conf.configure("/fr/neio/swagdium/resources/hibernate.cfg.xml");
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
        try {
            sessionFactory = conf.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            System.err.println("Initial SessionFactory creation failed." + e);
            throw new ExceptionInInitializerError(e);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

最后,调用如下:

session = getSessionFactory().openSession();
tx = session.beginTransaction();
Query<PodiumDo> query = session.createQuery("FROM PodiumDo where id = :id",PodiumDo.class).setParameter(":id", podiumId);
List<PodiumDo> podiums = query.list();
    if(podiums != null && !podiums.isEmpty()){
        podium = podiums.get(0);
    }
tx.commit();

有什么问题吗? 感谢您阅读所有这些:)

其实错误在配置路径,我没看到使用的配置文件拼写错误重定向到另一个,只描述了数据库连接信息,没有bean。