Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@ManyToOne with @JoinTable

 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have a @ManyToOne with a @JoinTable ?

I have an Employee object with is @ManyToOne with a Role object. The mapping is defined in the EmployeeRoleMap table. How do I go about defining this.

And one more complication, EmployeeRoleMap has a composite key apart from the EmployeeId and the RoleId.
 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to get it to work. The mapping should have the actual table names and it doesn't matter if the join table has a composite key.

I just have one issue now. The employee object instead of returning just one record, return multiple because the empRoleMap has multiple records for this association. This is because the empRoleMap has a active_flag for which only one is active at a time.

How do I take this into consideration. Can I add the active_flag='Y' as a condition to the @JoinTable or do I use inheritance and if so how do I add that to the @JoinTable condition.


 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait a minute, it's a bigger mess. If there are 5 mappings with the current one marked active. The query will return 5 employee object with all 5 having the last role object.

If I remove the mapping i get just one employee object as expected.

Any idea on how to design this 'conditional' join table?
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using EclipseLink you could use an Expression to filter the relationship.

See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

Otherwise, maybe map it as a OneToMany or ManyToMany instead and just define a method on your class that returns the active instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic