Bidirectional OneToMany/ManyToOne mapping in OpenJPA JPQL problem
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 r开发者_JAVA技巧unning 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.
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.
精彩评论