Hibernate 5: - org.hibernate.MappingException:未知实体 [英] Hibernate 5 :- org.hibernate.MappingException: Unknown entity

查看:120
本文介绍了Hibernate 5: - org.hibernate.MappingException:未知实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这似乎是hibernate5.0.0和5.0.1的问题。这与hibernate 4.3.9正常工作



Maven依赖



 < ;依赖性> 
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 5.0.0.Final< / version>
< /依赖关系>
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.36< / version>
< /依赖关系>



hibernate.cfg.xml



 < session-factory> 

<! - 数据库连接设置 - >
< 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连接池(使用内置) - >
< property name =connection.pool_size> 1< / property>
< property name =dialect> org.hibernate.dialect.MySQLDialect< / property>

<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>

<! - 在启动时删除并重新创建数据库模式 - >
< property name =hbm2ddl.auto>建立< / property>

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

< / session-factory>



HibernateMain.java代码 h3>

  package UserA; 

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
导入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(来自arpit的Hello world);
user1.setUserId(22);

会话ss = sf.openSession();
ss.beginTransaction();
//保存对象到会话
ss.save(user1);
ss.getTransaction()。commit();
ss.close();

}

}



User.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)
字符串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相同的问题。此代码存在问题。

  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来说,相同的代码也有相同的问题。



使用Hibernate 5,配置 configuration.buildSessionFactory(sr) 丢失了通过调用 configuration.configure()获取的映射的所有信息。



为了解决这个问题,如果您使用标准配置文件 hibernate.cfg.xml c>和 hibernate.properties ,您可以通过这种方式创建会话工厂(没有 ServiceRegistry



pre $ SessionFactory sessionFactory = new Configuration()。configure()。buildSessionFactory();

加载属性



如果您的文件中的属性不是 hibernate.properties ,您可以使用 StandardServiceRegistryBuilder 来构建会话工厂。无论如何,如果你有 hibernate.properties 和其他文件,它将被加载)

资源

  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



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

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

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



Hibernate 5教程不正确



Hibernate 5教程中有一个不正确的示例 1.1.6。启动和帮助者。它使用这个代码

 返回新的Configuration().configure()。buildSessionFactory(
新的StandardServiceRegistryBuilder()。build ());

它没有做适当的配置。


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

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

Maven dependices

<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>

HibernateMain.java code

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

    }

}

User.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;
    }

}

解决方案

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

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

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

Solution

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

Loading 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)

To load properties as a resource

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

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

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.

Incorrect Hibernate 5 tutorial

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.

这篇关于Hibernate 5: - org.hibernate.MappingException:未知实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆