OpenJPA JPQL问题中的双向OneToMany / ManyToOne映射

时间:2011-02-11 14:08:18

标签: java jpa persistence openjpa

我有两个具有双向关系的课程:

@Entity
@Access(AccessType.FIELD)
@Table(name="ROOM")
public class Room {
    @Id
    @GeneratedValue
    @Column(name="ROOM_ID_PK", updatable=false, nullable=false)
    private int id;
    @Column(name="NAME", nullable=false, unique=true)
    private String name;
    @OneToMany(mappedBy="room", cascade={CascadeType.ALL})
    private final Set<Wall> walls;
    ...
}

@Entity
@Access(AccessType.FIELD)
@Table(name="WALLS")
public class Wall {
    @Id
    @GeneratedValue
    @Column(name="WALL_ID_PK", updatable=false, nullable=false)
    private int id;
    @Column(name="NAME", nullable=false, unique=true)
    private String name;
    @ManyToOne
    @JoinColumn(name="ROOM_ID_FK", referencedColumnName="ROOM_ID_PK")
    private Room room;
    ...
}

我正在运行mysql - 生成了一组看似理智的表:

ROOMS: INT ROOM_ID_PK, VARCHAR NAME
WALLS: INT WALL_ID_PK, VARCHAR NAME, INT ROOM_ID_FK

但是当我执行JPQL查询时

SELECT w FROM Wall w, Room r WHERE w MEMBER OF r.walls AND r = :room

我收到错误:

PropertyAccessException 1: org.springframework.beans.MethodInvocationException:
Property 'wall' threw exception; nested exception is <openjpa-2.0.1-r422266:989424
nonfatal user error> org.apache.openjpa.persistence.ArgumentException:An error occurred
while parsing the query filter "SELECT w FROM Wall w, Room r WHERE w MEMBER OF r.walls
AND r = :room". Error message: No field named "walls" in "Room". Did you mean "name"?
Expected one of the available field names in "mypackage.Room": "[id, name]".

出于某种原因&#39;墙壁&#39;不被视为Room类的一个领域。此代码在休眠状态下工作 - 我尝试迁移到OpenJPA但遇到此错误。我已经确认这两个类都是在我的persistence.xml中定义的。

1 个答案:

答案 0 :(得分:2)

我不是100%确定是否是原因,但让walls变量final看起来很奇怪。

请删除final标记,然后重试。