我有两个具有双向关系的类:
@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]".
出于某种原因,“墙”不被视为 Room 类的字段。这段代码在 hibernate 状态下工作——我试图迁移到 OpenJPA,但遇到了这个错误。我已经确认这两个类都在我的 persistence.xml 中定义。
最佳答案
我不能 100% 确定这是否是原因,但使 walls
变量 final
对我来说看起来很奇怪。
因此,请删除 final
标记,然后重试。
关于java - OpenJPA JPQL 问题中的双向 OneToMany/ManyToOne 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4969929/