Assume an entity class as follows (trivial):
Code:
@Entity
@Table(name="EVENTS")
public class Event {
@Id @GeneratedValue
@Column(name="EVENT_ID")
private Long id;
@Column(name="EVENT_TEXT")
private String text;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="NEXT_EVENT_ID")
private Event nextEvent;
}
If I do the following:
Event evt = new Event("Incoming Event");
saveObject(evt);
where saveObject is:
Code:
public static void saveObject(Object o){
emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(o);
tx.commit();
em.close();
}
If I do a query after the save object but the entity manager of the save is not the same as the entity manager for the query, e.g. I do
Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("events");
before doing a select all events, the query returns no result.
If I reuse the emf object, that persisted the object for the select query, then the query returns results.
I can not undertand this. Why does it work like that? I create a new entity manager
of the same persistent unit.
Am I wrong to expect to use a fresh instantiation of the entity manager?
Thanks