pre*_*oid 9 hibernate jpa spring-data spring-data-jpa
我如何使用hibernate注释实现这一点?
目前的代码是:( 为简洁而剥离)
用户
@Entity
@Table(name = "user")
public class User implements java.io.Serializable {
@Id
@GeneratedValue
public Long getId() {
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
社交网络
@Entity
@Table(name = "social_network")
public class SocialNetwork implements java.io.Serializable {
@Id
@GeneratedValue
public int getId() {
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
SocialProfile
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
显然我的代码现在无法正常工作.任何人都可以对此有所了解吗?
你需要一个像这样的可嵌入的SocialProfileId:
@Embeddable
public class SocialProfileId implements Serializable {
@Column(name = "user_id")
private long userId;
@Column(name = "social_network_id")
private long socialNetworkId;
}
Run Code Online (Sandbox Code Playgroud)
那么,您的SocialProfile实体将如下所示:
@Entity
@Table(name = "social_profile")
public class SocialProfile implements java.io.Serializable {
@EmbeddedId
private SocialProfileId id;
@ManyToOne
@JoinColumn(name="user_id")
public User getUser() {
return user;
}
@ManyToOne
@JoinColumn(name="social_network_id")
public SocialNetwork getSocialNetwork() {
return socialNetwork;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑抱歉,我在答案中对字段和方法进行了混合注释......从来没有这样做过!;-)
| 归档时间: |
|
| 查看次数: |
6359 次 |
| 最近记录: |