1

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>
Gary Kephart
  • 4,860
  • 5
  • 39
  • 52
  • What behavior or exception do you get when you call the merge? The stack trace or at least the underlying exception is helpful. – Brod May 16 '16 at 18:20
  • Just check if any of your fields in your entity is marked as updatable=false – shankarsh15 May 16 '16 at 18:45
  • Please see this post http://stackoverflow.com/questions/24922294/hibernate-entitymanager-merge-does-not-update-database – shankarsh15 May 16 '16 at 19:02
  • I am using container-managed transactions (CMT), not user-managed transactions, so I can't use em.getTransaction() – Gary Kephart May 16 '16 at 20:14
  • Updated post with more info. – Gary Kephart May 16 '16 at 21:09
  • Just to confirm, you are querying the database after the transaction has committed? And you are sure that it has committed, not rolled back? – Dragan Bozanovic May 17 '16 at 08:54
  • Yes, I am querying the database afterwards. Right now, I'm testing this out with HtmlUnit and after the browser calls, I check to see that the registration has been confirmed by fetching it from the database via a remote EJB call. I don't know if there's been a rollback. I don't think so since I have logging set to org.hibernate.SQL=DEBUG and org.hibernate.type=TRACE and have seen neither an update nor a rollback. – Gary Kephart May 17 '16 at 14:02
  • Updated post with persistence.xml file – Gary Kephart May 17 '16 at 22:07

0 Answers0