无法解析属性Hibernate查询

时间:2013-04-04 13:05:11

标签: java hibernate jpa

@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,则查询有效

有人请帮忙

1 个答案:

答案 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;
...
...
}