I'd like to get people's opinion on the optimal way to map the following:
I have an entity "App" and an entity "Group". A group belongs to an app (that is, it's a many groups to one app relationship). An app can have many groups. An app can also have a list of default groups. So the App entity has two one-to-many relationships with Group, the App/Owned-Groups relationship and the App/Default-Groups relationship. The former needs to be bidirectional.
The option I have working now is:
- create a many-to-one relationship on Group to App
- create a one-to-many relationship on App to Group and marked it as mapped by Group (this being the inverse of the previous relationship)
- create a one-to-many relationship on App to Group to track the default groups
Code:
@Entity
public class Group {
@ManyToOne
private App app;
}
@Entity
public class App {
@OneToMany(mappedBy="app")
private List<Groups> groups;
@OneToMany
private List<Group> defaultGroups;
}
As I said, this works now but I end up with a join table tracking the default groups. Is there a method whereby I can put the "this is a default group" flag on the Group entity itself and adjust the two relationships in App such that the defaultGroups property only contains the Group entities where isDefaultGroup==true? Would doing so result in less traffic to the database? Would Hibernate make only a single query to the Group table and then populate both collections from the retrieved data?
Thanks.