@Entity
@Table(name = "t_wday")
@IdClass(DayPk.class)
public class Day {
@Id
private Date id;
..
..
}
@Embeddable
public class DayPk {
@Column(name = "idwday")
private Date id;
...
... other key properties
...
}
@Entity
@Table(name = "t_assigned")
@IdClass(AssignedPk.class)
public class Assigned {
@Id
@ManyToOne
private Day day;
...
...
}
@Embeddable
public class AssignedPk {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "t_wday_idwday", referencedColumnName = "idwday"),
@JoinColumn(name = "t_user_iduser", referencedColumnName = "t_user_iduser")
})
private Day day;
...
... other key properties
...
}
当我尝试创建查询
时getSession().createQuery("FROM Day as day WHERE day.user = :user YEAR(day.id) = :year")
它有效,但当我尝试创建像
这样的查询时getSession().createQuery("FROM Assigned as assig WHERE YEAR(assig.day.id) = :year")
我有一个例外
could not resolve property: day.id of: com.db.entity.Assigned .....
但如果我将DayPk的归档ID和实体Day中提交的id重命名为idwday,则查询有效
有人请帮忙答案 0 :(得分:0)
您应该使用EmbeddedId
课程中的Entity
来访问PK
课程。
@Embeddable
public class DayPk {
@Column(name = "idwday")
private Date id;
...
... other key properties
...
}
@Entity
@Table(name = "t_wday")
public class Day {
@EmbeddedId DayPk id;
..
}
@Embeddable
public class AssignedPk {
private Day day;
...
... other key properties
...
}
@Entity
@Table(name = "t_assigned")
@IdClass(AssignedPk.class)
public class Assigned {
@EmbeddedId AssignedPk id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "t_wday_idwday", referencedColumnName = "idwday"),
@JoinColumn(name = "t_user_iduser", referencedColumnName = "t_user_iduser")
})
private Day day;
...
...
}