javax.ejb.EJBException:事务已中止标记为回滚的事务

时间:2014-12-31 15:20:08

标签: java-ee transactions glassfish-4

我正在使用EJB开发JavaEE应用程序。

我的两个实体如下:

@Entity
public class Utente implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private int cod_utente; //BI
private String nome_utente;
private String morada_utente;
@Temporal(TemporalType.DATE)
private GregorianCalendar dnasc_utente;
private int tel_utente;
private ArrayList<GregorianCalendar> agenda;
@OneToMany
@Column(nullable=true)
private ArrayList<Prescricao> lista_presc;

@Entity
public class FichaClinica implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * 
 */
@Id
@GeneratedValue
private int cod_ficha;
@OneToOne
private Utente utente;
@OneToMany
@Column(nullable=true)
private ArrayList <AtoEnfermagem> lista_atos_enf;
@OneToMany
@Column(nullable=true)
private ArrayList <AtoMedico> lista_atos_medicos;
@OneToMany
@Column(nullable=true)
private ArrayList<Consulta> lista_consultas;

当我尝试使用以下方法创建实体 Utente FichaClinica 时:

@Override
public String cria_utente(String nome_utente, String morada_utente,GregorianCalendar dnasc_utente, int tel_utente) {
    Utente u=new Utente(nome_utente,morada_utente,dnasc_utente,tel_utente);
    em.persist(u);
    FichaClinica fc=new FichaClinica();
    em.persist(fc);
    fc.setUtente(u);
    return "Utente "+Integer.toString(u.getCod_utente())+" cirado!";
}

发生以下错误:

Exception in thread "main" javax.ejb.EJBException: Transaction aborted
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:725)
at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:212)
at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
at com.sun.proxy.$Proxy235.cria_utente(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 com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:143)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:173)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
Caused by: javax.transaction.RollbackException: Transaction marked for rollback.
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:490)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:854)
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:719)
... 24 more
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: An internal error occurred while accessing method [_persistence_set_lista_atos_enf] on class [class Entities.FichaClinica].
Internal Exception: java.lang.NoSuchMethodException: Entities.FichaClinica._persistence_set_lista_atos_enf(Entities.AtoEnfermagem)
Descriptor: RelationalDescriptor(Entities.FichaClinica --> [DatabaseTable(FICHACLINICA)])
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl$1.handleException(EntityManagerSetupImpl.java:696)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.handleException(AbstractSynchronizationListener.java:275)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:170)
at org.eclipse.persistence.transaction.JTASynchronizationListener.beforeCompletion(JTASynchronizationListener.java:68)
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:452)
... 26 more
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: An internal error occurred while accessing method [_persistence_set_lista_atos_enf] on class [class Entities.FichaClinica].
Internal Exception: java.lang.NoSuchMethodException: Entities.FichaClinica._persistence_set_lista_atos_enf(Entities.AtoEnfermagem)
at org.eclipse.persistence.exceptions.DescriptorException.errorAccessingSetMethodOfEntity(DescriptorException.java:410)
at org.eclipse.persistence.internal.indirection.WeavedObjectBasicIndirectionPolicy.getSetMethod(WeavedObjectBasicIndirectionPolicy.java:121)
at org.eclipse.persistence.internal.indirection.WeavedObjectBasicIndirectionPolicy.setRealAttributeValueInObject(WeavedObjectBasicIndirectionPolicy.java:176)
at org.eclipse.persistence.internal.indirection.WeavedObjectBasicIndirectionPolicy.setRealAttributeValueInObject(WeavedObjectBasicIndirectionPolicy.java:144)
at org.eclipse.persistence.internal.indirection.WeavedObjectBasicIndirectionPolicy.updateValueInObject(WeavedObjectBasicIndirectionPolicy.java:134)
at org.eclipse.persistence.internal.indirection.WeavedObjectBasicIndirectionPolicy.getRealAttributeValueFromObject(WeavedObjectBasicIndirectionPolicy.java:77)
at org.eclipse.persistence.mappings.ForeignReferenceMapping.getRealAttributeValueFromAttribute(ForeignReferenceMapping.java:1068)
at org.eclipse.persistence.mappings.DatabaseMapping.getRealAttributeValueFromObject(DatabaseMapping.java:786)
at org.eclipse.persistence.mappings.CollectionMapping.getRealCollectionAttributeValueFromObject(CollectionMapping.java:1142)
at org.eclipse.persistence.mappings.CollectionMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(CollectionMapping.java:473)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectBuilder.java:2514)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4207)
at org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectReferenceMapping.java:949)
at org.eclipse.persistence.mappings.ObjectReferenceMapping.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectReferenceMapping.java:927)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.cascadeDiscoverAndPersistUnregisteredNewObjects(ObjectBuilder.java:2514)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(UnitOfWorkImpl.java:4207)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.discoverUnregisteredNewObjects(RepeatableWriteUnitOfWork.java:305)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:723)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1516)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.issueSQLbeforeCompletion(UnitOfWorkImpl.java:3168)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.issueSQLbeforeCompletion(RepeatableWriteUnitOfWork.java:352)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:158)
... 28 more
Caused by: java.lang.NoSuchMethodException: Entities.FichaClinica._persistence_set_lista_atos_enf(Entities.AtoEnfermagem)
at java.lang.Class.getDeclaredMethod(Class.java:2122)
at org.eclipse.persistence.internal.security.PrivilegedAccessHelper.findMethod(PrivilegedAccessHelper.java:87)
at org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getMethod(PrivilegedAccessHelper.java:290)
at org.eclipse.persistence.internal.helper.Helper.getDeclaredMethod(Helper.java:1020)
at org.eclipse.persistence.internal.indirection.WeavedObjectBasicIndirectionPolicy.getSetMethod(WeavedObjectBasicIndirectionPolicy.java:97)
... 48 more

我还创建了一个persistence.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Hospital" transaction-type="JTA">
    <class>Entities.Medico</class>
    <class>Entities.Utente</class>
    <class>Entities.FichaClinica</class>
    <class>Entities.Enfermeiro</class>
    <class>Entities.AtoEnfermagem</class>
    <class>Entities.AtoMedico</class>
    <class>Entities.Consulta</class>
    <class>Entities.Prescricao</class>
    <class>Entities.Encomenda</class>
    <class>Entities.Produto</class>
    <class>Entities.Medicamento</class>
    <properties>
        <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
    </properties>
</persistence-unit>
</persistence>

有人可以帮我理解为什么会这样吗?

1 个答案:

答案 0 :(得分:-1)

我已经解决了这个问题。

问题在于arraylists。 Glassfish不支持这种类型的结构,因此我将其更改为Lists