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

发布于 2024-10-17 03:33:12 字数 1730 浏览 2 评论 0原文

我有两个具有双向关系的类:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶视觉 2024-10-24 03:33:12

我不能 100% 确定这是否是原因,但使 walls 变量 final 对我来说看起来很奇怪。

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

I am not 100% sure if it is the cause, but making the walls variable final looks strange for me.

So remove the final marker, and try it again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文