问题描述
我想创建一个没有ID的实体.
Hi I want to create an Entity which doesn't have an ID.
@Entity
@Table(name="USER_PROD_LIC_TYPE_ALL")
public class UserProdLicTypeAll {
@EmbeddedId
private UserProdLicTypeAllPK id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="USER_ID")
private User user;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="LICENSE_TYPE_ID")
private LicenseType licType;
....
}
因为它没有主键,所以我创建了Embeddable类,如下所示:
since it doesn't have primary key i created Embeddable class as below:
@Embeddable
public class UserProdLicTypeAllPK {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="USER_ID")
private User user;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="LICENSE_TYPE_ID")
private LicenseType licType;
...
}
这两个字段的组合返回唯一值.但这是行不通的.我得到
The combination of these two fields returns a unique value.But it doesn't work. I get
org.springframework.beans.factory.UnsatisfiedDependencyException: exception
.
我是否需要在User和LicenseType实体中对UserProdLicTypeAll和UserProdLicTypeAllPK进行引用?我也尝试过,但是仍然行不通.
Do i need to have references in User and LicenseType entities for both UserProdLicTypeAll and UserProdLicTypeAllPK? I have tried that also but still it doesn't work.
推荐答案
这是我的私人地狱.我不确定正确解决问题的最佳方法是什么.
This is my private hell. I'm not sure what the best way to solve it properly.
我最好的解决方案是:
@Entity
@Table(name = TableName.AREA_USER)
@IdClass(UserAreaPK.class)
public class UserArea implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = UserAreaPK.C_AREA_ID)
private Long areaId;
@Id
@Column(name = UserAreaPK.C_USER_ID)
private String userId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = UserAreaPK.C_AREA_ID, insertable = false, updatable = false)
private Area area;
还有
/**
* Primary key for the relationship User-Area
*/
public class UserAreaPK implements Serializable {
protected static final String C_AREA_ID = "area_id";
protected static final String C_USER_ID = "user_id";
private static final long serialVersionUID = 1L;
@Id
@Column(name = C_AREA_ID)
private Long areaId;
@Id
@Column(name = C_USER_ID)
private String userId;
这篇关于没有主键ID的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!