Hibernate 4:如何解析“需要使用An AnnotationConfiguration实例

时间:2012-08-07 19:51:18

标签: hibernate configuration

我正在使用Hibernate 4.1.5.SP1,但无法配置会话工厂以用于基于注释的类。我尝试用

配置它
    final Configuration hibConfiguration = new Configuration().configure();       

    final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
        .applySettings(hibConfiguration.getProperties())
        .buildServiceRegistry();

    return hibConfiguration.buildSessionFactory(serviceRegistry);

但是我得到了这个异常(“An AnnotationConfiguration实例需要使用

org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: javax.ejb.EJBTransactionRolledbackException: An AnnotationConfiguration instance is required to use <mapping class="com.mainco.fdr.myproject.model.UserRole"/>]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:214)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
* Nested Exception (Underlying Cause) ---------------

javax.ejb.EJBTransactionRolledbackException:需要使用AnnotationConfiguration实例         在org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87)         在org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)         at org.jboss.aspects.tx.TxInterceptor $ Required.invoke(TxInterceptor.java:195)         在org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)         at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)         在org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)         在org.jboss.ejb3.mdb.MessagingContainer.localInvoke(MessagingContainer.java:249)         在org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.delivery(MessageInflowLocalProxy.java:268)         在org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.invoke(MessageInflowLocalProxy.java:138)         在$ Proxy128.execute(未知来源)         在org.jboss.resource.adapter.quartz.inflow.QuartzJob.execute(QuartzJob.java:57)         在org.quartz.core.JobRunShell.run(JobRunShell.java:203)         在org.quartz.simpl.SimpleThreadPool $ WorkerThread.run(SimpleThreadPool.java:520)     由以下原因引起:org.hibernate.MappingException:需要使用AnnotationConfiguration实例         在org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1600)         在org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)         在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)         在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)         在org.hibernate.cfg.Configuration.configure(Configuration.java:1428)         在org.hibernate.cfg.Configuration.configure(Configuration.java:1414)         at org.subco.myco.dido.service.AbstractServiceProvider.getSessionFactory(AbstractServiceProvider.java:64)         在org.subco.myco.dido.service.AbstractServiceProvider.initServices(AbstractServiceProvider.java:127)         在org.subco.myco.dido.quartz.ProcessOrdersJob.execute(ProcessOrdersJob.java:79)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)         在java.lang.reflect.Method.invoke(Method.java:597)         在org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)         在org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)         at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)         在org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)         at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)         在org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)         在org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)         在org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)         在org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)         ......还有11个

以下是我的Maven依赖项

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.jboss.spec.javax.transaction</groupId>
                <artifactId>jboss-transaction-api_1.1_spec</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.jboss.logging</groupId>
                <artifactId>jboss-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

这是我的hibernate.cfg.xml文件......

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="connection.datasource">MySqlDS</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>

        <mapping class="com.mainco.fdr.myproject.model.UserRole" />
        <mapping class="com.mainco.fdr.myproject.model.Organization" />
        <mapping class="com.mainco.fdr.myproject.model.Product" />
        <mapping class="com.mainco.fdr.myproject.model.State" />
        <mapping class="com.mainco.fdr.myproject.model.Country" />
        <mapping class="com.mainco.fdr.myproject.model.AccessCode" />
        <mapping class="com.mainco.fdr.myproject.model.Contract" />
        <mapping class="com.mainco.fdr.myproject.model.TrainingLink" /> 
    </session-factory>
</hibernate-configuration>

我将我的应用程序作为JAR文件回复给JBoss 4.2.2.GA服务器(无法控制JBoss版本)。我应该包含哪些正确的依赖项以及配置会话工厂的正确方法是什么,以便我可以读取基于注释的类,如下面的那样......

@Entity
@Table(name = "lyc_user_role")
public class UserRole {

    @Id
    @Column(name = "USER_ROLE_ID")
    private String roleId;

1 个答案:

答案 0 :(得分:0)

看起来你实际上并没有使用你认为正在使用的Hibernate版本4.1.5.SP1。实际上,当我们将注释(及其处理)合并到hibernate-core模块中时,异常消息在3.6中完全从Hibernate中删除。可能你需要浏览一下JBoss lib目录,你会发现导致问题的旧Hibernate jar。我不记得回到JBoss 4的所有方式都能告诉你在哪里看。