Caso estrano com Persistencia JSF 2+Spring 3+JPA

4 respostas
W

Puts pessoal to com um problema estranho…
o trem tema configuração que mencionei no titulo, mas so faz perquisa e alteração, persistencia de um novo resgistro não faz… olhem minha configuração:
Entidade:

@Entity
@Table(name = PAIS)
public class Pais implements Identificavel<Long> {

/** */
private static final long serialVersionUID = 8615329059375862033L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = ID)
private Long id;

@NotNull(message=notnull)
@NotEmpty
@Label(lblPais)
@Column(name = NOME)
private String nome;

@NotNull(message=notnull)
@NotEmpty
@Label(lblSiglaPais)
@Column(name = SIGLA)
private String sigla;

@Column( name = PADRAO )
@Label( lblPadrao )
private boolean padrao;

@OneToMany(fetch = FetchType.LAZY, mappedBy = pais)
@OrderBy(value = nome)
private List<Estado> estados;

//metodos gets e sets
}



Metodos salvar e alterar:

#Código

@Component
public class PaisRepositoryImpl extends CrudRepositoRIO<Pais> implements
PaisRepositorio, Serializable {
// método alterar país
@Override
public Pais alterar(Pais entity) throws BusinessException {
Pais p = super.alterar(entity);
getEntityManager().flush();
return p;
}

// método inserir país
@Override
public Pais cadastrar(Pais pais) throws BusinessException {
// entityManager.getTransaction().begin();
// entityManager.persist(pais);
// entityManager.getTransaction().commit();
Pais p = super.cadastrar(pais);
return p;
}

}

Fachada:

@Component(controllerCadastro)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = BusinessException.class)
public class CadastroFacadeImpl implements CadastroFacade, Serializable {

private static final long serialVersionUID = -3831130939388543469L;

@Resource
PaisRepositorio repositorio;

@Override
public Pais cadastrarPais(Pais pais) throws Exception {
// UPPER CASE
pais.setNome(pais.getNome().toUpperCase());
pais.setSigla(pais.getSigla().toUpperCase());

return repositorio.cadastrar(pais);
}

@Override
public Pais alterarPais(Pais pais) throws Exception {

// UPPER CASE
pais.setNome(pais.getNome().toUpperCase());
pais.setSigla(pais.getSigla().toUpperCase());

return paisReposytory.alterar(pais);
}

config do Spring:

<?xml version=1.0 encoding=UTF-8?>

<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:tx=http://www.springframework.org/schema/tx
xmlns:util=http://www.springframework.org/schema/util
xmlns:jee=http://www.springframework.org/schema/jee
xsi:schemaLocation=
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd>

<context:annotation-config />
<tx:annotation-driven transaction-manager=txManager />	

<context:component-scan base-package=br.com.projeto />


<!-- Gerenciador de transacoes baseado em JPA -->
<bean id=txManager class=org.springframework.orm.jpa.JpaTransactionManager>
<property name=entityManagerFactory ref=entityManagerFactory />
</bean>
<!-- banco desenv -->

// dados do banco, que no caso é SQL SERVER


<!-- Fabrica de entity managers --> 
<bean id=entityManagerFactory class=org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
<property name=dataSource ref=myDataSource />
<property name=persistenceUnitName value=ilog />	
<property name=jpaVendorAdapter>
<bean class=org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
<property name=showSql value=true />
<property name=generateDdl value=true />
<property name=databasePlatform value=org.hibernate.dialect.SQLServerDialect />	
</bean>
</property> 
</bean>


<bean id=myAspect class=br.com.projeto.classe/>

<import resource=classpath:/META-INF/myview-spring-init.xml/>
<bean id=persistentContextFactory
class=org.apache.myfaces.orchestra.conversation.spring.JpaPersistenceContextFactory>
<property name=entityManagerFactory ref=entityManagerFactory/>
</bean>
</beans>

4 Respostas

W

gera uma saida que foi inserido:

Hibernate: insert into dbo.PAIS (NOME, PADRAO, SIGLA) values (?, ?, ?)

mas não commita no banco, ou seja, não insere

tumvoodoo

Você esta abrindo e fechando a transação no seu super ?

getEntityManager.getTransaction().begin()
getEntityManager.getTransaction().commit()

Também tenta usar o @Transactional do spring nos métodos de persistencia.

W

O Spring gerencia isso pra mim…

W

Qnd eu tbm coloco no metodo gera este erro:

org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is javax.persistence.PersistenceException: unexpected error when rollbacking

Criado 25 de maio de 2012
Ultima resposta 25 de mai. de 2012
Respostas 4
Participantes 2