A request is for a host, and from a visitor, So you simply have 2 ManyToOne associations from Request to User:
@Entity
public class Request {
@Id
@Column(name = "req_id")
private Long id;
@ManyToOne
@JoinColumn(name = "visitor_id")
private User visitor;
@ManyToOne
@JoinColumn(name = "host_id")
private User host;
// ...
}
If you want to make these associations bidirectional, then you simply need corresponding collections in the user:
@Entity
private class User {
/**
* requests made to this user, in order for this user to be a host
*/
@OneToMany(mappedBy = "host")
private Set<Request> hostRequests = new HashSet<>();
/**
* requests made by this user, in order for this user to be a visitor
*/
@OneToMany(mappedBy = "visitor")
private Set<Request> visitorRequests = new HashSet<>();
// ...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…