Java Persistence with Tertiary Relationship

H

Harry

Hi All,

As we know, we can perform relationship between two entities, using
@OneToOne, @OneToMany, @ManyToOne, @ManyToMany

Can I use JPA to persist a Tertiary Relationship?

For example, the relationship between User, Role and Module, which can
represent whether a User instance having a particular Role can access a
Module instance.

Thanks,
Harry
 
D

Danno

Hi All,

As we know, we can perform relationship between two entities, using
@OneToOne, @OneToMany, @ManyToOne, @ManyToMany

Can I use JPA to persist a Tertiary Relationship?

For example, the relationship between User, Role and Module, which can
represent whether a User instance having a particular Role can access a
Module instance.

Thanks,
Harry

That doesn't make sense
 
H

Harry

Can you elaborate more?
Which part doesn't make sense
and how to solve if I am in this case.
 
D

Daniel Pitts

Hi All,

As we know, we can perform relationship between two entities, using
@OneToOne, @OneToMany, @ManyToOne, @ManyToMany

Can I use JPA to persist a Tertiary Relationship?

For example, the relationship between User, Role and Module, which can
represent whether a User instance having a particular Role can access a
Module instance.

Thanks,
Harry

So, a User has a specific Roles, and the Roles allow access to
specific Modules?
Would that look like:

class User {
Collection<Role> roles;
// Extra user data.
}

class Role {
Collection<Module> accessableModules;
Collection<User> users;
// Extra role data
}

class Module {
// module data
}

So, this would be
User many-to-many Role
Role one-to-many Module

Hope this helps,
Daniel.
 
H

Harry

Thanks for that in advance
Perhaps it's my problem on not giving a good example.

What I mean is really when tertiary relationship exists ( I think you
should know tertiary does exist in ERD)
I try to use another example.
Person-Order-LineItem relationship

-----------------
| Line Item |
-----------------
|
-------------- | -----------
| Person | ------- ( ) ------ | Order |
-------------- -----------

In this case, Line Item instances appear when a relationship between
the Person and Order forms.
Hope my example does a good illustration.

Thanks for your help.
Harry
 
D

Danno

Thanks for that in advance
Perhaps it's my problem on not giving a good example.

What I mean is really when tertiary relationship exists ( I think you
should know tertiary does exist in ERD)
I try to use another example.
Person-Order-LineItem relationship

-----------------
| Line Item |
-----------------
|
-------------- | -----------
| Person | ------- ( ) ------ | Order |
-------------- -----------

A Person Order Line Item Relationship would look like this...

Person (1)------------(∞) Order (1)--------------(∞) LineItem

There would be no need for a "tertiary" relationship.
 
D

Danno

specific Modules?
Would that look like:

class User {
Collection<Role> roles;
// Extra user data.

}class Role {
Collection<Module> accessableModules;
Collection<User> users;
// Extra role data

}class Module {
// module data

}So, this would be
User many-to-many Role
Role one-to-many Module

Hope this helps,
Daniel.

I agree, I would have Module have access to roles, and roles not have
access to modules. But that's the same design I was thinking.
 
H

Harry

So, what you mean is to convert every tertiary relationship to two
relationships each in between two of the three entities?
Is it really possible to convert all kinds of relationships like this?

Thanks
Harry
 
D

Danno

Well, I wouldn't treat anything anyone says as religious doctrine.
So, to say, "convert ALL kinds of relationships like this" won't help
you out either. You have to model your ERD and object mappings based
on reality. You will find that this idea of tertiary relationship
where you have a prop table with 3 keys and nothing else will less
likely exist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top