如何在JPA中添加用户实体和好友关系实体的外键
3个回答
展开全部
// User model类
@Entity
@Table(name = "t_users")
public class User extends Model {
@Id
@Column(name = "u_id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long userId;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "fromUser")
public List<轿拍森Relationship> followUsers;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "toUser")
public List<Relationship> followers;
...
}
@Entity
@Table(name = "t_relationship")
public class Relationship extends Model {
@Id
@Column(name = "rs_id")
public Long relationshipId;
@JoinColumn(name = "rs_fromuser", nullable = false, updatable = false)
@ManyToOne(optional = false)
public User fromUser;
@JoinColumn(name = "rs_touser", nullable = false, updatable = false)
@ManyToOne(optional = false)
public User toUser;
...
}
以下为2014.3.18答案闭亩
问题经过探索后已经解决,但并没有完全理解其中的门贺困道。关于JPA和ORM的基础知识不够扎实,希望能得到解惑。
就如前面所怀疑的,这并不是多对多关系,而是简单的一对一,不过是一个实体中有两个一对一关系罢了。
换用@OneToOne注解,并且了解了一下其他相关的内容,修改为下面的样子。
// User model类
@Entity
@Table(name = "t_users")
public class User extends Model {
@Id
@Column(name = "users_id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long userId;
...
}
// Relationship model类
@Entity
@Table(name = "t_relationship")
public class Relationship extends Model {
@Id
@Column(name = "rs_id")
public Long relationshipId;
@JoinColumn(name = "rs_fromuser", referencedColumnName = "users_id", nullable = false, updatable=false)
@OneToOne(optional = false, targetEntity = User.class)
public User fromUserId;
@JoinColumn(name = "rs_touser", referencedColumnName = "users_id", nullable = false, updatable=false)
@OneToOne(optional = false, targetEntity = User.class)
public User toUserId;
...
}
@Entity
@Table(name = "t_users")
public class User extends Model {
@Id
@Column(name = "u_id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long userId;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "fromUser")
public List<轿拍森Relationship> followUsers;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "toUser")
public List<Relationship> followers;
...
}
@Entity
@Table(name = "t_relationship")
public class Relationship extends Model {
@Id
@Column(name = "rs_id")
public Long relationshipId;
@JoinColumn(name = "rs_fromuser", nullable = false, updatable = false)
@ManyToOne(optional = false)
public User fromUser;
@JoinColumn(name = "rs_touser", nullable = false, updatable = false)
@ManyToOne(optional = false)
public User toUser;
...
}
以下为2014.3.18答案闭亩
问题经过探索后已经解决,但并没有完全理解其中的门贺困道。关于JPA和ORM的基础知识不够扎实,希望能得到解惑。
就如前面所怀疑的,这并不是多对多关系,而是简单的一对一,不过是一个实体中有两个一对一关系罢了。
换用@OneToOne注解,并且了解了一下其他相关的内容,修改为下面的样子。
// User model类
@Entity
@Table(name = "t_users")
public class User extends Model {
@Id
@Column(name = "users_id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long userId;
...
}
// Relationship model类
@Entity
@Table(name = "t_relationship")
public class Relationship extends Model {
@Id
@Column(name = "rs_id")
public Long relationshipId;
@JoinColumn(name = "rs_fromuser", referencedColumnName = "users_id", nullable = false, updatable=false)
@OneToOne(optional = false, targetEntity = User.class)
public User fromUserId;
@JoinColumn(name = "rs_touser", referencedColumnName = "users_id", nullable = false, updatable=false)
@OneToOne(optional = false, targetEntity = User.class)
public User toUserId;
...
}
2017-11-01
展开全部
tity
@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;
...
}
@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;
...
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
u
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询