i have 2 entities: User and Role
i have one class Userrole that will contain a composite key between user and role.
now Userrole will not contain userId and roleId.. but the object User and Role and it looks like this:
public class UserRole implements Serializable{
User user;
Role role;
can i put @Id on the User? like:
@Id
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
?
Using annotations, you need to define a proper class for your composite id, and annotate it as
@Embeddable. (With XML, one can map the composite id without an explicit id class too.) Here is an example using an inner class as composite id, and another one, with a top-level id class.