在 jpa 中使用对象作为复合主键
use objects as composite primary key in jpa
您好,我想请您提供有关 JPA 复合主键的帮助。
对于我的项目,我在 MySQL 数据库 中有不同的 table 用户、工作和参与。为此,我创建了 class 用户、工作和参与,因为在 table 参与中,我有一个复合主键,它引用用户主键和工作主键。
总结以下代码涉及 class Participation 和 MyKeyParticipation.
@Entity
@ManagedBean
@IdClass(value=MyParticipationKey.class)
public class Participation {
@Id
@Column(name="pkIduser")
private User user;
@Id
@Column(name="pkIdet")
private Work work;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Work getWork() {
return work;
}
public void setWork(Work work) {
this.work = work;
}}
并完成
public class MyParticipationKey implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private Work work;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Work getWork() {
return work;
}
public void setWork(Work work) {
this.work = work;
}
}
以下行涉及 class 用户和工作
@Entity
@ManagedBean
public class User implements Serializable{
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long pkIduser;
@NotNull(message="Please enter your firstname")
@Size(min=3,message="Min size 3 characters!")
private String firstname;
@NotNull(message="Please enter your lastname")
@Size(min=3,message="Min size 3 characters!")
private String lastname;
@NotNull(message="Phone number field must not be null")
@Size(min=5,message="Phone number must have !")
private String phone;
private String statut;
@NotNull(message="Enter valid email")
@Pattern(regexp="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+ ([^.@]+)",message="please enter valid email")
private String email;
@NotNull(message="please enter password")
@Size(min=3,message="Password must have 3 characters")
private String password;
public Long getPkIduser() {
return pkIduser;
}
public void setPkIduser(Long pkIduser) {
this.pkIduser = pkIduser;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getStatut() {
return statut;
}
public void setStatut(String statut) {
this.statut = statut;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Entity
@ManagedBean
public class Work implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long pkIdet;
@NotNull(message="Enter the name")
@Size(min=3,message="Name must have 3 characters!")
private String name;
private String type;
private String description;
private String statut;
@ManyToOne
@JoinColumn(name="pkIduser")
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getStatut() {
return statut;
}
public void setStatut(String statut) {
this.statut = statut;
}
public Long getPkIdet() {
return pkIdet;
}
public void setPkIdet(Long pkIdet) {
this.pkIdet = pkIdet;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
我不明白这里出了什么问题,因为我的 IDE Eclipse 告诉我
来自嵌入 ID class [class gct.entities.MyParticipationKey] 的映射 [work] 是此 class 的无效映射。与嵌入式 ID 规范(来源 [class gct.entities.Participation] 中的属性 [myParticipationKey])一起使用的可嵌入 class 只能包含基本映射。删除非基本映射或更改要嵌入的源上的嵌入 ID 规范。
请帮助我,欢迎提出任何建议。谢谢!
您的 @IdClass
应包含与目标实体的 @Id
字段匹配的字段。 MyParticipationKey
应该看起来像这样:
public class MyParticipationKey implements Serializable {
private Long user;
private Long work;
...
}
请注意,与正常的 @IdClass
一样,字段名称必须与实体的相应字段名称匹配(在本例中为 user
和 work
);但字段的类型必须与目标实体的主键字段的类型匹配(Long
,在本例中)。
派生身份在 JPA 2.1 规范的第 2.4.1 节中进行了讨论。
您好,我想请您提供有关 JPA 复合主键的帮助。 对于我的项目,我在 MySQL 数据库 中有不同的 table 用户、工作和参与。为此,我创建了 class 用户、工作和参与,因为在 table 参与中,我有一个复合主键,它引用用户主键和工作主键。 总结以下代码涉及 class Participation 和 MyKeyParticipation.
@Entity
@ManagedBean
@IdClass(value=MyParticipationKey.class)
public class Participation {
@Id
@Column(name="pkIduser")
private User user;
@Id
@Column(name="pkIdet")
private Work work;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Work getWork() {
return work;
}
public void setWork(Work work) {
this.work = work;
}}
并完成
public class MyParticipationKey implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private Work work;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Work getWork() {
return work;
}
public void setWork(Work work) {
this.work = work;
}
}
以下行涉及 class 用户和工作
@Entity
@ManagedBean
public class User implements Serializable{
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long pkIduser;
@NotNull(message="Please enter your firstname")
@Size(min=3,message="Min size 3 characters!")
private String firstname;
@NotNull(message="Please enter your lastname")
@Size(min=3,message="Min size 3 characters!")
private String lastname;
@NotNull(message="Phone number field must not be null")
@Size(min=5,message="Phone number must have !")
private String phone;
private String statut;
@NotNull(message="Enter valid email")
@Pattern(regexp="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+ ([^.@]+)",message="please enter valid email")
private String email;
@NotNull(message="please enter password")
@Size(min=3,message="Password must have 3 characters")
private String password;
public Long getPkIduser() {
return pkIduser;
}
public void setPkIduser(Long pkIduser) {
this.pkIduser = pkIduser;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getStatut() {
return statut;
}
public void setStatut(String statut) {
this.statut = statut;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Entity
@ManagedBean
public class Work implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long pkIdet;
@NotNull(message="Enter the name")
@Size(min=3,message="Name must have 3 characters!")
private String name;
private String type;
private String description;
private String statut;
@ManyToOne
@JoinColumn(name="pkIduser")
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getStatut() {
return statut;
}
public void setStatut(String statut) {
this.statut = statut;
}
public Long getPkIdet() {
return pkIdet;
}
public void setPkIdet(Long pkIdet) {
this.pkIdet = pkIdet;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
我不明白这里出了什么问题,因为我的 IDE Eclipse 告诉我 来自嵌入 ID class [class gct.entities.MyParticipationKey] 的映射 [work] 是此 class 的无效映射。与嵌入式 ID 规范(来源 [class gct.entities.Participation] 中的属性 [myParticipationKey])一起使用的可嵌入 class 只能包含基本映射。删除非基本映射或更改要嵌入的源上的嵌入 ID 规范。 请帮助我,欢迎提出任何建议。谢谢!
您的 @IdClass
应包含与目标实体的 @Id
字段匹配的字段。 MyParticipationKey
应该看起来像这样:
public class MyParticipationKey implements Serializable {
private Long user;
private Long work;
...
}
请注意,与正常的 @IdClass
一样,字段名称必须与实体的相应字段名称匹配(在本例中为 user
和 work
);但字段的类型必须与目标实体的主键字段的类型匹配(Long
,在本例中)。
派生身份在 JPA 2.1 规范的第 2.4.1 节中进行了讨论。