How to implements getId() method with 2 Primary Key ?

Added by Eric MAIRET 5 months ago

Hello,

Every thing is in the title ! I get a table (RIGHT) with 2 FK, and I don't know how to implement getId() method.
This table is junction table, between 2 other tables linked by "many to many" relation.

 ----------            ----------              ----------
| User     |          |Right     |            |Zone      |
 ----------            ----------              ----------
|userId(PK)|----------|userId(FK)|------------|zoneId(PK)|
|login     |          |zoneId(FK)|            |label     |
|pwd       |          |label     |             ----------
 ----------            ----------

Usualy I implement getId() method like :

1 @Transient
2 public Integer getId() {
3   return this.getUserId();
4 }

But in the case of Right Table i don't know how to do ... I need help ;-) !


Replies

RE: How to implements getId() method with 2 Primary Key ? - Added by Oliver Gierke 5 months ago

Hey Eric,

this is actually rather a JPA issue than a Hades one :). Nevertheless there's help for you. Take a look at this Stack Overflow post.

In general it was one of the JPA 1.0 drawbacks not to address such scenarios appropriately. The good news is it is addressed in 2.0. So you might want to check out Mike Keith's article on DZone (section on derived identifiers), too.

You can use current 1.x Hades with JPA 2.0 by just updating to appropriate persistence provider's versions as well as explicitly stating JPA 2.0 as dependency and excluding 1.0. Or you use Hades 2.0-SNAPSHOT from our Maven repositories which already has the dependencies updated (which includes an update to Spring 3.0.1, too).

Hope that helps,
Ollie