简体   繁体   中英

Exception in thread “main” javax.persistence.RollbackException: Transaction marked as rollbackOnly

I'm using Spring and Hibernate in one of the applications that I'm working on and I've got a problem with handling of transactions.

I've got a service class that loads some entities from the database, modifies some of their values and then (when everything is valid) commits these changes to the database. In detail:

The error I am getting is this:

    févr. 12, 2016 8:24:36 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
Infos: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b7ec5d: startup date [Fri Feb 12 20:24:36 WAT 2016]; root of context hierarchy
févr. 12, 2016 8:24:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
Infos: Loading XML bean definitions from class path resource [application-context.xml]
févr. 12, 2016 8:24:36 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
Infos: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9096c6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,adrService,demandeService,rgService,utlService,myEMF,tr,org.springframework.aop.config.internalAutoProxyCreator,adrPC,utlPC,rgPC,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,org.springframework.aop.aspectj.AspectJPointcutAdvisor#5]; root of factory hierarchy
févr. 12, 2016 8:24:37 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
févr. 12, 2016 8:24:37 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.7.Final}
févr. 12, 2016 8:24:37 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
févr. 12, 2016 8:24:37 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:xe]
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=appACTEL, password=****, autocommit=true, release_mode=auto}
févr. 12, 2016 8:24:38 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
févr. 12, 2016 8:24:38 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
févr. 12, 2016 8:24:38 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
févr. 12, 2016 8:24:38 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
févr. 12, 2016 8:24:39 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Region :REGION2
Region :REGION1
févr. 12, 2016 8:24:40 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 904, SQLState: 42000
févr. 12, 2016 8:24:40 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-00904: "REGION2": invalid identifier

Exception in thread "main" javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:72)
    at com.springJPA.util.TransactionAspect.commit(TransactionAspect.java:14)
    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:601)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:622)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:604)
    at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:45)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
    at com.springJPA.service.RegionService$$EnhancerByCGLIB$$e6ae4f73.listeVille(<generated>)
    at com.springJPA.test.Main.main(Main.java:90)

Main.java:

package com.springJPA.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springJPA.domain.Adresse;
import com.springJPA.domain.Region;
import com.springJPA.domain.Utilisateur;
import com.springJPA.service.AdresseService;
import com.springJPA.service.RegionService;
import com.springJPA.service.UtilisateurService;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
        AdresseService adrService = (AdresseService) context.getBean("adrService");
        UtilisateurService utlService = (UtilisateurService) context.getBean("utlService");
        RegionService rgService = (RegionService) context.getBean("rgService");

        //--------
        Utilisateur utilisateur1 = new Utilisateur();
        utilisateur1.setNom("user1");
        utilisateur1.setNomUtilisateur("user1");
        utilisateur1.setMotdepasse("user1");

        utlService.ajoutUtilisateur(utilisateur1);

        Utilisateur utilisateur2 = new Utilisateur();
        utilisateur2.setNom("user2");
        utilisateur2.setNomUtilisateur("user2");
        utilisateur2.setMotdepasse("user2");

        utlService.ajoutUtilisateur(utilisateur2);
        //--------
        Adresse adresse = new Adresse();
        adresse.setRue("kantawi");
        adresse.setVille("Sousse");
        adresse.setPays("Tunisie");

        adrService.ajoutAdresse(adresse);
        //--------
        Region region1 = new Region();
        region1.setCodep(4000);
        region1.setNom_region("region1");
        region1.setNum_region(1);
        region1.setVille("ville1");
        region1.setNum_ville(1);

        rgService.ajoutRegion(region1);
        //--------
        Region region2 = new Region();
        region2.setCodep(4001);
        region2.setNom_region("region2");
        region2.setNum_region(2);
        region2.setVille("ville1");
        region2.setNum_ville(2);

        rgService.ajoutRegion(region2);

        //--------
        Region region3 = new Region();
        region3.setCodep(4003);
        region3.setNom_region("region3");
        region3.setNum_region(3);
        region3.setVille("ville1");
        region3.setNum_ville(2);

        Region region4 = new Region();
        region4.setCodep(4001);
        region4.setNom_region("region2");
        region4.setNum_region(3);
        region4.setVille("ville1");
        region4.setNum_ville(2);

        rgService.ajoutRegion(region4);


        List<String> listeRegion= rgService.listeRegion();
          for(String e:listeRegion) {
                 System.out.println("Region :"+e);
              }

          List<String> listeVille= rgService.listeVille("REGION2");
          for(String e:listeVille) {
                 System.out.println("Ville :"+e);
              }




    }

}

RegionService.java:

package com.springJPA.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.springJPA.domain.Region;
import com.springJPA.util.MyEntityManagerFactory;

@Component("rgService")
public class RegionService {
    @Autowired @Qualifier("myEMF")
    private MyEntityManagerFactory emf;
    private EntityManager entityManager;

    public void ajoutRegion(Region region) {
        entityManager = emf.getEntityManager();
        entityManager.persist(region);
    }

    public void modifRegion(Region region) {
        entityManager = emf.getEntityManager();
        entityManager.merge(region);

    }

    /**
     * supprimerRegion
     * @param region
     */
    public void supprimerRegion(Region region) {
        entityManager = emf.getEntityManager();
        entityManager.remove(region);

    }

        /**
         * listeRegion
         * @return listeRegion
         */
        public List<String> listeRegion() {
            entityManager = emf.getEntityManager();
            TypedQuery<String> query = entityManager.createQuery("Select DISTINCT UPPER(e.nom_region) from Region e", String.class);
            List<String> results = query.getResultList();
            return results;

        }

        public List<String> listeVille(String nomRegion) {
            entityManager = emf.getEntityManager();
            TypedQuery<String> query = entityManager.createQuery("Select DISTINCT UPPER(e.ville) from Region e where e.nom_region =:arg1 ", String.class);
            query.setParameter("arg1", nomRegion);
            List<String> results = query.getResultList();
            return results;

        }

        public EntityManager getEntityManager() {
            return entityManager;
        }

        public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
        }

        public MyEntityManagerFactory getEmf() {
            return emf;
        }

        public void setEmf(MyEntityManagerFactory emf) {
            this.emf = emf;
        }
    }

First of all: the errormessage in the title is not going to tell you what is wrong. It never does. This:

Exception in thread “main” javax.persistence.RollbackException: Transaction marked as rollbackOnly

is a generic errormessage, telling you that there was an error before during a DB operation , which makes this transaction ineligible for commiting.

The important errormessage in your case is this:

ERROR: ORA-00904: "REGION2": invalid identifier

which means that you are referencing a column called REGION2 that does not exist in your table . Check your SQL / Hibernate definition against the table to fix this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM