1

我有一个我认为是一个简单的项目,我在其中使用 Spring 来管理 JPA 和 EclipseLink 用于我的持久性单元。我将 JavaFX 用于我的前端,并将我的项目拆分为客户端模块、通用模块和服务器模块。我正在使用 Maven 进行构建。几天来,我一直在用头撞墙,试图弄清楚这一点。我尝试了持久性单元和 servlet 配置的不同配置,但似乎没有任何效果。并且错误似乎总是非常相似:事务管理器配置或使用中的某些东西是可疑的。

非常感谢所有帮助。我是这个 Spring JPA 空间的新手,如果问题没有得到充分定义,请原谅。

这是我的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.lamtec</groupId>
<artifactId>NewTrialServer</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<name>NewTrialServer</name>
<!-- Spring Web MVC -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>unknown-jars-temp-repo</id>
        <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
        <url>file:${project.basedir}/lib</url>
    </repository>
    <repository>
        <url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url>
        <id>eclipselink</id>
        <layout>default</layout>
        <name>Repository for library EclipseLink (JPA 2.0)</name>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.lamtec</groupId>
        <artifactId>NewTrialCommon</artifactId>
        <version>1.0</version>
    </dependency>

    <!-- Spring Web MVC -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>

    <!-- Spring data -->

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.0.2.RELEASE</version>
    </dependency>

    <!-- JPA -->

    <dependency>
        <groupId>org.hibernate.java-persistence</groupId>
        <artifactId>jpa-api</artifactId>
        <version>2.0-cr-1</version>
    </dependency>

    <!-- Hibernate -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.8.Final</version>
    </dependency>

    <!-- HSQL DB -->

    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.10</version>
    </dependency>

    <!-- Injection framework -->

    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.3.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.3</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.3.2</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
  <persistence-unit name="NewTrialsPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>NewTrials</jta-data-source>
    <class>com.lamtec.newtrial.service.Trial</class>
    <class>com.lamtec.newtrial.service.TrialCtWtCheck</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

这是我的小服务程序:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:data="http://www.springframework.org/schema/data/jpa"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:annotation-config/>
<context:component-scan base-package="com.lamtec"/>

<bean name="trialServiceImpl" class="com.lamtec.newtrial.service.TrialServiceImpl"/>

<bean name="/new-trial.service" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    <property name="service" ref="trialServiceImpl"/>
    <property name="serviceInterface" value="com.lamtec.newtrial.service.TrialService"/>
</bean>

<!-- Database Setup -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="NewTrialsPU" />
</bean>
<data:repositories base-package="com.lamtec.newtrial.service.repository"/>

<!-- Transaction Setup -->
<tx:annotation-driven/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean> 
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

我有两个实体类,试用:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lamtec.newtrial.service;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity
@Table(name = "Trial")
public class Trial implements Serializable{
private static final long serialVersionUID = 1L;
// @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Id
@Basic(optional = false)
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "TRIAL_ID")
private Long trial_Id;
@Column(name = "AUX_ADHESIVE1_WEIGHT")
private String auxAdhesive1Weight;
@Column(name = "AUX_ADHESIVE2_WEIGHT")
private String auxAdhesive2Weight;
@Column(name = "AUX_ADHESIVE_1")
private String auxAdhesive1;
@Column(name = "AUX_ADHESIVE_2")
private String auxAdhesive2;
@Column(name = "CT_WT_RANGE")
private String ctWtRange;
@Column(name = "DRIVE")
private String drive;
@Column(name = "DRIVE_WEIGHT")
private String driveWeight;
@Column(name = "FINISH_WT_ACCEPTABLE_RANGE")
private String finishWtAcceptableRange;
@Basic(optional = false)
@Column(name = "LAMINATOR")
private String laminator;
@Column(name = "LOWER_ADHESIVE")
private String lowerAdhesive;
@Column(name = "LOWER_ADHESIVE_WEIGHT")
private String lowerAdhesiveWeight;
@Column(name = "LOWER_AUX_WEIGHT")
private String lowerAuxWeight;
@Column(name = "LOWER_AUXILLIARY")
private String lowerAuxilliary;
@Column(name = "LOWER_UNWIND")
private String lowerUnwind;
@Column(name = "LOWER_UNWIND_WEIGHT")
private String lowerUnwindWeight;
@Column(name = "TARGET_CT_WEIGHT")
private String targetCtWeight;
@Column(name = "TARGET_TOTAL_FINISHED_WEIGHT")
private String targetTotalFinishedWeight;
@Column(name = "TRIAL_DATE")
private String trialDate;
@Basic(optional = false)
@Column(name = "TRIALNO")
private String trialno;
@Lob
@Column(name = "TRIAL_NOTES")
private String trialNotes;
@Column(name = "TRIAL_START_TIME")
private String trialStartTime;
@Column(name = "UPPER_ADHESIVE")
private String upperAdhesive;
@Column(name = "UPPER_ADHESIVE_WEIGHT")
private String upperAdhesiveWeight;
@Column(name = "UPPER_AUX_WEIGHT")
private String upperAuxWeight;
@Column(name = "UPPER_AUXILLIARY")
private String upperAuxilliary;
@Column(name = "UPPER_UNWIND")
private String upperUnwind;
@Column(name = "UPPER_UNWIND_WEIGHT")
private String upperUnwindWeight;
@Column(name = "YARN")
private String yarn;
@Column(name = "YARN_WEIGHT")
private String yarnWeight;
@OneToMany(cascade={CascadeType.ALL}, mappedBy="trialId", targetEntity=TrialCtWtCheck.class, fetch= FetchType.EAGER)
//    @JoinColumn(name="ID")
private List<TrialCtWtCheck> trialCtWtCheckList;
public List<TrialCtWtCheck> getTrialCtWtCheckList() {
    return trialCtWtCheckList;
}

public void setTrialCtWtCheckList(List<TrialCtWtCheck> trialCtWtCheckList) {
    this.trialCtWtCheckList = trialCtWtCheckList;
}


public Trial() {
    super();
}

public Trial(Trial t) {
   //        this.trial_Id = t.getTrialId();
        this.auxAdhesive1Weight = t.getAuxAdhesive1Weight();
    this.auxAdhesive2Weight = t.getAuxAdhesive2Weight();
    this.auxAdhesive1 = t.getAuxAdhesive1();
    this.auxAdhesive2 = t.getAuxAdhesive2();
    this.ctWtRange = t.getCtWtRange();
    this.drive = t.getDrive();
    this.driveWeight = t.getDriveWeight();
    this.finishWtAcceptableRange = t.getFinishWtAcceptableRange();
    this.laminator = t.getLaminator();
    this.lowerAdhesive = t.getLowerAdhesive();
    this.lowerAdhesiveWeight = t.getLowerAdhesiveWeight();
    this.lowerAuxWeight = t.getLowerAuxWeight();
    this.lowerAuxilliary = t.getLowerAuxilliary();
    this.lowerUnwind = t.getLowerUnwind();
    this.lowerUnwindWeight = t.getLowerUnwindWeight();
    this.targetCtWeight = t.getTargetCtWeight();
    this.targetTotalFinishedWeight = t.targetTotalFinishedWeight;
    this.trialDate = t.getTrialDate();
    this.trialno = t.getTrialno();
    this.trialNotes = t.getTrialNotes();
    this.trialStartTime = t.getTrialStartTime();
    this.upperAdhesive = t.getUpperAdhesive();
    this.upperAdhesiveWeight = t.getUpperAdhesiveWeight();
    this.upperAuxWeight = t.getUpperAuxWeight();
    this.upperAuxilliary = t.getUpperAuxilliary();
    this.upperUnwind = t.getUpperUnwind();
    this.upperUnwindWeight = t.getUpperUnwindWeight();
    this.yarn = t.getYarn();
    this.yarnWeight = t.getYarnWeight();
    this.trialCtWtCheckList = t.getTrialCtWtCheckList();
}

...  and getters and setters....

}

和 TrialCtWtCheck:

package com.lamtec.newtrial.service;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;


@Entity
@Table(name = "TrialCtWtCheck")
@XmlRootElement

public class TrialCtWtCheck implements Serializable {
private static final long serialVersionUID = 1L;
// @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Id
@Basic(optional = false)
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "COAT_WEIGHT")
private String coatWeight;
@Column(name = "FINISHED_MATERIAL_WT")
private String finishedMaterialWt;
@Column(name = "RAW_MATERIAL_TOT")
private String rawMaterialTot;
@ManyToOne
@JoinColumn(name="TRIAL_ID")
private Trial trialId;
public Trial getTrialId() {
    return trialId;
}

public void setTrialId(Trial trialId) {
    this.trialId = trialId;
}

public TrialCtWtCheck() {
    super();
}

public TrialCtWtCheck(Long id, String coatWeight, String finishedMaterialWt, String rawMaterialTot, Trial trialId) {
    this.id = id;
    this.coatWeight = coatWeight;
    this.finishedMaterialWt = finishedMaterialWt;
    this.rawMaterialTot = rawMaterialTot;
}

public TrialCtWtCheck(Long id) {
    this.id = id;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getCoatWeight() {
    return coatWeight;
}

public void setCoatWeight(String coatWeight) {
    this.coatWeight = coatWeight;
}

public String getFinishedMaterialWt() {
    return finishedMaterialWt;
}

public void setFinishedMaterialWt(String finishedMaterialWt) {
    this.finishedMaterialWt = finishedMaterialWt;
}

public String getRawMaterialTot() {
    return rawMaterialTot;
}

public void setRawMaterialTot(String rawMaterialTot) {
    this.rawMaterialTot = rawMaterialTot;
}


@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof TrialCtWtCheck)) {
        return false;
    }
    TrialCtWtCheck other = (TrialCtWtCheck) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.lamtec.newtrials.entity.TrialCtWtCheck[ id=" + id + " ]";
}

}

如下所示的存储库文件:

package com.lamtec.newtrial.service.repository;

import com.lamtec.newtrial.service.Trial;
import org.springframework.data.repository.CrudRepository;


public interface TrialRepository extends CrudRepository<Trial, Long> {

}

我的服务:

package com.lamtec.newtrial.service;

import com.lamtec.newtrial.service.repository.TrialRepository;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Repository
@Transactional(readOnly=true)
public class TrialServiceImpl implements TrialService {
  @Inject private TrialRepository trialRepository;

  @Override
  public void addTrial(Trial trial) {
      trialRepository.save(trial);
  }

  @Override
  @Transactional(readOnly=true)
  public List<Trial> listTrials() {
      return (List<Trial>)trialRepository.findAll();
  }
  @Override
  public Trial findTrial(Long Id) {
      return trialRepository.findOne(Id);
  }

  @Override
  @Transactional(readOnly=false)
  public void updateTrial(Trial trial) {
      trialRepository.save(trial);
  }

}

war 文件在 GlassFish 3.1.2.2 服务器上成功构建和部署。我的开发环境是 NetBeans 7.2。

当我针对该服务运行我的客户端时,我得到以下堆栈跟踪:

org.springframework.remoting.RemoteAccessException: Could not deserialize result from HTTP invoker remote service [http://localhost:8080/NewTrialServer/new-trial.service]; nested exception is java.lang.ClassNotFoundException: org.springframework.transaction.CannotCreateTransactionException
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.convertHttpInvokerAccessException(HttpInvokerClientInterceptor.java:208)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:145)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy7.listTrials(Unknown Source)
at com.lamtec.newtrialclient.app.TrialsUIPresenter$1.call(TrialsUIPresenter.java:540)
at com.lamtec.newtrialclient.app.TrialsUIPresenter$1.call(TrialsUIPresenter.java:536)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1229)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: org.springframework.transaction.CannotCreateTransactionException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:622)
at org.springframework.core.ConfigurableObjectInputStream.resolveClass(ConfigurableObjectInputStream.java:79)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.doReadRemoteInvocationResult(AbstractHttpInvokerRequestExecutor.java:290)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.readRemoteInvocationResult(AbstractHttpInvokerRequestExecutor.java:241)
at org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor.doExecuteRequest(CommonsHttpInvokerRequestExecutor.java:143)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.executeRequest(AbstractHttpInvokerRequestExecutor.java:136)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:192)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:174)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:142)
... 9 more

我在堆栈跟踪中没有看到的一件事,我在客户端和服务器之间的有线通信中看到的是:

    4317 [Thread-4] DEBUG httpclient.wire.content  - << "Exception Description: Cannot use an EntityTransaction while using JTA."

这是新的堆栈跟踪:

org.springframework.remoting.RemoteAccessException: Could not deserialize result from HTTP invoker remote service [http://localhost:8080/NewTrialServer/new-trial.service]; nested exception is java.lang.ClassNotFoundException: org.springframework.transaction.CannotCreateTransactionException
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.convertHttpInvokerAccessException(HttpInvokerClientInterceptor.java:208)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:145)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy7.listTrials(Unknown Source)
at com.lamtec.newtrialclient.app.TrialsUIPresenter$1.call(TrialsUIPresenter.java:540)
at com.lamtec.newtrialclient.app.TrialsUIPresenter$1.call(TrialsUIPresenter.java:536)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1229)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: org.springframework.transaction.CannotCreateTransactionException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:622)
at org.springframework.core.ConfigurableObjectInputStream.resolveClass(ConfigurableObjectInputStream.java:79)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.doReadRemoteInvocationResult(AbstractHttpInvokerRequestExecutor.java:290)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.readRemoteInvocationResult(AbstractHttpInvokerRequestExecutor.java:241)
at org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor.doExecuteRequest(CommonsHttpInvokerRequestExecutor.java:143)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.executeRequest(AbstractHttpInvokerRequestExecutor.java:136)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:192)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:174)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:142)
... 9 more

在服务器和 IDE(我在其中启动客户端)之间的消息流量是:

"Could not open JPA EntityManager for transaction"
4

3 回答 3

0

看起来您的交易类型不匹配,可能是由于配置。我认为您正在使用 JTA 和基于实体的事务的组合。这整个区域有点混乱,但我认为除非您查询多个数据库等,否则您可以将 JTA 排除在外,而只需使用简单的基于实体的事务管理(基本上是标准的数据库级事务)。

我对 EclipseLink 了解不多,但尝试使用使用 RESOURCE_LOCAL 的 persistence.xml。也许是这样的:http ://www.vogella.com/articles/JavaPersistenceAPI/article.html#example_persistenceunit

如果您真的想使用完整的 JTA,那么您可能可以保留您的 persistence.xml 并更改 EntityManager 的配置以使用 JtaTransactionManager。为此,您必须在网上搜索更多详细信息。

作为旁注,“ClassNotFoundException”有点误导。这只是客户端尝试反序列化来自服务器的真正异常时抛出的内容。真正的例外是CannotCreateTransactionException,它通过线路发送到您的客户端,但客户端在其类路径中没有此JAR,因此无法反序列化它,然后您会看到ClassNotFoundException。

在标准设置中,我认为您会在服务器日志中看到真正的异常。如果没有,您可以在服务器方法的主体周围添加一个 try-catch,捕获所有内容并记录它(或围绕您的服务类包装一个方面,以神奇地做到这一点)。

或者,您可以将 spring 持久性和 hibernate jar 添加到客户端的类路径(通过将它们添加到 POM)。这会增加客户端的膨胀,但允许客户端反序列化异常,然后记录完整的堆栈跟踪等。

希望有帮助,宗斯基

于 2012-07-28T08:06:13.807 回答
0
Caused by: java.lang.ClassNotFoundException: org.springframework.transaction.CannotCreateTransactionException

似乎创建交易时出错。这可能是由于persistance.xml或 glassfish 配置文件中的配置错误。

<persistence-unit name="NewTrialsPU" transaction-type="JTA">

EntityManagerFactory无法从 JNDI 找到事务。我没有那么多用玻璃鱼。但这应该是找到实际问题的线索。应该有某种方法来验证是否在 glassfish 中创建了 JNDI 资源并相应地使用它。

编辑

对于快速解决方案,请考虑使用transaction-type="RESOURCE_LOCAL"并提供相关database connection的数据库username信息password应该会有所帮助。

于 2012-07-26T17:52:42.897 回答
0

根据异常,您的配置似乎与perrsistence.xmlJTAApplication-context.xml事务不兼容。你可以看到下面的配置,我在 JBoss AS 服务器中使用了 JpaTransaction 管理器来处理事务和数据源。

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:jboss/datasources/testDS" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="restUnit" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="persistenceUnitManager">
            <bean
                class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
                <property name="defaultDataSource" ref="dataSource" />
            </bean>
        </property>
        <property name="dataSource" ref="dataSource" />
    </bean>



    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
        id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <context:spring-configured />
于 2014-11-24T12:16:17.897 回答