两个外键作为主键
Two foreign keys as primary key
我将如何使用休眠注释实现下表?
当前代码是:(为简洁起见已删除)
用户
@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;
}
}
显然我的代码现在不能正常工作。任何人都可以阐明这一点吗?
看来您需要具有 join-table 和额外列的多对多映射。
为此,您需要再创建 2 个 类.
仅供参考:http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
您需要一个像这样的可嵌入的 SocialProfileId :
@Embeddable
public class SocialProfileId implements Serializable {
@Column(name = "user_id")
private long userId;
@Column(name = "social_network_id")
private long socialNetworkId;
}
那么,您的 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;
}
}
编辑 抱歉,我在回答中对字段和方法进行了混合注释……永远不要那样做! ;-)
我的 REST 应用程序问题与此类似,但又略有不同,我将在此处 post。
我有 2 个数据 tables:Song & Tag 每个都有一个 id(songid,tagid)。
然后我有一个 table 用于将它们连接在一起 Tagassignment 它只有来自 Song 和 的两个主键标签。所以我不想加入他们,我想保留 table 和两个外键。
我的解决方案来源:http://www.objectdb.com/java/jpa/entity/id
之前
歌曲
@Entity
@Table(name = "songs")
data class Song(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
val id: Int,
...
)
标签
@Entity
@Table(name = "tags")
data class Tag(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
val id: Int,
...
)
标签分配
@Entity
@Table(name = "tagassignment")
data class Tagassignment(
val songid: Int,
val tagid: Int
)
之后
我没有更改歌曲和标签。
标签分配
@Entity
@IdClass(value = TagassignmentKey::class)
@Table(name = "tagassignment")
data class Tagassignment(
@Id
val songid: Int,
@Id
val tagid: Int
)
我创建了一个密钥class
TagassignmentKey
class TagassignmentKey(val songid: Int, val tagid: Int) : Serializable {
constructor() : this(0, 0)
}
除了 Pras 所说的之外,我们甚至可以将 @ManyToOne 移动到 @Embeddable class 本身中,这样 SocialProfileId 就会看起来像,您可以在嵌入式 class 本身中清楚地看到它引用了另一个 table
@Embeddable
public class SocialProfileId implements Serializable {
@ManyToOne
@JoinColumn(name = "user_id")
private User userId;
@ManyToOne
@JoinColumn(name = "social_network_id")
private SocialNetwork socialNetworkId;
public SocialProfileId(User userId, SocialNetwork socialNetworkId) {
this.userId = userId;
this.socialNetworkId = socialNetworkId;
}
}
并且您的 SocialProfile 实体可以简单地嵌入 class 和其他字段,例如电子邮件等..
@Entity
@Table(name = "social_profile")
public class SocialProfile implements java.io.Serializable {
@EmbeddedId
private SocialProfileId id;
@Column(name="email")
private String email;
}
我将如何使用休眠注释实现下表?
当前代码是:(为简洁起见已删除)
用户
@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;
}
}
显然我的代码现在不能正常工作。任何人都可以阐明这一点吗?
看来您需要具有 join-table 和额外列的多对多映射。 为此,您需要再创建 2 个 类.
仅供参考:http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
您需要一个像这样的可嵌入的 SocialProfileId :
@Embeddable
public class SocialProfileId implements Serializable {
@Column(name = "user_id")
private long userId;
@Column(name = "social_network_id")
private long socialNetworkId;
}
那么,您的 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;
}
}
编辑 抱歉,我在回答中对字段和方法进行了混合注释……永远不要那样做! ;-)
我的 REST 应用程序问题与此类似,但又略有不同,我将在此处 post。 我有 2 个数据 tables:Song & Tag 每个都有一个 id(songid,tagid)。 然后我有一个 table 用于将它们连接在一起 Tagassignment 它只有来自 Song 和 的两个主键标签。所以我不想加入他们,我想保留 table 和两个外键。
我的解决方案来源:http://www.objectdb.com/java/jpa/entity/id
之前
歌曲
@Entity
@Table(name = "songs")
data class Song(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
val id: Int,
...
)
标签
@Entity
@Table(name = "tags")
data class Tag(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
val id: Int,
...
)
标签分配
@Entity
@Table(name = "tagassignment")
data class Tagassignment(
val songid: Int,
val tagid: Int
)
之后
我没有更改歌曲和标签。
标签分配
@Entity
@IdClass(value = TagassignmentKey::class)
@Table(name = "tagassignment")
data class Tagassignment(
@Id
val songid: Int,
@Id
val tagid: Int
)
我创建了一个密钥class
TagassignmentKey
class TagassignmentKey(val songid: Int, val tagid: Int) : Serializable {
constructor() : this(0, 0)
}
除了 Pras 所说的之外,我们甚至可以将 @ManyToOne 移动到 @Embeddable class 本身中,这样 SocialProfileId 就会看起来像,您可以在嵌入式 class 本身中清楚地看到它引用了另一个 table
@Embeddable
public class SocialProfileId implements Serializable {
@ManyToOne
@JoinColumn(name = "user_id")
private User userId;
@ManyToOne
@JoinColumn(name = "social_network_id")
private SocialNetwork socialNetworkId;
public SocialProfileId(User userId, SocialNetwork socialNetworkId) {
this.userId = userId;
this.socialNetworkId = socialNetworkId;
}
}
并且您的 SocialProfile 实体可以简单地嵌入 class 和其他字段,例如电子邮件等..
@Entity
@Table(name = "social_profile")
public class SocialProfile implements java.io.Serializable {
@EmbeddedId
private SocialProfileId id;
@Column(name="email")
private String email;
}