samedi 30 mai 2015

JPA (hibernate) does not insert on persist, needs to be flushed

I have a stateless bean that observes an event and save a record:

@Stateless
public class Manager {
    @Inject
    Repository repository;

    Manager() {}

    @Inject
    Manager(Repository repository) {
        this.repository = repository;
    }

    public void EventHandler(@Observes MyEvent myEvent) {
        save(event.obj);
    }

    private save(Object object) {
        repository.add(object);
    }
}

My repository is like this:

@Stateless
public class Repository {
    @PersistenceContext
    EntityManager em;

    Repository() {}

    public void add(Object object) {
        em.persist(object);
        // em.flush();  <---
    }
}

It doesn’t work unless I uncomment the line The thing I don’t understand is why do I need to flush! Shouldn’t the transaction commit automatically?

Aucun commentaire:

Enregistrer un commentaire