JPA / Hibernate does not release UPDATE for commit in EJB / Seam

I have a Seam 3 sandbox using JBoss 7, Hibernate as a standard JPA implementation, and as JSF as a web interface.

I have a problem that SQL UPDATE is swallowed by default.

My stateful conversation EJB supports extended EntityManager coverage and one Entity, managed by a transaction container (requires a new one)

  • EntityManager gets an injection
  • EJB uses EM to load Entity and stores it in a field
  • JSF application accesses EJB and its entity, changes String field
  • JSF Application Calls Save Method in EJB
  • In save (), I check if the Entities field has been changed -> it has been changed correctly
  • I do nothing else, the container completes the transaction after the save () is complete.
  • Problem: SQL update for the database is not performed.

If I continue save ():

a) entityManager.contains (entity) UPDATE is executed as expected (the result is "true")

OR

b) entityManager.persist (entity) UPDATE is executed as expected

Q: As far as I understand, specifications are not needed for either a) or b), because Entity remains manageable throughout the process. I do not understand why a) affects savings. I can depict b) affects conservation, but is it not necessary if it is?

Any explanation is appreciated.

Here is my EJB:

@Named
@ConversationScoped
@Stateful
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class LanguageBean {

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager em;
    @Inject
    private UserTransaction transaction;

    private Language value;

    @Inject
    Conversation conversation;

    public LanguageBean() {
        super();
    }

    @Begin
    public void selectLanguage(Long anId) {
        conversation.setTimeout(10 * 60 * 1000);
        if (anId != null) {
            value = em.find(Language.class, anId);
        }
    }

    @BeforeCompletion
    public void transactionComplete(){
        System.out.println("transactionComplete");
    }

    public Language getValue() {
        return value;
    }

    @Produces
    @Named
    @ConversationScoped
    public Language getLanguage() {
        return getValue();
    }

    public void setValue(Language aValue) {
        value = aValue;
    }

    @End
    public String save() {
//      displays the changed attribute:
        System.out.println("save code: "+value.getCode());

//      why is either this required:
//      boolean tempContains = em.contains(value);
//      System.out.println("managed: "+tempContains);

//      or: why is persist required:
        em.persist(value);
        return "languages?faces-redirect=true";
    }

    @End
    public String cancel() throws SystemException {
        transaction.setRollbackOnly();
        return "languages?faces-redirect=true";
    }

}
+5
source share
2 answers

My experience is largely related to seam-2, but here it should be equally applicable.

JPA , .

, ( )

, , , , @user1187037 ( , , )

, , , , .

http://javalangblog.blogspot.co.uk/2010/04/flush-mode-conversation.html, ,

, .

EDIT: xml

<begin-conversation join="true" flush-mode="COMMIT" />

@Begin(flushMode=COMMIT)

, @End . , , , . , , .

:

http://docs.jboss.org/seam/3/persistence/3.0.0.Alpha1/reference/en-US/html_single/#d0e249 http://docs.jboss.org/seam/3/latest/api/org/jboss/seam/persistence/FlushModeType.html

+1

@Remove , @End.

@End bean. , persistence save(), .

0

All Articles