JPQL更新查询

时间:2013-10-18 07:23:38

标签: java sql hibernate jpa 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,有什么方法可以改变它以使其有效吗?

3 个答案:

答案 0 :(得分:2)

我能够重写我需要的查询:

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

答案 1 :(得分:0)

您的属性localizedText是您的实体LocalizedText表的外键

您的查询尝试将:content(无论何种类型)设置为外键列。这不起作用。你有定义LocalizedText表吗?

你能做什么:

  1. 为您的属性localizedText
  2. 设置新ID
  3. 或使用内容
  4. 更新Table LocalizedText

    请注意:当您需要Id时,您必须在QuestionGroupText中设置@Id注释。

    generatedId接缝是正确的地方

答案 2 :(得分:-1)

在我看来,你正试图从这个表中更新一个外键。

我认为正确的方法是更新LocalizedText。