tags:

views:

43

answers:

1

Hi, I would like a 'RolesPlayed' entity with the following columns

  • user
  • role
  • department/project/group

All the three columns above constitute a composite primary key. I would like to know if defining a column to be one of department/project/group possible ? If yes, how ? Or do I need to break the entity into DepartmentRoles, GroupRoles and ProjectRoles.

Thanks.

A: 

You could use polymorphism with an abstract base class to do that.

@Entity
public class RolePlayed {

    @ManyToOne
    private User user;

    @ManyToOne
    private Role role;

    @ManyToOne
    private Body body;

    ...

}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Body {
    ...
}

@Entity
public class Department extends Body {
    ...
}

@Entity
public class Project extends Body {
    ...
}

@Entity
public class Group extends Body {
    ...
}

Check out the Polymorphism section in the Java Enterprise tutorial for a good overview.

Alternatively, you could also make the RolePlayed entity abstract, with DepartmentRole, GroupRole and ProjectRole implementations.

Henning