0
@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 中的归档重命名为 idwday 查询是有效的

有人请帮忙

4

1 回答 1

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;
...
...
}
于 2013-04-04T16:24:54.080 回答