【发布时间】:2015-09-22 09:20:02
【问题描述】:
如何使用 hibernate 注释实现以下表格?
当前代码是:(为简洁起见)
用户
@Entity
@Table(name = "user")
public class User implements java.io.Serializable {
@Id
@GeneratedValue
public Long getId() {
return id;
}
}
社交网络
@Entity
@Table(name = "social_network")
public class SocialNetwork implements java.io.Serializable {
@Id
@GeneratedValue
public int getId() {
return id;
}
}
社交档案
@Entity
@Table(name = "social_profile")
public class SocialProfile implements java.io.Serializable {
@Id
@ManyToOne
@JoinColumn(name="user_id")
public User getUser() {
return user;
}
@Id
@ManyToOne
@JoinColumn(name="social_network_id")
public SocialNetwork getSocialNetwork() {
return socialNetwork;
}
}
显然我的代码现在不能正常工作。任何人都可以对此有所了解吗?
【问题讨论】:
-
您也可以考虑为您的 social_profile 添加一个简单的 ID 列,并在两个 FK 上一起添加一个唯一约束。它几乎相同,但更容易处理,恕我直言
-
@stg 感谢您的提示,我在 social_profile 表中添加了一个
id,并且我使这些字段独一无二且不为空。这种方式简单得多。
标签: hibernate jpa spring-data spring-data-jpa