我需要以原子方式在数据库中保留一些实体,但我遇到了一些困难......我有 4 个实体:Emitente、NotaFiscal、Item 和 Duplicata。实体 Item 和 Duplicata 依赖于 NotaFiscal,而 NotaFiscal 又依赖于 Emittente。所以,我的 dao 类尝试坚持 Emittente,然后是 NotaFiscal,然后是 Duplicata,最后是 Item。但是,当我尝试坚持 NotaFiscal 时,我收到一条错误消息,指出 Emittente 不存在。这些相同的实体使用 entityManager.getTransaction().begin(); 当这个程序是桌面时的方法......
这是调用所有 Dao 类的类:
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;
}
这是弹簧配置:
<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>
我能做些什么来解决它?