[英]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.