Hello,
could you please help me to map OneToOne relation using two-part foreing key. Unfortunatelly, one part of this key is in another table !
Table
Type has two-column-primary-key
TYPE_ID, PROVIDER_ID (private forein key). I need to map relation between
Task and
Type tables (named
relationTaskType on enclosed image). To do so, I need whole PK from
Type in
Task. But in table
Task is only half of this PK ! Second part is in linked table
Client (linked by
CLIENT_ID).
I do not know how to map it. Any idea? Thanks a lot.
PS: I do not like this DB design, but its old project and Iam not permited to change it )-:
IMAGE:
http://www.rajce.net/f297352485Code:
@Entity
@Table(name = "Task")
public class Task implements Serializable {
@Id
@Basic(optional = false)
@Column(name = "TASK_ID")
private Long taskId;
@JoinColumn(name = "CLIENT_ID", referencedColumnName = "CLIENT_ID")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Client client;
// @ ???
// private Type type;
}
@Entity
@Table(name = "TYPE")
public class Type implements Serializable {
@EmbeddedId
protected TypePK typePK;
@JoinColumn(name = "PROVIDER_ID", referencedColumnName = "PROVIDER_ID", insertable = false, updatable = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Provider provider;
}
@Embeddable
public class TypePK implements Serializable {
@Basic(optional = false)
@Column(name = "TYPE_ID")
private Long typeId;
@Basic(optional = false)
@Column(name = "PROVIDER_ID")
private Long providerId;
}
@Entity
@Table(name = "CLIENT")
public class Client implements Serializable {
@Id
@Basic(optional = false)
@Column(name = "CLIENT_ID")
private Long clientId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "client", fetch = FetchType.LAZY)
private Collection<Task> tasks;
}