در این بخش به بررسی و راه کار برای خطای org.hibernate.MappingException : UnknownEntity میپزدازیم
یکی از محتمل ترین حالت های زخ دادن این خطا موقعی است که در کلاس 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 {
...
که نوع javax.persistence.Entity درست است و org.hibernate.annotations.Entity منقضی شده است :
import javax.persistence.Entity;
@Entity
public class Foo implements Serializable {
...
فرض کنید یک SessionFactory Bean داریم :
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
...
return sessionFactory;
}
حال آنرا تست میکنیم :
...
@Autowired
private SessionFactory sessionFactory;
@Test(expected = MappingException.class)
@Transactional
public void givenEntityIsPersisted_thenException() {
sessionFactory.getCurrentSession().saveOrUpdate(new Foo());
}
و میبینیم که اجرا همراه با خطا است :
org.hibernate.MappingException: Unknown entity: com.baeldung.ex.mappingexception.persistence.model.Foo at o.h.i.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1141)
دو راه حل برای رفع این خطا موجود است :
این دو راه حل به LocalSessionFactoryBean کلاس Entity را معرفی میکند
روش اول معرفی کردن پکیجی که کلاس های Entity در آن آدرس قرار دارند :
sessionFactory.setPackagesToScan(
new String[] { "com.baeldung.ex.mappingexception.persistence.model" });
روش دوم ثبت مستقیم Entity به Session Factory :
sessionFactory.setAnnotatedClasses(new Class[] { Foo.class });
حال اگر از 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 در فایل Properties :
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: com.baeldung.ex.mappingexception.persistence.model.Foo at o.h.i.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1141)
و جهت رفع آن کافیست کلاس Entity را معرفی کنیم :
configuration.addAnnotatedClass(Foo.class);