Hibernate Mapping Exception – Unknown Entity – Hibernate Mapping Exception – Unknown Entity

最后修改: 2013年 8月 13日

中文/混合/英文(键盘快捷键:t)

1. The Problem

1.问题

This article is going to discuss the org.hibernate.MappingException: Unknown entity issue and solutions, both for Hibernate as well as for a Spring and Hibernate environment.

本文将讨论org.hibernate.MappingException:未知实体问题和解决方案,既包括Hibernate,也包括Spring和Hibernate环境。

2. Missing or Invalid @Entity Annotation

2.缺少或无效的@Entity注释

The most common cause for the mapping exception is simply an entity class missing the @Entity annotation:

导致映射异常的最常见原因是实体类缺少@Entity注解

public class Foo implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    public Foo() {
        super();
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}

Another possibility is that it may have the wrong type of @Entity annotation:

另一种可能是,它可能有错误的@Entity注释类型

import org.hibernate.annotations.Entity;

@Entity
public class Foo implements Serializable {
    ...

The deprecated org.hibernate.annotations.Entity is the wrong type of entity to use – what we need is the javax.persistence.Entity:

被废弃的org.hibernate.annotations.Entity是错误的实体类型 – 我们需要的是javax.persistence.Entity

import javax.persistence.Entity;

@Entity
public class Foo implements Serializable {
    ...

3. MappingException With Spring

3.MappingException With Spring

The configuration of Hibernate in Spring involves bootstrapping the SessionFactory from annotation scanning, via a LocalSessionFactoryBean:

在Spring中配置Hibernate涉及通过LocalSessionFactoryBean,从注解扫描中引导SessionFactory

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(restDataSource());
    ...
    return sessionFactory;
}

This simple configuration of the Session Factory Bean is missing a key ingredient, and a test trying to use the SessionFactory will fail:

这个简单的Session Factory Bean的配置缺少了一个关键因素,试图使用SessionFactory的测试将失败。

...
@Autowired
private SessionFactory sessionFactory;

@Test(expected = MappingException.class)
@Transactional
public void givenEntityIsPersisted_thenException() {
    sessionFactory.getCurrentSession().saveOrUpdate(new Foo());
}

The exception is, as expected – the MappingException: Unknown entity:

异常是,正如预期的那样–MappingException。未知实体

org.hibernate.MappingException: Unknown entity: 
com.baeldung.ex.mappingexception.persistence.model.Foo
    at o.h.i.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1141)

Now, there are two solutions to this problem – two ways ways to tell the LocalSessionFactoryBean about the Foo entity class.

现在,这个问题有两种解决方案–两种告诉LocalSessionFactoryBean关于Foo实体类的方法。

We can specify which packages to search for entity classes in the classpath:

我们可以指定在classpath中搜索实体类的

sessionFactory.setPackagesToScan(
  new String[] { "com.baeldung.ex.mappingexception.persistence.model" });

Or we can simply register the entity classes directly into the Session Factory:

或者我们可以直接将实体类直接注册到会话工厂。

sessionFactory.setAnnotatedClasses(new Class[] { Foo.class });

With either of these additional configuration lines, the test will now run correctly and pass.

有了这些额外的配置行,现在测试将正确运行并通过。

4. MappingException With Hibernate

4.使用Hibernate的MappingException

Let’s now see the error when using just Hibernate:

现在让我们看看只使用Hibernate时的错误。

public class Cause4MappingExceptionIntegrationTest {

    @Test
    public void givenEntityIsPersisted_thenException() throws IOException {
        SessionFactory sessionFactory = configureSessionFactory();

        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.saveOrUpdate(new Foo());
        session.getTransaction().commit();
    }

    private SessionFactory configureSessionFactory() throws IOException {
        Configuration configuration = new Configuration();
        InputStream inputStream = this.getClass().getClassLoader().
          getResourceAsStream("hibernate-mysql.properties");
        Properties hibernateProperties = new Properties();
        hibernateProperties.load(inputStream);
        configuration.setProperties(hibernateProperties);

        // configuration.addAnnotatedClass(Foo.class);

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
          applySettings(configuration.getProperties()).buildServiceRegistry();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }
}

The hibernate-mysql.properties file contains the Hibernate configuration properties:

hibernate-mysql.properties文件包含Hibernate配置属性

hibernate.connection.username=tutorialuser
hibernate.connection.password=tutorialmy5ql
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.url=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create

Running this test will result in the same mapping exception:

运行这个测试将导致同样的映射异常。

org.hibernate.MappingException: 
  Unknown entity: com.baeldung.ex.mappingexception.persistence.model.Foo
    at o.h.i.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1141)

As it is probably already clear from the example above, what is missing from the configuration is adding the metadata of the entity class – Foo – to the configuration:

从上面的例子中可能已经很清楚了,配置中缺少的是将实体类–Foo的元数据添加到配置中

configuration.addAnnotatedClass(Foo.class);

This fixes the test – which is now able to persist the Foo entity.

这修复了测试–现在能够持久化Foo实体。

5. Conclusion

5.结论

This article illustrated why the Unknown entity Mapping Exception may occur, and how to fix the problem when it does, first at the entity level, then with Spring and Hibernate and finally, just with Hibernate alone.

这篇文章说明了为什么会出现未知实体映射异常,以及当它出现时如何解决这个问题,首先是在实体层面,然后是Spring和Hibernate,最后是单独使用Hibernate。

The implementation of all exceptions examples can be found in the github project – this is an Eclipse based project, so it should be easy to import and run as it is.

所有异常的实现都可以在github项目中找到 – 这是一个基于Eclipse的项目,所以应该很容易导入并按原样运行。