Hi,
I'm trying to use @IdClass as composite key. But a lot of example used @IdClass pointing to a class with simple entities. Consider the following:
Code:
@Entity
@IdClass(AssignedRoleId.class)
public class AssignedRole
{
@Id
@ManyToOne
private User userId;
@Id
@ManyToOne
private Role roleId;
private Date dateAssigned;
}
public class AssignedRoleId
{
private long userId;
private long roleId;
}
other entities:
user ( id, username, password )
role (id, role, description )
This quite work but only one foreign key is generated (AssignedRole.userId), there's no generated foreign key for AssignedRole.role.
I can't use user and role as data type for members of AssignedRoleId because hibernate will generate tinyblob as the type of the column if I do.
Notice the name of members of AssignedRole, they're "userId" and "roleId" instead of "user" and "role", because I want those keys stored in the table AssignedRole as "userId" and "roleId". Even if you annotate it with @JoinColumn it will just get ignored. And by looking at the generated sql from "select as.user from AssignedRole as" it only returns a long value instead of an object, so an exception throws when you try "select as.user.username from AssignedRole as" because "as.user" is just a long value and not a user object.
Is the semantics that I want supported by hibernate? Or using @IdClass with @Id(s) with @ManyToOne is misusing hibernate?
Or, can I use non-primitive type as members of AssignedRoleId without letting hibernate generate a blob as the datatype of the column?
Thanks.