Java JPQL 更新查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19443888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPQL Update Query
提问by user2219247
I have an entity like this:
我有一个这样的实体:
@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
}
This query: UPDATE QuestionGroupText T SET T.localizedText.content = :content WHERE T.generatedId = :generatedId
does not work, it throws:
这个查询: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
There must be something wrong with this part of the query: T.localizedText.content
, is there any way I can change it to make this work?
查询的这一部分肯定有问题:T.localizedText.content
有什么办法可以更改它以使其正常工作?
回答by Shervin Asgari
Its seems to me you are trying to update a foreign key from this table.
在我看来,您正在尝试从该表更新外键。
I think the correct way is to update LocalizedText instead.
我认为正确的方法是更新 LocalizedText。
回答by cho
Your attribute localizedText is a foreignkey to your Table for Entity LocalizedText
您的属性 localizedText 是实体 LocalizedText 表的外键
Your query tries to set :content (whatever type this may be) to a foreign key column. This does not work. Do you have the table LocalizedText defined?
您的查询尝试将 :content (无论它是什么类型)设置为外键列。这不起作用。您是否定义了 LocalizedText 表?
What you can do:
你可以做什么:
- set a new ID to your attribute localizedText
- Or update Table LocalizedText with content
- 为您的属性 localizedText 设置一个新 ID
- 或者用内容更新 Table LocalizedText
Please note: You have to set @Id annotation inside QuestionGroupText when you want an Id.
请注意:当您需要 Id 时,您必须在 QuestionGroupText 中设置 @Id 注释。
generatedId seams to be the right place
generateId 接缝是正确的地方
回答by user2219247
I was able to rewrite the query that I needed:
我能够重写我需要的查询:
UPDATE LocalizedText L SET L.content = :content WHERE EXISTS (SELECT T FROM QuestionGroupText T WHERE T.localizedText.id = L.id AND T.generatedId = :generatedId)