2013-10-18 105 views
1

我有一個這樣的實體:JPQL更新查詢

@Entity 
@Table(name = "QUESTION_GROUP_TEXT") 
@NamedQueries({ 
     @NamedQuery(
       name = EntityUtils.NamedQuery.UPDATE_QUESTION_GROUP_TEXT, 
       query = "UPDATE QuestionGroupText T SET T.localizedText.content = :content WHERE T.generatedId = :generatedId") 
}) 
public class QuestionGroupText implements Serializable { 

    @Column(name = "GENERATED_ID", nullable = false, unique = true) 
    private String generatedId = generateId(); 

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
    @JoinColumn(name = "LOCALIZED_TEXT_ID", nullable = false) 
    private LocalizedText localizedText; 

    //Getters and setters 

} 

這個查詢:UPDATE QuestionGroupText T SET T.localizedText.content = :content WHERE T.generatedId = :generatedId不起作用,它拋出:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set CONTENT='Question Label' where GENERATED_ID='AD78F9B5E2A146D9AB9D94096DA835B' at line 1 

必須有與查詢的這部分不對勁: T.localizedText.content,有什麼辦法可以改變它來完成這項工作嗎?

回答

-1

在我看來,你正試圖從這張表更新一個外鍵。

我認爲正確的方法是更新LocalizedText。

0

你的屬性localizedText是一個外鍵到表的實體LocalizedText

您的查詢試圖爲:內容(無論何種類型,這可能是)一個外鍵列。這不起作用。你有定義的表LocalizedText嗎?

你可以做什麼:

  1. 設置一個新的ID,以您的屬性localizedText
  2. 或更新表LocalizedText與內容

請注意:你必須設置裏面QuestionGroupText @Id註釋當你想要一個Id。

generatedId接縫是正確的地方

1

我能改寫,我需要查詢:

UPDATE LocalizedText L SET L.content = :content WHERE EXISTS (SELECT T FROM QuestionGroupText T WHERE T.localizedText.id = L.id AND T.generatedId = :generatedId)