-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Question on Entity Managers of the same persistent unit
PostPosted: Mon Sep 19, 2011 10:52 am 
Newbie

Joined: Mon Sep 19, 2011 10:42 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.