Assume the following
- My username is somemail@mail.com and this mail could have one or many roles assigned(i.e. Administrator, Supervisor, Operative)
- You can only log in with one role at a time (that is, if you log in as an administrator, you should not load the role Supervisor, you only need to have the specific role information with which you are connected
I have two options:
1) Left the relationship between User-Role as OneToMany-ManyToOne in database tables and in jpa entities
@Entity
@Table(name = "User")
public class User{
//properties
@OneToMany
private Collection<Role> roles
//getters & setters
}
And
@Entity
@Table(name = "Role")
public class Role{
//properties
@ManyToOne
private User user;
//getters & setters
}
So when I need to retrieve the information from database through jpa I have to write something like this:
select user from User as user where user.mail='somemail@mail.com' and user.roles.id=1
2) Left the database tables as OneToMany and modify jpa relationship as a OneToOne
@Entity
@Table(name = "User")
public class User{
//properties
@OneToOne
private Role roles
//getters & setters
}
And
@Entity
@Table(name = "Role")
public class Role{
//properties
@OneToOne
private User user;
//getters & setters
}
So when I need to retrive the information from database throught jpa I have to write something like this:
select user from User as user where user.mail='somemail@mail.com' and user.role.id=1
It will return me a single row and I can retrive this user with something like this user.getRole(); (without the collection that in this specific case it seems unnecesary)
So my specific question is if is a good idea having differences between the designs (database and jpa) in cases like this that maybe in the database make sense have some type of relationships but not in java(JPA)?
According to this post It's a good idea to separate the two designs and work them separately but it not mention nothing about my specific question