Hibernate 5“未映射”异常

时间:2017-04-15 15:21:36

标签: java hibernate-5.x

您好 StackOverflow 用户!

我正面临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>

这是班级:

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
}

hibernate util:

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();

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

1 个答案:

答案 0 :(得分:0)

实际上,错误出在配置路径上,我没有看到所使用的配置文件拼写错误并重定向到另一个文件,该文件仅描述了数据库连接信息,而没有描述任何bean。