Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Problem with Bidirectional relationship and Cache
Problem with Bidirectional relationship and Cache [message #1232227] Thu, 16 January 2014 13:29 Go to next message
Noa Drach is currently offline Noa DrachFriend
Messages: 6
Registered: November 2012
Junior Member
This will be a long one because I tried to add all the relevant data...

I'm using eclipslink 2.4.2M3 and mysql 5.5
we use static weaving and Eager relations are not being weaved

I have the following entities:

  1. User
  2. Strategy
  3. Permission


  • User can have permissions on a Strategy.
  • A Strategy is Connected to at least an owner=User
  • A Strategy can be connected to an Assignee=User
  • Which means that a Strategy also has 2 members that can point to a User


the link between them is:
User->Permission - OneToMany - loaded lazily
Strategy->Permission - OneToMany - loaded lazily

Permission->User - ManyToOne
Permission->Strategy - ManyToOne

now i make sure handling all the Bidirectional relationships in all 3 entities and i have the following scenario.
my interaction with my app is done using REST.

the basic scenario is:
in my test i have 4 users
1. create a strategy
2. connect owner and maybe assignee
3. count how many strategies each of the 4 users has
4. change owner or assignee or both
5. count how many strategies each of the 4 users has
6. the test compares the results of steps 3 & 5 to see if it failed or not

now the test fails only in the 2 scenarios where both owner and assignee were replaced to users that didn't have permissions earlier.
in the db all the info appears correctly

  • Before - User A is the owner; After - User B is the owner and User C is the assignee
  • Before - User A is the owner and User B is the assignee; After - User C is the owner and User D is the assignee

The solution we have right now is to evict the user from the cache when we add a new permission.

But we don't like this solution because we don't know what caused the issue - so in our investigations we encountered another solution that actually increases the confusion:
If we change the relation between Strategy and permission to be loaded Eagerly the problem is solved - remember that Eager is not being cached...
but if we evict the Strategy from the cache instead of evicting the User the problem isn't solved.

I wanted to consult with you how to find the root cause of the problem given the 2 different solutions, because I can't understand how those 2 different solution have the same outcome.

thanks,
Noa
Re: Problem with Bidirectional relationship and Cache [message #1232718 is a reply to message #1232227] Fri, 17 January 2014 15:08 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
You haven't mentioned what the resutls actually are, or how you are changing the relationships. My guess though is that you have bidirectional relationships (User to Permission and Permission back to User) and are not maintaining both sides of the relationship when you make a change. If you change the Permission->User relationship, you must also change the User->Permission relationships or the User object remains out of sync with what you have in the database. This also goes for the Strategy->Permission and Permision->Strategy relationships.

Clearing the User from the cache is probably working for you as it forces it to be rebuilt from the database, the same as using em.refresh() would work. Neither are recommended though, as it is an extra database hit that could be avoided in code.

Best Regards,
Chris
Re: Problem with Bidirectional relationship and Cache [message #1233390 is a reply to message #1232718] Sun, 19 January 2014 13:46 Go to previous messageGo to next message
Noa Drach is currently offline Noa DrachFriend
Messages: 6
Registered: November 2012
Junior Member
I will add more info.

i do the creation of the strategy permissions inside a code that is activated by an invocation handler.

This is how the entity manager is created:
EntityManager proxyEm = (EntityManager) Proxy.newProxyInstance(this.getClass().getClassLoader(), em.getClass()
          .getInterfaces(), new EntityHookAwareInvocationHandler(em, hookSupport));


in the hook that is related to a change in Strategy I have the following code
    // Create a new permission
    StrategyPermission sp = new StrategyPermission();
    sp.setStrategy(strategy);
    sp.setUser(user);
    boolean added = false;

    // Connect the permission to the strategy
    if (strategy.getStrategyPermissions() == null)
      strategy.setStrategyPermissions(new HashSet<StrategyPermission>());

    if (!strategy.getStrategyPermissions().contains(sp)) {
      strategy.getStrategyPermissions().add(sp);
      added = true;
    }

    // Connect the permission to the user
    if (user.getStrategyPermissions() == null)
      user.setStrategyPermissions(new HashSet<StrategyPermission>());

    if (!user.getStrategyPermissions().contains(sp)) {
      user.getStrategyPermissions().add(sp);
      added = true;
    }

    if (added)
      em.persist(sp);

    ((JpaCache) em.getEntityManagerFactory().getCache()).evict(user, true);


Do you think i'm mishandling the bi-directional relationship?
Re: Problem with Bidirectional relationship and Cache [message #1235159 is a reply to message #1233390] Thu, 23 January 2014 17:33 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Possibly. You have made changes to user and strategy and have not merged them into the persistence unit. If the strategy and user objects shown in the code are not managed instances, changes made to them will not be put into the cache.

If they are managed, there would be no need to evict the user or strategy from the cache. The changes you've made would be merged into it when the transaction commits.
Previous Topic:MismatchedTokenException
Next Topic:Accessing foreign key on @ManyToOne - how to avoid DB roundtrip?
Goto Forum:
  


Current Time: Tue Apr 23 17:07:50 GMT 2024

Powered by FUDForum. Page generated in 0.02824 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top