为什么AnnotationConfiguration不推荐使用AnnotationConfiguration实例? [英] Why am I getting AnnotationConfiguration instance is required when AnnotationConfiguration is deprecated?
问题描述
使用Hibernate 4.2.6和Spring 3.1.1
我有一个类似于这个
@Entity
@Table(name =MY_TABLE)
public class MyTable {...}
然后我在我的hibernate.cfg.xml文件中有以下内容:
< mapping class =com.mycompany.myapp.persistence.domain.MyTable/>
当我运行一个JUnit测试时,我得到一个Session并查询该表时,出现以下错误:
创建初始SessionFactory failed.org.hibernate.MappingException:需要使用AnnotationConfiguration实例来使用< mapping class =com .mycompany.myapp.persistence.domain.MyTable/>
我不明白为什么什么时候,基于我读过的内容,AnnotationConfiguration已被弃用,进入配置。
为了获得会话,我正在这样做
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()。applySettings(configuration.getProperties())。buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
但它无法获取会话并引发此错误。
似乎现在正在工作。解决方案有两方面:
第一,这段代码是正确的:
映射类仍然保留在hibernate.cfg.xml中b)创建一个HibernateUtil类b
$ b
创建一个HibernateUtil类 code> public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory(){
try {
//从hibernate.cfg.xml创建SessionFactory
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()。applySettings(configuration.getProperties())。buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
}
catch(Throwable ex){
//确保您记录异常,因为它可能被吞噬
System.err.println(初始化SessionFactory创建失败。 + ex);
抛出新的ExceptionInInitializerError(ex);
public static SessionFactory getSessionFactory(){
return sessionFactory;
创建Model类$ / $
然后在测试中,像这样在setup()中实例化sessionFactory
sessionFactory = HibernateUtil.getSessionFactory();
然后像这样查询数据库 Session session = sessionFactory.openSession();
session.beginTransaction();
列表< TableName> result = session.createQuery(from table)。list();
session.getTransaction()。commit();
session.close();
第二,我使用JBoss 4.2作为我的目标运行时,在使用Hibernate 3.4.2之前,此AnnotatedConfiguration类已被弃用。没有将JBoss作为运行时目标,导致使用Hibernate 4.2.6(通过Maven导入)并且不会导致此错误。 Hibernate在创建它的版本时应该更加注意! : - )
Using Hibernate 4.2.6 and Spring 3.1.1
I have one class similar to this
@Entity @Table( name = "MY_TABLE" ) public class MyTable{ ... }
Then I have the following in my hibernate.cfg.xml file
<mapping class="com.mycompany.myapp.persistence.domain.MyTable"/>
When I run a JUnit test where I get a Session and query the table, I get the following error:
Initial SessionFactory creation failed.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.mycompany.myapp.persistence.domain.MyTable"/>
I don't understand why when, based on what I've read, AnnotationConfiguration is deprecated and functionality moved into Configuration.
To get the Session, I'm doing this
Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); Session session = sessionFactory.openSession();
but it's unable to get the Session and throws this error.
解决方案Seems to be working now. Solution was twofold:
First, this code is correct:
a) mapping class remains in the hibernate.cfg.xml
b) Create a HibernateUtil Class
public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); return configuration.buildSessionFactory(serviceRegistry); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
c) Create the Model class
d) Then in the test, instantiate the sessionFactory in the setup() like this
sessionFactory = HibernateUtil.getSessionFactory();
e) Then query the database like this
Session session = sessionFactory.openSession(); session.beginTransaction(); List<TableName> result = session.createQuery("from table").list(); session.getTransaction().commit(); session.close();
Second, I was using JBoss 4.2 as my targeted runtime, which was using Hibernate 3.4.2, before this AnnotatedConfiguration class was deprecated. Not targeting JBoss as a runtime results in using Hibernate 4.2.6 (imported via Maven) and not causing this error. Should've paid better attention as Hibernate prints its version when it sets up! :-)
这篇关于为什么AnnotationConfiguration不推荐使用AnnotationConfiguration实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!