现有用户实体,相关代码如下:@Entity@Table(name = "t_users")public class User extends Model { @Id @Column(name = "users_id") @GeneratedValue(strategy = GenerationType.AUTO) public Long userId; ...}以及好友关系实体,相关代码如下:@Entity@Table(name = "t_relationship")public class Relationship extends Model { @Id @Column(name = "rs_id") public Long relationshipId; @Column(name = "rs_fromuser", nullable = false) public User fromUser; @Column(name = "rs_touser", nullable = false) public User toUser; ...}从SQL书写来考虑,只需要在关系实体的两个Column上添加外键约束即可。但却不知道在JPA中应该怎么完成。我个人数据库相关的知识比较欠缺,在这两个实体是不是多对多关系上存在疑问。一个用户可以对应多个好友关系,一个好友关系对应固定的两个用户。应该说是n..2这种关系吗?已经尝试使用@ManyToMany注解,但是我使用的Play 2框架和Ebean没有生成相对应的SQL语句。@Entity@Table(name = "t_users")public class User extends Model { @Id @Column(name = "users_id") @GeneratedValue(strategy = GenerationType.AUTO) @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY) @JoinTable(name = "users_relationship", joinColumns = { @JoinColumn(name = ("user_id"), referencedColumnName = "userId"), @JoinColumn(name = ("rs_id"), referencedColumnName = "relationshipId") }) public Long userId; ...}@Entity@Table(name = "t_relationship")public class Relationship extends Model { @Id @Column(name = "rs_id") public Long relationshipId; @Column(name = "rs_fromuser", nullable = false) @ManyToMany(targetEntity = User.class, mappedBy = "userId") public User fromUser; @Column(name = "rs_touser", nullable = false) @ManyToMany(targetEntity = User.class, mappedBy = "userId") public User toUser; ...}生成的SQL:create table t_relationship ( rs_id bigint auto_increment not null, #这里缺少了两个user id列 rs_makedate datetime not null, rs_accepted tinyint(1) default 0 not null, constraint pk_t_relationship primary key (rs_id));补充:后来注意到我需要映射的是User类而非User类的集合,在这里我大概是产生了一定的误解,需要搭配Play相关的内容来检查一下。
添加回答
举报
0/150
提交
取消