The composite key for my table is 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.
You can have EmbeddedId class contain all the 3 keys. 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.
@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.
Composite 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.