0

我正在使用 Hibernate 4.1.5.SP1,但在配置会话工厂以与基于注释的类一起使用时遇到问题。我尝试配置它

    final Configuration hibConfiguration = new Configuration().configure();       

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

    return hibConfiguration.buildSessionFactory(serviceRegistry);

但我得到了这个异常(“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:在 org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130) 的 org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87) 需要使用 AnnotationConfiguration 实例) 在 org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) 在 org.jboss.ejb3.stateless 的 org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)。 StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.mdb.MessagingContainer.localInvoke(MessagingContainer.java:249) at org .jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.delivery(MessageInflowLocalProxy.java:268) 在 org.jboss.ejb3.mdb。inflow.MessageInflowLocalProxy.invoke(MessageInflowLocalProxy.java:138) at $Proxy128.execute(Unknown Source) at org.jboss.resource.adapter.quartz.inflow.QuartzJob.execute(QuartzJob.java:57) at org.quartz.core .JobRunShell.run(JobRunShell.java:203) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520) 原因:org.hibernate.MappingException:在 org.hibernate 需要使用 AnnotationConfiguration 实例.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) at org.hibernate.cfg.Configuration.configure(Configuration.java:1414) at org.subco.myco.dido.service.AbstractServiceProvider.getSessionFactory(AbstractServiceProvider.java:64) at org.subco .myco.dido.service.AbstractServiceProvider.initServices(AbstractServiceProvider.java:127) 在 org.subco.myco.dido.quartz.ProcessOrdersJob.execute(ProcessOrdersJob.java:79) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 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。org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) 的interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)在 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 更多166) 在 org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) 在 org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor 的 org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63) .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 更多166) 在 org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) 在 org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor 的 org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63) .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 更多MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org .jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java :126) ... 11 更多MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org .jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java :126) ... 11 更多ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126) ... 11 更多ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at 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 文件……</p>

<?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 版本)。我应该包含哪些正确的依赖项以及配置会话工厂的正确方法是什么,以便我可以读取基于注释的类,如下面的……</p>

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

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

看起来您可能需要用引号将用户代理包装起来。在设置标题时试试这个,看看是否能解决问题。

$headers = array(
        'X-PHX' => 'true',
        'X-Requested-With' => 'XMLHttpRequest',
        'Referer' => 'https://domain.com/index.html',
        'User-Agent' => "'".self::getRandomUserAgent()."'"
    );
    $this->setHeaders($headers);
4

1 回答 1

0

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

于 2012-08-07T22:28:53.797 回答