JPA持久化事务回滚java.lang.NoClassDefFoundError

时间:2014-09-18 12:34:51

标签: java jpa rollback jta transactional

我试图写一个持久化实体的函数。它非常简单,但在持久化实体时会抛出java.lang.NoClassDefFoundError。我不明白为什么。

我整天都在RTFMed,为什么这么简单的代码不起作用,但只发现了java.lang.NoClassDefFoundError。

功能:

@Transactional(Transactional.TxType.REQUIRED)
public void commitTest1() {
    try {
        Replaces replaceEntry=new Replaces(6077, new Date(), "username");
        replaceEntry.setAddId(1);
        entityManager.persist(replaceEntry);
    } catch (Throwable ex) {
        ex.printStackTrace();
    } finally {

    }
}

实体类:

@Entity
@Table(name = "replaces")
@XmlRootElement
public class Replaces implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Basic(optional = false)
    @NotNull
    @Column(name = "date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "user")
    private String user;

    @Basic(optional = false)
    @Column(name = "computer_id")
    private Integer computerId;

    @Basic(optional = true)
    @Column(name = "remove_id")
    private Integer removeId;

    @Basic(optional = true)
    @Column(name = "add_id")
    private Integer addId;
//getters, setters
}

和persistence.xml:

<persistence-unit name="Sandbox-ejbPU" transaction-type="JTA">
    <jta-data-source>jdbc/testdb</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
</persistence-unit>

我将不胜感激任何帮助或建议。

更新:堆栈跟踪

Severe:   java.lang.NoClassDefFoundError: sandbox/entities/Replaces 
    at sandbox.ReplacesTester.commitTest1(ReplacesTester.java:35) 
    at sandbox.ReplacesTester$Proxy$_$$_WeldSubclass.commitTest1(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at org.jboss.weld.interceptor.proxy.SimpleInterceptionChain.interceptorChainCompleted(SimpleInterceptionChain.java:47) 
    at org.jboss.weld.interceptor.chain.AbstractInterceptionChain.invokeNextInterceptor(AbstractInterceptionChain.java:80) 
    at org.jboss.weld.interceptor.proxy.InterceptorInvocationContext.proceed(InterceptorInvocationContext.java:141) 
    at org.glassfish.cdi.transaction.TransactionalInterceptorBase.proceed(TransactionalInterceptorBase.java:132) 
    at org.glassfish.cdi.transaction.TransactionalInterceptorRequired.transactional(TransactionalInterceptorRequired.java:95) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at org.jboss.weld.interceptor.proxy.SimpleMethodInvocation.invoke(SimpleMethodInvocation.java:30) 
    at org.jboss.weld.interceptor.chain.AbstractInterceptionChain.invokeNext(AbstractInterceptionChain.java:93) 
    at org.jboss.weld.interceptor.chain.AbstractInterceptionChain.invokeNextInterceptor(AbstractInterceptionChain.java:78) 
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:48) 
    at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:41) 
    at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:53) 
    at sandbox.ReplacesTester$Proxy$_$$_WeldSubclass.commitTest1(Unknown Source) 
    at sandbox.ReplacesTester$Proxy$_$$_WeldClientProxy.commitTest1(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at javax.el.ELUtil.invokeMethod(ELUtil.java:326) 
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:536) 
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:269) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) 
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) 
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) 
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) 
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) 
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) 
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) 
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) 
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.ClassNotFoundException: sandbox.entities.Replaces 
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1761) 
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611) 
    ... 71 more 

解决。我找到了该怎么做。我使用以下代码连接到构造函数中的DB:

//Any transaction will be rolled back in this case
EntityManagerFactory emf = Persistence.createEntityManagerFactory("IRM2EE-ejbPU");    
entityManager = emf.createEntityManager();

我创建了无状态bean

@Stateless
@LocalBean
public class ConnectorDB {
    @PersistenceContext(unitName = "Sandbox-ejbPU", type=PersistenceContextType.TRANSACTION)
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    //Voodoo code, the program doesn't work without that
    //You will get Unable to retrieve EntityManagerFactory for unitName "Sandbox-ejbPU"
    @PostConstruct
    private void postConstructChores() {
        Replaces replaceEntry=entityManager.find(Replaces.class, 9138);
    }
}

我将其添加到托管bean中:

@EJB
private ConnectorDB connectorDB;

@PostConstruct
private void postConstructChores() {
    entityManager=connectorDB.getEntityManager();
}

1 个答案:

答案 0 :(得分:0)

您是否使用@Entity注释了Replaces.class?