Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7031829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:51:15+00:00 2026-05-28T00:51:15+00:00

I need to persist some entities in database atomically, but I’m having some difficulties…

  • 0

I need to persist some entities in database atomically, but I’m having some difficulties…
I have 4 entities: Emitente, NotaFiscal, Item and Duplicata.
Entities Item and Duplicata depends on NotaFiscal that depends on Emitente.
So, my dao classes try persist Emitente then NotaFiscal then Duplicata and finally Item.
But when I try to persist NotaFiscal I receive an error saying that Emitente doesn’t exists.
These same entities was persisted correctly using entityManager.getTransaction().begin(); approach when this program was a desktop…

This is the class that call all Dao classes:

public class ControladorArmazenamentoNFeCompleta extends BaseDao implements
    IControladorArmazenamentoNotaFiscalCompleta {

private static final long serialVersionUID = 1L;
private IControladorArmazenamentoNFe armazenadorNFe;
private IControladorArmazenamentoElementoNFe<Item> armazenadorItem;
private IControladorArmazenamentoEmitente armazenadorEmitente;
private IControladorArmazenamentoElementoNFe<Duplicata> armazenadorDuplicata;
private String mensagemErro;

public ControladorArmazenamentoNFeCompleta(
        EntityManagerFactory entityManagerFactory) {
    super(entityManagerFactory);
}

public StatusArmazenamento criar(NotaFiscal bean) {
    if (armazenarEmitente(bean)) {
            if (armazenadorNFe.recuperarPelaChave(bean.getId()) == null) {
                if (armazenadorNFe.criar(bean)) {
                    if (armazenarDuplicatas(bean)) {
                        if (armazenarItens(bean)) {                             
                            return StatusArmazenamento.SUCESSO;
                        } else {
                            mensagemErro = armazenadorItem
                                    .getMensagemErro();
                        }
                    } else {
                        mensagemErro = armazenadorDuplicata
                                .getMensagemErro();
                    }
                } else {
                    mensagemErro = armazenadorNFe.getMensagemErro();
                }
            } else {
                mensagemErro = "A nota fiscal já foi importada!";
                return StatusArmazenamento.IMPORTADA_ANTERIORMENTE;
            }
        }
    return StatusArmazenamento.ERRO;
}

private boolean armazenarEmitente(NotaFiscal bean) {
    if (armazenadorEmitente.recuperarPeloId(bean.getEmitente()
            .getNumeroCadastroNacional()) == null) {
        if (!armazenadorEmitente.criar(bean.getEmitente())) {
            mensagemErro = armazenadorEmitente.getMensagemErro();
            return false;
        }
    }
    return true;
}

private boolean armazenarDuplicatas(NotaFiscal notaFiscal) {
    armazenadorDuplicata.setChaveNotaFiscal(notaFiscal.getId());
    Cobranca cobranca = notaFiscal.getCobranca();
    if (cobranca != null) {
        for (Duplicata duplicata : cobranca.getListaDeDuplicatas()) {
            if (!armazenadorDuplicata.criar(duplicata))
                return false;
        }
    }
    return true;
}

private boolean armazenarItens(NotaFiscal notaFiscal) {
    armazenadorItem.setChaveNotaFiscal(notaFiscal.getId());
    if (notaFiscal.getListaDeItens() == null)
        return false;
    for (Item item : notaFiscal.getListaDeItens()) {
        if (!armazenadorItem.criar(item))
            return false;
    }
    return true;
}

and this is the spring configuration:

<bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager" />

<tx:advice id="txDao">
    <tx:attributes>
        <tx:method name="recuperar*" read-only="true" />
        <tx:method name="criar" rollback-for="PreexistingEntityException,Exception"/>
        <tx:method name="editar"
            rollback-for="IllegalOrphanException,NonexistentEntityException,Exception"/>
    </tx:attributes>
</tx:advice>

<tx:advice id="txControlador">
    <tx:attributes>
        <tx:method name="criar" rollback-for="Exception"/>
        <tx:method name="atualizar" rollback-for="Exception"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="operacoesDao"
        expression="execution(* com.hrgi.persistencia.dao.*.*(..))" />
    <aop:pointcut id="operacoesCadastroDao"
        expression="execution(* com.hrgi.persistencia.cadastro.dao.interfaces.*.*(..))" />
    <aop:pointcut id="operacoesBancoDao"
        expression="execution(* com.hrgi.persistencia.banco.dao.interfaces.*.*(..))" />
    <aop:pointcut id="operacoesNFeDao"
        expression="execution(* com.hrgi.persistencia.nfe.dao.interfaces.*.*(..))" />
    <aop:pointcut id="operacoesControladorArmazenamento"
        expression="execution(* com.hrgi.persistencia.*.*(..))" />
    <aop:pointcut id="operacoesControladorArmazenamentoNFe"
        expression="execution(* com.hrgi.persistencia.nfe.controladores.interfaces.*.*(..))" />

    <aop:advisor advice-ref="txDao" pointcut-ref="operacoesDao" />
    <aop:advisor advice-ref="txDao" pointcut-ref="operacoesCadastroDao" />
    <aop:advisor advice-ref="txDao" pointcut-ref="operacoesBancoDao" />
    <aop:advisor advice-ref="txDao" pointcut-ref="operacoesNFeDao" />
    <aop:advisor advice-ref="txControlador"
        pointcut-ref="operacoesControladorArmazenamento" />
    <aop:advisor advice-ref="txControlador"
        pointcut-ref="operacoesControladorArmazenamentoNFe" />
</aop:config>

what could I do to solve it??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:51:16+00:00Added an answer on May 28, 2026 at 12:51 am

    I solved the problem simply changing propagation:

    <tx:advice id="txDao">
        <tx:attributes>
            <tx:method name="recuperar*" read-only="true" />
            <tx:method name="criar" rollback-for="PreexistingEntityException,Exception"
                propagation="REQUIRED" />
            <tx:method name="editar"
                rollback-for="IllegalOrphanException,NonexistentEntityException,Exception"
                propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    
    <tx:advice id="txControlador">
        <tx:attributes>
            <tx:method name="criar" rollback-for="Exception"
                propagation="REQUIRES_NEW" />
            <tx:method name="atualizar" rollback-for="Exception"
                propagation="REQUIRES_NEW" />
        </tx:attributes>
    </tx:advice>
    

    Thanks for your attention.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using mapreduce and I need to persist some entities when they are not
I have a set of configuration items I need to persist to a human
I have an array of bytes in my C# class, and need to persist
I have an app where I need a BOOL value to persist across launches.
I have the following setup, and I need to know how to persist state.
need ask you about some help. I have web app running in Net 2.0.
I have a console application which screen scrapes some data, and now I need
I have a mapped POJO which I need to persist. In that POJO I
I think I need some help understanding how static objects persist in an ASP.Net
I have a Windows Phone application I am developing that needs to persist some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.