1.问题
本文将讨论org.hibernate.MappingException:未知的实体问题和解决方案,既适用于Hibernate,也适用于Spring和Hibernate环境。
2.缺少或无效的@Entity注释
映射异常的最常见原因是缺少@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;
}
}
另一种可能是它可能有错误类型的@Entity注释:
import org.hibernate.annotations.Entity;
@Entity
public class Foo implements Serializable {
...
不推荐使用的org.hibernate.annotations.Entity是要使用的错误类型 - 我们需要的是javax.persistence.Entity:
import javax.persistence.Entity;
@Entity
public class Foo implements Serializable {
...
3. 使用Spring的MappingException
Spring中Hibernate的配置包括通过LocalSessionFactoryBean从注释扫描引导SessionFactory:
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
...
return sessionFactory;
}
Session Factory Bean的这个简单配置缺少一个关键因素,尝试使用SessionFactory的测试将失败:
...
@Autowired
private SessionFactory sessionFactory;
@Test(expected = MappingException.class)
@Transactional
public void givenEntityIsPersisted_thenException() {
sessionFactory.getCurrentSession().saveOrUpdate(new Foo());
}
正如预期的那样,例外是MappingException:未知实体:
org.hibernate.MappingException: Unknown entity:
org.baeldung.ex.mappingexception.persistence.model.Foo
at o.h.i.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1141)
现在,这个问题有两种解决方案 - 两种方式告诉LocalSessionFactoryBean关于Foo实体类。
我们可以指定在类路径中搜索实体类的包:
sessionFactory.setPackagesToScan(
new String[] { "org.baeldung.ex.mappingexception.persistence.model" });
或者我们可以直接将实体类注册到Session Factory中:
sessionFactory.setAnnotatedClasses(new Class[] { Foo.class });
使用这些附加配置行中的任何一个,测试现在将正确运行并通过。
4. MappingException与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;
}
}
该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
运行此测试将导致相同的映射异常:
org.hibernate.MappingException:
Unknown entity: org.baeldung.ex.mappingexception.persistence.model.Foo
at o.h.i.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1141)
从上面的示例中可能已经清楚,配置中缺少的是将实体类的元数据--Foo - 添加到配置中:
configuration.addAnnotatedClass(Foo.class);
这修复了测试 - 现在能够持久化Foo实体。