[英]Hibernate gives me invalid update sql query
我嘗試使用HQL查詢:
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("update AlgorithmScript set isActive = false where user.loginName=:userName");
query.setParameter("userName", userName);
query.executeUpdate();
但是Hibernate生成無效的SQL查詢:
Hibernate: update algorithmfight_checkers_db.algorithmscripts, set IsActive=0 where LoginName=?
幫我。
EDIT1(AlgorithmScript的實體類):此代碼由Hibernate Tools插件生成。================================= ==================
@Entity
@Table(name = "algorithmscripts", catalog = "algorithmfight_checkers_db")
public class AlgorithmScript implements java.io.Serializable {
private int algorithmScriptId;
private User user;
private String fileName;
private String content;
private Date uploadedDate;
private boolean isActive;
private boolean isDeleted;
public AlgorithmScript() {
}
public AlgorithmScript(int algorithmScriptId, User user, String fileName, String content, Date uploadedDate,
boolean isActive, boolean isDeleted) {
this.algorithmScriptId = algorithmScriptId;
this.user = user;
this.fileName = fileName;
this.content = content;
this.uploadedDate = uploadedDate;
this.isActive = isActive;
this.isDeleted = isDeleted;
}
@Id
@Column(name = "AlgorithmScriptId", unique = true, nullable = false)
public int getAlgorithmScriptId() {
return this.algorithmScriptId;
}
public void setAlgorithmScriptId(int algorithmScriptId) {
this.algorithmScriptId = algorithmScriptId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "UserId", nullable = false)
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
@Column(name = "FileName", nullable = false)
public String getFileName() {
return this.fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
@Column(name = "Content", nullable = false, length = 65535)
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "UploadedDate", nullable = false, length = 19)
public Date getUploadedDate() {
return this.uploadedDate;
}
public void setUploadedDate(Date uploadedDate) {
this.uploadedDate = uploadedDate;
}
@Column(name = "IsActive", nullable = false)
public boolean isIsActive() {
return this.isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
@Column(name = "IsDeleted", nullable = false)
public boolean isIsDeleted() {
return this.isDeleted;
}
public void setIsDeleted(boolean isDeleted) {
this.isDeleted = isDeleted;
}
}
首先,您確實需要真正意識到該查詢不是SQL查詢,而是HQL查詢。 這些語言不一樣。
以下是有關DML查詢的文檔說明:
UPDATE和DELETE語句的偽語法為:(UPDATE | DELETE)FROM? EntityName(WHERE where_conditions)?。
需要注意的幾點:
在從句中,FROM關鍵字是可選的
從子句中只能有一個命名的實體。 但是,可以使用別名。 如果實體名稱是別名,則任何屬性引用都必須使用該別名進行限定。 如果實體名稱沒有別名,則對任何屬性引用進行限定都是非法的。
在批量HQL查詢中,不能指定第16.4節“隱式或顯式的連接語法形式”。 子查詢可以在where子句中使用,其中子查詢本身可能包含聯接。
where子句也是可選的。
您的查詢是
update AlgorithmScript set isActive = false where user.loginName=:userName"
因此它違反了第三點,因為它在AlgorithmScript實體和User實體之間使用了隱式聯接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.