jpa - Why entityManager.contains returns different results? -
This is in JPA2 (Eclipse Link) and JSF2.
I have a student of the unit class:
@Entity public class student serializable {Private stationary last long serialvarsian UID = 1L; @ Id @ Generated Values (strategy = generation type.eeto) Private long id; Private string firstname; Private string last name; Personal gaps ages; Public student (string firstname, string lastname, int era) {this.firstname = firstname; This .lastname = lastname; This.age = age; } Public Student () {} // Accessors and Mutes Here} Session Bean Student Fact which handles Effectfacade:
Public Essentials Comprehensive & lt ; T & gt; {Private Sector & lt; T & gt; EntityClass; Public Abstracts (class unit class) {this.entityClass = entityClass; } Secure Essential EntityManager getEntityManager (); Public Zero Generation (T organization) {getEntityManager (). Continues (unit); } Public T Edit (T unit) {return getEntityManager (). Merge (unit); } Public Zero Extraction (T Institution) {getEntityManager (). Remove (getEntityManager (). Merge (unit)); } Find Public T (Object ID) {return getEntityManager (). Find (entityClass, id); } // Other methods: search, all, find, count} @statelets public class studentfacade abstracts famed & lt; Students & gt; {@ PrecisionNettex (unit -name = "JPA2TestPU") Private Entity Manager; @ Override Protected EntityManager getEntityManager () {Return them; } Public student faculty () {super (student.class); } In Public Boolean (student) {return getEntityManager ()) Include; } Public Zero Testing Materials () {Student S = Search (1l); Boolean is = Meet the Entity Manager (). Include; }} This is my JSF managed vane:
@ManagedBean @RequestScoped public category index controller {@EJB Private StudentFamily StudentFac; / ** * Index Controller * / Public Index Controller () {} public string test () {creates a new example of student = new student ("John", "Do", 20); StudentFacade.create (s); Student s1 = studentFacade.find (1l); // This works because only 1 record boolean in the table; = Studentfacet Return tap; }} When I run the test () with Managed Bean, then the request is incorrect. But when testContains () is called in StudentFacade, isContains is true. Why is this?
Studentfood is a Stateless Session Bean (SSB). The content of its instance variable is not guaranteed to be preserved in the method call (). It is like having a different example of EntityManager created for each method invitation.
When you run your test from Managed Bean, you call for two different methods on SSB, so a separate EntityManager example is created for each call, and the student example in the second Not because it has not loaded yet.
But when you run your test inside a method of SSB, then the entire Entity Manager is used for the scope of the entire method, so the call () returns the return.
Comments
Post a Comment