Hibernate给了我无效的更新sql查询

时间:2015-09-11 22:07:53

标签: java mysql hibernate

我尝试使用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;
}

}

1 个答案:

答案 0 :(得分:1)

首先,您确实需要意识到查询不是SQL查询,而是HQL查询。那些语言不一样。

这是the documentation关于DML查询的内容:

  

UPDATE和DELETE语句的伪语法是:(UPDATE | DELETE)FROM? EntityName(WHERE where_conditions)?.

     

需要注意的一些要点:

     
      
  • 在from子句中,FROM关键字是可选的

  •   
  • 在from子句中只能命名一个实体。但是,它可能是别名。如果实体名称是别名,则必须使用该别名限定任何属性引用。如果实体名称没有别名,则任何属性引用都是合法的。

  •   
  • 可以在批量HQL查询中指定第16.4节“隐式或显式的连接语法形式”。子查询可以在where子句中使用,子子查询本身可以包含连接。

  •   
  • where子句也是可选的。

  •   

您的查询是

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

所以它违反了第三点,因为它使用AlgorithmScript实体和User实体之间的隐式连接。