2011-02-11 108 views
1

我有兩個班,一個bidrectional關係:雙向一對多/多對一映射OpenJPA中JPQL問題

@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中定義。

回答

2

我不是100%確定是否它的原因,但使walls變量final看起來很奇怪。

因此,請刪除final標記,然後再試一次。