We tried out the SingleTable inheritance with a ManyToMany
Code:
@Entity
@Table(name="SHB_PRINCIPAL")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="TYPE",discriminatorType=DiscriminatorType.INTEGER)
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class Principal implements Serializable {
....
Code:
@Entity
@DiscriminatorValue(PrincipalType.USER_NR)
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ApplicationUser extends Principal {
private static final long serialVersionUID = 1L;
@ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},
targetEntity=Role.class)
@JoinTable(name="SHB_PRINCIPAL_TO_PRINCIPAL",
joinColumns=@JoinColumn(name="FROM_PRINCIPAL_ID"),
inverseJoinColumns=@JoinColumn(name="TO_PRINCIPAL_ID"))
private Set<Role> roles;
@ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},
targetEntity=ApplicationGroup.class)
@JoinTable(name="SHB_PRINCIPAL_TO_PRINCIPAL",
joinColumns=@JoinColumn(name="FROM_PRINCIPAL_ID"),
inverseJoinColumns=@JoinColumn(name="TO_PRINCIPAL_ID"))
private Set<ApplicationGroup> groups;
Code:
@Entity
@DiscriminatorValue(PrincipalType.GROUP_NR)
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ApplicationGroup extends Principal {
private static final long serialVersionUID = 1L;
@ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(name="SHB_PRINCIPAL_TO_PRINCIPAL",
joinColumns=@JoinColumn(name="TO_PRINCIPAL_ID"),
inverseJoinColumns=@JoinColumn(name="FROM_PRINCIPAL_ID"))
private Set<ApplicationUser> users;
public ApplicationGroup() {
super();
this.type = PrincipalType.GROUP;
}
public Set<ApplicationUser> getUsers() {
return users;
}
public void setUsers(Set<ApplicationUser> users) {
this.users = users;
}
}
Code:
@Entity
@Table(name="SHB_PRINCIPAL_TO_PRINCIPAL")
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class PrincipalPrincipal implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private Integer id;
@ManyToOne
@JoinColumn(name="FROM_PRINCIPAL_ID")
private Principal fromPrincipal;
@Required
@ManyToOne
@JoinColumn(name="TO_PRINCIPAL_ID")
private Principal toPrincipal;
public PrincipalPrincipal() {
super();
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Principal getFromPrincipal() {
return fromPrincipal;
}
public void setFromPrincipal(Principal fromPrincipal) {
this.fromPrincipal = fromPrincipal;
}
public Principal getToPrincipal() {
return toPrincipal;
}
public void setToPrincipal(Principal toPrincipal) {
this.toPrincipal = toPrincipal;
}
}
But in the groups and the roles of the user, hibernate returns all principals, not only the users or groups. In the query it's clear that the descriminator is NOT used.