简体   繁体   English

休眠给我无效的更新SQL查询

[英]Hibernate gives me invalid update sql query

I try to use HQL 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(); 

but Hibernate generates invalid SQL query: 但是Hibernate生成无效的SQL查询:

 Hibernate: update algorithmfight_checkers_db.algorithmscripts,  set IsActive=0 where LoginName=?

Help me. 帮我。

EDIT1 (Entity class for AlgorithmScript): This code was generated by Hibernate Tools plugin==================================================== 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;
}

}

First of all, you really, really need to realize that the query is not a SQL query, but a HQL query. 首先,您确实需要真正意识到该查询不是SQL查询,而是HQL查询。 Those are not the same languages. 这些语言不一样。

Here's what the documentation says about DML queries: 以下是有关DML查询的文档说明:

The pseudo-syntax for UPDATE and DELETE statements is: ( UPDATE | DELETE ) FROM? UPDATE和DELETE语句的伪语法为:(UPDATE | DELETE)FROM? EntityName (WHERE where_conditions)?. EntityName(WHERE where_conditions)?。

Some points to note: 需要注意的几点:

  • In the from-clause, the FROM keyword is optional 在从句中,FROM关键字是可选的

  • There can only be a single entity named in the from-clause. 从子句中只能有一个命名的实体。 It can, however, be aliased. 但是,可以使用别名。 If the entity name is aliased, then any property references must be qualified using that alias. 如果实体名称是别名,则任何属性引用都必须使用该别名进行限定。 If the entity name is not aliased, then it is illegal for any property references to be qualified. 如果实体名称没有别名,则对任何属性引用进行限定都是非法的。

  • No Section 16.4, “Forms of join syntax”, either implicit or explicit, can be specified in a bulk HQL query. 在批量HQL查询中,不能指定第16.4节“隐式或显式的连接语法形式”。 Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins. 子查询可以在where子句中使用,其中子查询本身可能包含联接。

  • The where-clause is also optional. where子句也是可选的。

Your query is 您的查询是

update AlgorithmScript set isActive = false where user.loginName=:userName"

So it violates the third point, since it uses an implicit join between the AlgorithmScript entity and the User entity. 因此它违反了第三点,因为它在AlgorithmScript实体和User实体之间使用了隐式联接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM