[英]How to have a composite id where fields are available in multiple embedded classes
The composite key for my table is userid, orderno, orderstatus.我的表的组合键是 userid、orderno、orderstatus。
Entity class is实体类是
@Entity
public class OrTest implements java.io.Serializable {
private UserInfo user; //UserInfo is embedded class having userid
private OrderInfo order; // OrderInfo is embedded class having order
private Status status; //Status is embedded class having orderstatus
//Contains other misc info
}
How to use embeddedid in this scenario where my actual composite key fields are split across multiple embedded classes.如何在这种情况下使用 embeddingid,其中我的实际复合键字段被拆分为多个嵌入式类。
You can have EmbeddedId class contain all the 3 keys.您可以让 EmbeddedId 类包含所有 3 个键。 like below像下面
public class OrTextPk implements Serializable{
@Column(name="userId")
public String userId;
@Column(name="orderNo")
public String orderNo'
@Column(name="orderStatus")
public String orderStatus;
}
Now, in the OrText class you can do the following.现在,在 OrText 类中,您可以执行以下操作。
@Entity
public class OrTest implements java.io.Serializable {
@EmbeddedId
private OrTextPk id;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="userId")
private UserInfo user; //UserInfo is embedded class having userid
.
.
.
so on...
}
In addition to the solution posted by @Zeus, You only need to do minor change in following way.除了@Zeus 发布的解决方案,您只需要按照以下方式进行细微更改。
Composite PK复合PK
public class OrTextPk implements Serializable{
public String userId;
@Column(name="orderNo")
public String orderNo'
@Column(name="orderStatus")
public String orderStatus;
}
Main entity class主要实体类
@Entity
public class OrTest implements java.io.Serializable {
@EmbeddedId
private OrTextPk id;
@OneToOne(fetch = FetchType.LAZY)
// Note: it will help to map value of userId (UserInfo) to Pk object
@MapsId("userId")
@JoinColumn(name = "userId")
private UserInfo user; //UserInfo is embedded class having userid
.
.
.
so on...
}
Now, on setting New/Persisted object of UserInfo
(user), value of userId
of UserInfo
object will be assigned to userId
of OrTextPk
while persisting OrTest
object.现在,在设定的新的/持久对象UserInfo
(用户),的值userId
的UserInfo
对象将被分配给userId
的OrTextPk
而持续OrTest
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.