Hibernate - 实体经理工厂

时间:2009-12-30 23:57:49

标签: java hibernate seam

我最近遇到了一个问题,我的Web应用程序无法正常启动,堆栈跟踪并未指出发生了什么。我已经能够将它与我写的事件监听器隔离开来。每当我尝试激活它时,我都会得到一个非常普遍的例外:

org.jboss.seam.InstantiationException: Could not instantiate Seam component: entityManagerFactory
    at org.jboss.seam.Component.newInstance(Component.java:2144)
    at org.jboss.seam.contexts.Contexts.startup(Contexts.java:304)
    at org.jboss.seam.contexts.Contexts.startup(Contexts.java:278)
    at org.jboss.seam.contexts.ServletLifecycle.endInitialization(ServletLifecycle.java:116)
    at org.jboss.seam.init.Initialization.init(Initialization.java:740)
    at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:36)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:645)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:189)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:978)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:586)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:349)
    at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:102)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:165)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:162)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:165)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:92)
    at org.eclipse.jetty.server.Server.doStart(Server.java:228)
    at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:69)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:433)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:377)
    at org.mortbay.jetty.plugin.JettyRunWarMojo.execute(JettyRunWarMojo.java:68)
    at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

现在,我在过去遇到过这个问题,这是因为我试图通过让setter和getter最终来优化每个实体类。 Hibernate需要为实体类设置代理,因此它可以延迟加载东西,所以如果我做一个setter / getter final,它就不能这样做。

我想要使用的事件监听器本质上是监听持久化和更新事件。当一个发生时,它应该在一个用相应的注释注释的字段上设置当前日期,以将该字段标记为需要设置当前日期。

我在想这是因为我正在运行更新版本的javassist:

了Javassist:Javassist进行:罐子:3.11.0.GA:运行时

之前有没有人遇到过这个问题?

我在Jetty 7上运行JBoss Seam 2.2.0.GA。

persistence.xml(缩写版)

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">
    <persistence-unit name="${jdbc.database}" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>${jdbc.datasource.name}</non-jta-data-source>

        <class> ... classes go here </class>

        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="${hibernate.dialect}"/>
            <property name="hibernate.hbm2ddl.auto" value="${ddl.mode}"/>

            <property name="hibernate.show_sql" value="${hibernate.showSql}"/>
            <property name="format_sql" value="${hibernate.formatSql}"/>
            <property name="use_sql_comments" value="${hibernate.useSqlComments}"/>

            <property name="hibernate.jdbc.batch_versioned_data" value="true"/>

            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EHCacheProvider"/>

            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.cache.provider_configuration_file_resource_path" value="/ehcache.xml"/>

            <property name="hibernate.connection.release_mode" value="after_transaction"/>
            <property name="hibernate.connection.autocommit" value="true"/>

            <!-- Current Date listeners -->
            <!--
            <property name="hibernate.ejb.event.pre-insert" value="org.hibernate.ejb.event.EJB3PersistEventListener,com.walterjwhite.listener.persistence.listener.SetCurrentDateListener"/>
            <property name="hibernate.ejb.event.pre-update" value="org.hibernate.ejb.event.EJB3MergeEventListener,com.walterjwhite.listener.persistence.listener.SetCurrentDateListener"/>
            -->

            <!-- Envers listeners -->
            <property name="hibernate.ejb.event.post-insert" value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener"/>
            <property name="hibernate.ejb.event.post-update" value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener"/>
            <property name="hibernate.ejb.event.post-delete" value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener"/>
            <property name="hibernate.ejb.event.pre-collection-update" value="org.hibernate.envers.event.AuditEventListener"/>
            <property name="hibernate.ejb.event.pre-collection-remove" value="org.hibernate.envers.event.AuditEventListener"/>
            <property name="hibernate.ejb.event.post-collection-recreate" value="org.hibernate.envers.event.AuditEventListener"/>

            <!--    Hibernate Search    -->
            <property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider"/>
            <property name="hibernate.search.default.indexBase" value="${application.directory}/lucene/indexes"/>
        </properties>
    </persistence-unit>
</persistence>

当我使用Javassist 3.4.GA而不是3.11.GA时,我收到此错误。

java.lang.IllegalAccessError: tried to access class javassist.bytecode.StackMapTable$Writer from class org.jboss.seam.util.ProxyFactory
    at org.jboss.seam.util.ProxyFactory.makeConstructor(ProxyFactory.java:803)
    at org.jboss.seam.util.ProxyFactory.makeConstructors(ProxyFactory.java:685)
    at org.jboss.seam.util.ProxyFactory.make(ProxyFactory.java:565)
    at org.jboss.seam.util.ProxyFactory.createClass3(ProxyFactory.java:346)
    at org.jboss.seam.util.ProxyFactory.createClass2(ProxyFactory.java:325)
    at org.jboss.seam.util.ProxyFactory.createClass(ProxyFactory.java:284)
    at org.jboss.seam.Component.createProxyFactory(Component.java:2426)
    at org.jboss.seam.Component.getProxyFactory(Component.java:1513)
    at org.jboss.seam.Component.wrap(Component.java:1504)
    at org.jboss.seam.Component.instantiateJavaBean(Component.java:1442)
    at org.jboss.seam.Component.instantiate(Component.java:1359)
    at org.jboss.seam.Component.newInstance(Component.java:2122)
    at org.jboss.seam.contexts.Contexts.startup(Contexts.java:304)
    at org.jboss.seam.contexts.Contexts.startup(Contexts.java:278)
    at org.jboss.seam.contexts.ServletLifecycle.endInitialization(ServletLifecycle.java:116)
    at org.jboss.seam.init.Initialization.init(Initialization.java:740)
    at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:36)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:645)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:189)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:978)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:586)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:349)
    at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:102)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:165)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:162)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:165)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:92)
    at org.eclipse.jetty.server.Server.doStart(Server.java:228)
    at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:69)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:433)
    at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:377)
    at org.mortbay.jetty.plugin.JettyRunWarMojo.execute(JettyRunWarMojo.java:68)
    at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

沃尔特

2 个答案:

答案 0 :(得分:4)

我的建议是:

  

始终使用Seam-gen

生成项目

这样,您就不必担心项目需要的库。

消息是:

org.jboss.seam.InstantiationException: Could not instantiate Seam component: entityManagerFactory

您是否设置了EntityManagerFactory?

如果没有,请执行以下操作(我假设您没有使用JTA环境。所以我将向您显示RESOURCE_LOCAL EntityManagerFactory)

/WEB-INF/components.xml

<!--I AM USING 2.1 version-->
<!--SO I SUPPOSE YOU HAVE TO REPLACE 2.1 by 2.2-->
<components xmlns="http://jboss.com/products/seam/components"
        xmlns:core="http://jboss.com/products/seam/core"
        xmlns:persistence="http://jboss.com/products/seam/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation=
            "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd 
             http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd 
             http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd">

<!--SET UP A MANAGED EntityManagerFactory-->
<!--DEFAULT TO ScopeType.APPLICATION-->
<persistence:entity-manager-factory name="entityManagerFactory" persistence-unit-name="<PERSISTENCE_UNIT_NAME_MUST_MATCH_NAME_ATTRIBUTE_IN_PERSISTENCE.XML>"/>

<!--SET UP A MANAGED EntityManager-->
<!--DEFAULT TO ScopeType.CONVERSATION-->
<persistence:managed-persistence-context name="entityManager" entity-manager-factory="#{entityManagerFactory}" auto-create="true"/>

<!--SET UP SEAM TRANSACTION MANAGER-->
<!--IT TAKES CARE OF CALLING BEGIN AND COMMIT-->
<tx:entity-transaction entity-manager="#{entityManager}"/>

</components>

注意当使用auto-create属性等于true时,您不必在@In注释中设置create属性

// You do not need to set up create attribute 
// because of auto-create atrribute in persistence:managed-persistence-context component
@In(create=true)
private EntityManager entityManager;

使用命名约定,EntityManager引用必须匹配持久性中的name属性:managed-persistence-context组件。

现在你必须定义你的/META-INF/persistence.xml

/META-INF/persistence.xml

<persistence-unit name="<PERSISTENCE_UNIT_NAME_GOES_HERE>" transaction-type="RESOURCE_LOCAL">
    // Set up properties here
</persistence-unit>

如果您使用的是可以通过JNDI获取的数据源 - 您需要根据目标应用程序服务器(TOMCAT,JETTY等)进行设置,请执行以下操作

/META-INF/persistence.xml

<persistence-unit name="<PERSISTENCE_UNIT_NAME_GOES_HERE>">
    <non-jta-data-source>jdbc/myAppDS</non-jta-data-source>
</persistence-unit>

如果您想设置当前日期,请执行以下操作,而不是使用Hibernate事件

<!--currentDate IS A BUILT-IN Seam component-->
<!--DEFAULT TO ScopeType.STATELESS-->
<!--ScopeType.STATELESS IS SIMILAR TO Spring prototype scope-->
<h:inputHidden value="#{currentDate}" rendered="false" binding="#{myBackingBean.currentDate}"/>

关于最终关键字,您可能希望看到I get a log message: reflection optimizer disabled

的问候,

答案 1 :(得分:3)

好吧,我没有得到任何好的答案。在我遇到此问题的特定实例中,问题是配置了不正确的依赖项版本。这是我的项目分裂的负面因素之一。我必须密切关注我在每个项目中使用的版本。

在其他情况下,在我看来,我遇到了Lucene配置问题。在任何一种情况下,我都希望看到堆栈跟踪,所以我可以看看它然后,哎呀,我有一个坏罐或类似的东西。

沃尔特