Object not fetch well in java

I have a problem with fetching objects in Java. There is object A which is Entity bean class and it contains object B (other Entity bean class) as a field. Object A was created without B and later object B was attached to A with some action. Problem is that some users can not see object B in object A, it looks like null. In database everything is fine and after some time object A looks good (contains B). This happens only in some cases, in other object looks good from begin. I want to say that we use two virtual machines for running application (maybe it can be problem) and Oracle Application Server. Any help? Stojan

Code looks like: Entity class A is defined like this (B is also entity class).

 @Entity
 @Table(name = "A")
 public class A{
     ...
     @ManyToOne
     @JoinColumn(name="ID_B", referencedColumnName="ID")
     private B b;
     ...
 }

Object of class A is created in some action, for example:

public createA_action(){
     A a = new A();
     saveObject(a);
}

where 'saveObject' is some method that persist or merge object in database.

Later, there is call of other method, like this:

public addBtoA_action(){
     A a = getA();
     B b = getB();
     a.setB(b);
     saveObject(a);
}

where getA() and getB() get an existing objects.

Answers


Well, each entityManager (session in hibernate) has his own in-memory instances of the entity. Generally you have one EntityManager per transaction and one transaction per http-request.

So if One entity instance was loaded in a given transaction t1 and then loaded in another transaction t2, if you set the 'a' field on this entity in t1, then you won't be able to see the modification in t2 until t1 is commited * and the EntityManager.refresh() method is called on the given entity.

* Transaction isolation level in JPA is read_commited


I resolved the problem. Like I said before, I am using two virtual machines. Like Gab said before, every virtual machine has it's own cache. Scenario: user1 is using virtual machine 1 (VM1) and the other user (user2) is using VM2 and both of them see created object (after first transaction). After that user1 changes the object (second transaction). User2 will not see that change because cache of VM2 have an old object. Solution: I am using NativeQuery to get the ids from database and method that get object by id. I put EntityManager.refresh() in the method that got the object from the database and everything works fine. So, before change, I got objects from the ids with method like:

public Object getObj(Long id, Class class){
    return em.find(class, id);
}

After the change, this method looks like:

public Object getObj(Long id, Class class){
    Object obj = em.find(class, id);
    em.refresh(obj);
    return obj;
}

Need Your Help

JPA - SQLException create record

java oracle jpa netbeans eclipselink

I have imported this entity classes from database (with Netbeans function - Entity Classes from Database).

Asp.net MVC - Can I load a view from a different view folder?

asp.net-mvc

In my app I have a need to load the same view from two different controllers without placing the view in the shared views directory.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.