I am writing a webapp that runs on Wildfly 8.2 and uses Hibernate 4. I've gotten it to successfully persist a new entity but cannot seem to make it commit changes to it afterwards. I'm kind of assumming it's some transaction setting that I have wrong, but I'm not sure what it is. I have a service layer, in which I set the transaction settings and that layer calls a DAO layer. Here's an example:
@Transactional
@EnableTransactionManagement
@TransactionManagement(value = TransactionManagementType.CONTAINER)
@TransactionAttribute(value = TransactionAttributeType.REQUIRED)
@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
@DeclareRoles("Security Admin")
public class SecurityServiceBean
{
@Override
@PermitAll
public UserRegistration confirmRegistration(
String confirmationCode) throws ApplicationException
{
try
{
QueryResults<UserRegistration> userRegistrations = this.userRegistrationDAO
.find(new UserRegistrationQuery(null, confirmationCode));
if (userRegistrations.getTotalRecords() == 1)
{
UserRegistration userRegistration = userRegistrations.uniqueResult();
if (userRegistration.getConfirmationDate() == null)
{
userRegistration.setConfirmationDate(new Date());
userRegistration.setState(State.CONFIRMED);
userRegistration = this.userRegistrationDAO.saveOrUpdate(userRegistration);
...
}
...
}
}
}
}
and the base DAO class has this
public abstract class AbstractJpaDataAccessObject implements DataAccessObject
{
public <T extends UniqueObject<?>> T saveOrUpdate(
T obj) throws DAOException
{
try
{
if (obj.getId() == null)
{
this.em.persist(obj);
}
else
{
T attached = this.em.merge(obj);
this.em.flush();
return attached;
}
return obj;
}
catch (PersistenceException e)
{
throw new DAOException("[saveOrUpdate] obj=" + obj.toString() + ",msg=" + e.getMessage(), e);
}
}
}
I know that I should not need the call to flush(), but I wanted to try it to see if that helped, which it did not.
So what am I missing?
UPDATE: There is no exception being thrown. The object being returned from SecurityServiceBean.confirmRegistration() has all of the changes that were made in the method. However, querying the database shows that the changes were not committed. None of my fields in the entity are marked as updateable=false. Below is an example. I'll limit the fields to just the "status" field which is one of the fields I expect to be updated.
@Entity
@Table(name = "user_registrations", schema = "campaigner")
public class UserRegistration extends AbstractUserRegistration
{
}
and the mapped superclass.
@MappedSuperclass
public class AbstractUserRegistration extends CampaignerHistoryObject<Long>
{
public static enum State {
UNCONFIRMED, CONFIRMED, APPROVED, DENIED,
};
private State state;
@Column(name = "STATE")
public State getState()
{
return state;
}
}
And here's two XML files that I use. The first is beanRefContent.xml
<beans>
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
<!--
SpringBeanAutowiringInterceptor needs this file.
We need SpringBeanAutowiringInterceptor to autowire the EJBs.
-->
<bean
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="classpath:campaignerContext.xml" />
</bean>
</beans>
The second is campaignerContext.xml.
<beans>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="campaigner" />
</bean>
<bean id="em" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
UPDATE 2: Now I'm starting to think that my problems lie in my persistence.xml file, shown below:
<persistence>
<persistence-unit name="campaigner" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/jdbc/CampaignerDS</jta-data-source>
...
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<property name="jta.UserTransaction" value="java:jboss/UserTransaction" />
<property name="jta.TransactionManager" value="java:jboss/TransactionManager" />
</properties>
</persistence-unit>
</persistence>