我正在使用复合主键,它在实体类中定义为:
@EmbeddedId
private ParticipantPrimaryKey pKey;
主键由PrimaryKey实体类中定义的Event Id和Student Id组成。
现在我需要从参与任何特定事件的表中获取参与者。
由于上述问题而无法运行的 HQL 查询:
select pe from ParticipantEntity pe where pe.eventId=?
如果我使用任何其他字段,那么它将起作用,因为它们存在于实体类中,但事件 ID 存在于 PrimaryKey 实体中。
最佳答案
您可以使用@ClassId
,like :
@ClassId(ParticipantPrimaryKey.class)
class ParticipantEntity { ...
从ParticipantEntity
中删除:
@EmbeddedId
private ParticipantPrimaryKey pKey;
还在 ParticipantEntity
中添加两个键:
@Id
private Long eventId;
@Id
private Long studentId;
之后,您可以:
SELECT pe FROM ParticipantEntity pe where pe.eventId = :eventId
关于java - 通过Hibernate获取数据而不传递主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50323227/