OpenJPA JPQL 问题中的双向 OneToMany/ManyToOne 映射
我有两个具有双向关系的类:
@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 类的一个字段。这段代码在休眠状态下工作——我试图迁移到 OpenJPA,但遇到了这个错误。我已经确认这两个类都在我的 persistence.xml 中定义。
I have two classes with a bidrectional relationship:
@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;
...
}
I'm running mysql -- what looks like a sane set of tables is generated:
ROOMS: INT ROOM_ID_PK, VARCHAR NAME
WALLS: INT WALL_ID_PK, VARCHAR NAME, INT ROOM_ID_FK
However when I execute a JPQL query
SELECT w FROM Wall w, Room r WHERE w MEMBER OF r.walls AND r = :room
I get an error:
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]".
For some reason 'walls' is not being seen as a field of the Room class. This code works in hibernate -- I'm attempting to migrate to OpenJPA but came across this error. I've confirmed that both classes are defined in my persistence.xml.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能 100% 确定这是否是原因,但使
walls
变量final
对我来说看起来很奇怪。因此,请删除
final
标记,然后重试。I am not 100% sure if it is the cause, but making the
walls
variablefinal
looks strange for me.So remove the
final
marker, and try it again.