我有一个用户获得一些个人资料的用例:所以是一对多关联。
这是通过伪 sql 中的这些遗留表映射的。
TABLE USER
(
ID PK
SURNAME VARCHAR2(45 BYTE) NOT NULL,
NAME VARCHAR2(45 BYTE) NOT NULL,
USER_ID NUMBER
)
TABLE USER_PROFILES
(
USER_ID NUMBER NOT NULL,
ID_PROFILO NUMBER NOT NULL,
DESCRIPTION VARCHAR2(32 BYTE)
)
这是java映射:
@Entity
@Table(name = "USER)
public class User{
@Id
@Column(name = "ID")
private String id;
@OneToMany(fetch= FetchType.EAGER)
@JoinColumns({ @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")})
private List<UtenteProfilo> profiles;
}
和 userprofiles 类
@Entity
@Table(name = "USER_PROFILES")
@IdClass(UserProfile.UserProfileId.class)
public class UserProfile implements java.io.Serializable {
@Id
@Column(name = "USER_ID", nullable = false)
private Long userId;
@Id
@Column(name = "ID_PROFILO", nullable = false, insertable = false, updatable = false)
private Long profiloId;
private String description;
static public class UserProfileId implements Serializable {
@Id
@Column(name = "USER_ID", nullable = false)
Long userId;
@Id
@Column(name = "ID_PROFILO", nullable = false)
Long profiloId;
}
}
当我从 UserDao 获得用户时,我收到此异常:
Exception Description: The @JoinColumns on the annotated element [field profiles] from the entity class [User] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
问题是在源实体(获得 1-N 关联的实体)上,我只有一列可以进行连接。
我试图更改目标实体的 ID;所以在 user_profiles 类中,只声明 user_id 作为键:但是以这种方式,当 jpa 创建查询时,它只处理一个 user_profiles 出现,使关系为 1-1。
知道如何解决问题并使关联正常工作吗?
亲切的问候马西莫