休眠 5 :- org.hibernate.MappingException: 未知实体

问题描述:

当我尝试将 hibernate 5.0 与 mysql 集成时,我收到错误消息 org.hibernate.MappingException: Unknown entity

I am getting the error message org.hibernate.MappingException: Unknown entity when i am trying to integrate hibernate 5.0 with mysql

这似乎是 hibernate5.0.0 和 5.0.1 的问题.这适用于休眠 4.3.9

This seems to be an issue with hibernate5.0.0 and 5.0.1 . This works fine with hibernate 4.3.9

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

hibernate.cfg.xml

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3307/SampleDB
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

    <mapping class="UserA.User"></mapping>

</session-factory>

package UserA;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.Map;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.ServiceRegistry;

public class HibernateMain {

    public static void main(String[] args) {

        Configuration configuration = new Configuration();
        configuration.configure();
        ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

        SessionFactory sf = configuration.buildSessionFactory(sr);




        User user1 = new User();
        user1.setUserName("Arpit");
        user1.setUserMessage("Hello world from arpit");
        user1.setUserId(22);

        Session ss = sf.openSession();
        ss.beginTransaction();
        // saving objects to session
        ss.save(user1);
        ss.getTransaction().commit();
        ss.close();

    }

}

用户.java

package UserA;

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

@Entity(name="User_table")
public class User {
    @Id
    int userId;
    @Column(name = "User_Name")
    String userName;

    @Column(name = "User_Message")
    String userMessage;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserMessage() {
        return userMessage;
    }

    public void setUserMessage(String userMessage) {
        this.userMessage = userMessage;
    }

}

我已经修复了 Hibernate 5 相同的问题.这段代码有问题

I have fixed the same issue with Hibernate 5. There is a problem in this code

Configuration configuration = new Configuration();
configuration.configure();

ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(
    configuration.getProperties()).build();

SessionFactory sf = configuration.buildSessionFactory(sr);

这段代码适用于 Hibernate 4.3.5,但同样的代码在 Hibernate 5 中也有同样的问题.

This code works fine for Hibernate 4.3.5, but the same code has the same issue for Hibernate 5.

当您执行 configuration.buildSessionFactory(sr) 时,使用 Hibernate 5,Configuration 会丢失所有有关通过调用 configuration.configure().

When you do configuration.buildSessionFactory(sr), using Hibernate 5, Configuration losts all information about mapping that gets by call configuration.configure().

解决方案

为了解决这个问题,如果你使用标准配置文件hibernate.cfg.xmlhibernate.properties,你可以通过这种方式创建会话工厂(不用ServiceRegistry)

To fix the issue, if you use standard configuration files hibernate.cfg.xml and hibernate.properties, you can create the session factory by this way (without ServiceRegistry)

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

加载属性

如果您的文件中有 hibernate.properties 以外的属性,您可以使用 StandardServiceRegistryBuilder 构建会话工厂(无论如何,如果您有 hibernate.properties 和其他文件,它将同时加载)

If you have properties in a file other then hibernate.properties, you can build session factory using StandardServiceRegistryBuilder (anyway, if you have hibernate.properties and other file, it will be loaded both)

将属性作为资源加载

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
    configure().loadProperties("hibernate-h2.properties").build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);  

您需要在类路径(源文件夹的根目录,资源文件夹)中有 hibernate-h2.properties.您也可以从根源文件夹指定路径/com/github/xxx/model/hibernate-h2.properties.

You need to have hibernate-h2.properties in the class path (root of the sources folder, resources folder). You can specify a path from the root source folder too /com/github/xxx/model/hibernate-h2.properties.

从文件系统中的路径加载属性

To load properties from a path in the file system

File propertiesPath = new File("some_path");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
    configure().loadProperties(propertiesPath).build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);

您可以在此处找到使用此方法的示例控制台应用程序 fluent-hibernate-mysql一>.它使用实用程序类从 fluent-hibernate 库 构建会话工厂.

You can find an example console application using this approach here fluent-hibernate-mysql. It uses a utility class to build the session factory from the fluent-hibernate library.

不正确的 Hibernate 5 教程

Hibernate 5 教程中有一个不正确的例子 1.1.6.初创公司和帮手.它使用此代码

There is an incorrect example in Hibernate 5 tutorial 1.1.6. Startup and helpers. It uses this code

 return new Configuration().configure().buildSessionFactory(
                new StandardServiceRegistryBuilder().build() );

它没有进行适当的配置.

It doesn't do a proper configuration.