aspose file tools*
The moose likes Object Relational Mapping and the fly likes JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA "inverse" annotation" Watch "JPA "inverse" annotation" New topic
Author

JPA "inverse" annotation

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

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

Joined: Jul 14, 2005
Posts: 304
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.


-Mike
Pro JPA 2: Mastering the Java Persistence API
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

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

Joined: Jul 14, 2005
Posts: 304
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

Joined: Nov 08, 2003
Posts: 3479

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

Joined: Jul 14, 2005
Posts: 304
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

Joined: Jan 29, 2005
Posts: 327
Deleted


We must know, we will know. -- David Hilbert
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA "inverse" annotation