This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA "inverse" annotation

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
In Hibernate, we use "inverse" attribute to mark the side that is responsible for the relationship between to entities.
In JPA, what is the similar annotation for "inverse" (in the case it does exist)?
Thanks.
 
Mike Keith
author
Ranch Hand
Posts: 304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

In JPA we define relationships as having an "owning" side and an "inverse" side where the table of the owning entity contains the foreign key. Inverse means pretty much the same as how Hibernate users use it. The difference is that in JPA the inverse side specifies it through a "mappedBy" attribute in the mapping annotation.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I'm not woke up a zombie...

Please correct me if I'm wrong:
Team is the owning side and Player is the inverse side and Team is responsible for the relationship.
 
Mike Keith
author
Ranch Hand
Posts: 304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You have specified it incorrectly. Player is the owner and Team is the inverse side of this relationship. The code should look like:
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Player is the owner and Team is the inverse side of this relationship.

Player is the owner because it contains the FK to Team table, right?

So, according to your mapping, team is responsible of the relationship between the two entities, changes on team are propagated to players, but changes on players aren't, right?

One more thing, what is wrong in my original code?
Thank you Sir.
 
Mike Keith
author
Ranch Hand
Posts: 304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Player is the owner because it contains the FK to Team table, right?

So, according to your mapping, team is responsible of the relationship between the two entities, changes on team are propagated to players, but changes on players aren't, right?

One more thing, what is wrong in my original code?
Thank you Sir.

Yes, Player is the owner because it contains the FK, but I wouldn't say that Team is "responsible" for the relationship simply because it has a cascade set. It doesn't propagate changes. The changes to each object will be computed independently of the other objects. The cascade will just cause certain operations like persist() and remove(), etc. to be propagated.

I am not sure what is wrong with your original code because I can't see it. The little bit of code that was listed above had the mapping almost correct, it just forgot the target entity class.

public class Team {
...
@OneToMany(targetEntity=Player.class, mappedBy="team", cascade=CascadeType.ALL)
Set players;
...
}

I can't comment on other stuff that might be wrong in your code.
 
Joseph Sweet
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deleted
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic