Hi,
Im currently trying to use spring with hibernate and jpa, but currently when I try to persist an entity is not been commited
My persistence.xml is the following
and my applicationContext.xml
and my classes are the following
bean
service
repository
I've check the logs and I see the transaction been commited but in the database is not appearing
INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'userService'
INFO: testingtest
INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.interceptor.Trans actionInterceptor#0'
INFO: org.springframework.transaction.annotation.Annotat ionTransactionAttributeSource - Adding transactional method 'createUser' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'transactionManager'
INFO: org.springframework.transaction.jta.JtaTransaction Manager - Creating new transaction with name [com.zuca.commons.core.application.service.UserServ ice.createUser]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Opening JPA EntityManager
INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Registering transaction synchronization for JPA EntityManager
INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Closing JPA EntityManager
INFO: org.springframework.transaction.jta.JtaTransaction Manager - Initiating transaction commit
My datasource in postgresql is org.postgresql.xa.PGXADataSource
Tks for your help!
Im currently trying to use spring with hibernate and jpa, but currently when I try to persist an entity is not been commited
My persistence.xml is the following
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="commons-pu">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.zuca.commons.core.domain.model.UserEntity</class>
<class>com.zuca.commons.core.domain.model.IdentificationTypeEntity</class>
<properties/>
</persistence-unit>
</persistence>
Code:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.zuca" />
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:jta-transaction-manager />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:appserver/TransactionManager"/>
</bean>
<!-- XA enabled Data source -->
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/paymentDS"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:data-source-ref="dataSource" p:persistence-xml-location="classpath:persistence.xml">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="database" value="POSTGRESQL" />
<property name="generateDdl" value="true"/>
</bean>
</property>
<property name="jpaProperties">
<value>
hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.SunONETransactionManagerLookup
hibernate.current_session_context_class=jta
hibernate.transaction.flush_before_completion=true
hibernate.connection.release_mode=auto
hibernate.transaction.jta.platform=org.hibernate.service.jta.platform.internal.SunOneJtaPlatform
hibernate.connection.autocommit=true
</value>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
bean
Code:
@ManagedBean (name ="registerUserBean")
@SessionScoped
@TransactionManagement(TransactionManagementType.CONTAINER)
public class RegisterUserBean implements Serializable {
private String username;
private String email;
private String password;
private String confirmPassword;
private IUserService userService;
private String errorMsgRegister;
public RegisterUserBean() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
userService = context.getBean("userService", IUserService.class);
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public String registerUser(){
UserDTO userDTO = new UserDTO();
userDTO.setUsername(this.username);
userDTO.setPassword(this.password);
userDTO.setEmail(this.email);
userDTO.setRegisterDate(new Date());
userService.createUser(userDTO);
return "login";
}
}
service
Code:
public interface IUserService {
void createUser(UserDTO ue) ;
}
@Service("userService")
@Transactional
public class UserService implements IUserService,Serializable {
/** The user repository. */
@Autowired
@Qualifier("userRepository")
private IUserRepository userRepository;
@Override
@Transactional
public void createUser (UserDTO ue) {
userRepository.createEntity(UserBuilder.getInstance().build(ue));
}
repository
Code:
public interface IUserRepository {
void createEntity(UserEntity entity);
}
@Repository("userRepository")
public class UserRepository implements IUserRepository {
@PersistenceContext
private EntityManager entityManager;
public void createEntity(UserEntity entity) {
entityManager.persist(entity);
}
}
INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'userService'
INFO: testingtest
INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.interceptor.Trans actionInterceptor#0'
INFO: org.springframework.transaction.annotation.Annotat ionTransactionAttributeSource - Adding transactional method 'createUser' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'transactionManager'
INFO: org.springframework.transaction.jta.JtaTransaction Manager - Creating new transaction with name [com.zuca.commons.core.application.service.UserServ ice.createUser]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Opening JPA EntityManager
INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Registering transaction synchronization for JPA EntityManager
INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Closing JPA EntityManager
INFO: org.springframework.transaction.jta.JtaTransaction Manager - Initiating transaction commit
My datasource in postgresql is org.postgresql.xa.PGXADataSource
Tks for your help!