使用来自同一个表的两个外键来Hibernate POJO

时间:2012-10-08 03:21:58

标签: hibernate pojo

如何使用注释创建POJO,以便hibernate在映射时创建下表?一列是关注EE,另一列是关注ER

CREATE TABLE friends(
  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  followee_id INT(11) UNSIGNED NOT NULL,
  follower_id INT(11) UNSIGNED NOT NULL,
  start_time TIMESTAMP NOT NULL,
  end_time TIMESTAMP NOT NULL,
  PRIMARY KEY(id),
  KEY idx_fk_friends_followee_id (followee_id),
  KEY idx_fk_friends_follower_id (follower_id),
  CONSTRAINT `fk_friends_followee_id` FOREIGN KEY (followee_id) REFERENCES user (id) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `fk_friends_follower_id` FOREIGN KEY (follower_id) REFERENCES user (id) ON DELETE RESTRICT ON UPDATE CASCADE
)ENGINE=MyISAM DEFAULT CHARSET=utf8;

1 个答案:

答案 0 :(得分:0)

我不确定你的问题是什么,因为我觉得这很简单:

@Entity
public class Friend {
  @Id
  private Long id;

  @ManyToOne
  private User follower;

  @ManyToOne
  private User followee;
}