I'm trying to design a hospitality app. I have two tables as User and Request. Users could be Host or Visitor and can send host request to each other. but there is no need to define Users as visitor or host, it doesn't matter for the system so I don't have seperate tables for them. This difference is just important in Request table and it's needed to keep visitor_id and host_id as foreign keys (mapped with user_id primary key column from User table because both host and visitor are also User).

我正在尝试设计一个酒店应用程序。我有两个表作为用户和请求。用户可以是主持人或访客,可以互相发送主机请求。但是没有必要将用户定义为访问者或主机,这对系统来说无关紧要,因此我没有单独的表格。这种差异在Request表中非常重要,需要将visitor_id和host_id保持为外键(使用User表中的user_id主键列映射,因为主机和访问者都是User)。

My question is how can I define this relation in hibernate with Annotation? I mean, there should be two foreign keys in Request table and they're mapped to *user_id* primary key column from User table. Every user can be host or visitor many times and makes none or many requests.

我的问题是如何在Hibernate中使用Annotation定义这种关系?我的意思是,Request表中应该有两个外键,它们被映射到User表中的* user_id *主键列。每个用户可以多次成为主持人或访问者,并且不会发出任何请求。

@Entity
public class Request {
@Id
private Long req_id;

 ....

}

1 个解决方案

#1


10

A request is for a host, and from a visitor, So you simply have 2 ManyToOne associations from Request to User:

请求是针对主机的,也是来自访问者的,因此您只需要从请求到用户的2个ManyToOne关联:

@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<>();

    // ...
}

更多相关文章

  1. 使用mysql和PHP从本地主机连接到远程数据库
  2. 解决Linux主机上的 远程MySQL客户端无法连接的问题
  3. PHPMyWind教程:如何把本地测试好的程序发布到虚拟主机
  4. 如何将window.location设置为特定路径(没有主机)?
  5. 谷歌地图API V3的使用限制是每个网站访问者还是每个服务器?
  6. 如何访问远程节点。浏览器中的js应用程序,而不是本地主机
  7. linux centos 宝塔主机控制面板安装和安全狗安装过程记录
  8. 用虚拟机装的linux与宿主机的IP能互ping但是linux Ping不了外网
  9. Linux工程实践学习笔记——基于主机系统的多客户即时通讯/聊天室

随机推荐

  1. Interaction and Visual Design the Andr
  2. Android(安卓)Ice Cream Sandwich 截图首
  3. Android UI大杂烩
  4. Android屏蔽/禁止ViewPager左右滑动/滚动
  5. Android研究之英特尔 Android* 开发人员
  6. Android SMS相关操作
  7. Dealing with dependencies in Android p
  8. 最新統計:44% IT 和商務人士選擇 Android(
  9. imageView 的 android:maxHeight,maxWidt
  10. RK3288 Android 7.1 屏蔽“您的设备内部