I have the following 2 entities:
@Entity
@Table(name="user")
public class User {
@Id
@Column("id")
private long id;
@Column(name="code", nullable = false)
private String code;
@Column(name="session_id", nullable = false)
private long sessionId;
}
@Entity
@Table(name="task")
public class Task {
@Id
@Column("id", nullable = false)
private long id;
@ManyToOne(optional = false)
@JoinColumn(name="primary_user_code", referencedColumnName = "code", nullable = "false")
private User primaryUser;
@ManyToOne(optional = true)
@JoinColumn(name="secondary_user_code", referencedColumnName = "code", nullable = "true")
private User secondaryUser;
}
The problem is that the two User
objects in Task
has to have the same sessionId
. Is there a way for this to be enforced using hibernate annotations? Or do I have just to bite the bullet and enforce it in code?
I tried looking into the @Where
and @WhereJoinTable
annotations, but according to this hibernate bug report, link, it is not supported for @ManyToOne
Update
I should have probably mentioned that User
already exists and it cannot be changed. What I control is the Task
class which I am adding.
The code is not unique, and together with the session_id it identifies a unique User. The session_id
refers to a separate table but it is not annotated with a @OneToOne
relationship. It is just a plain column and the relationship with the Session
table is handled in code. Essentially each User
has a code, and linked to multiple Sessions
only one of which can be active.
For the Task table that I am adding, I wanted to annotate it if possible