• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JPA "inverse" annotation

 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • 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.
 
author
Posts: 304
  • 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
Posts: 3479
Mac Objective C Clojure
  • 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
Posts: 304
  • 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
Posts: 3479
Mac Objective C Clojure
  • 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
Posts: 304
  • 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.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deleted
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic