Java 软删除指令休眠
我有一个插件类。删除其实体时,我希望将记录保留在数据库中,但将其隐藏在查询中 在以下实现中,我遇到了一个异常:Java 软删除指令休眠,java,mysql,hibernate,Java,Mysql,Hibernate,我有一个插件类。删除其实体时,我希望将记录保留在数据库中,但将其隐藏在查询中 在以下实现中,我遇到了一个异常: @Entity @SQLDelete(sql = "UPDATE addon SET active IS FALSE WHERE id = ?") @Where(clause = "active = false") public class Addon { @Id @GeneratedValue(strategy = GenerationType.AUTO)
@Entity
@SQLDelete(sql = "UPDATE addon SET active IS FALSE WHERE id = ?")
@Where(clause = "active = false")
public class Addon {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
private Category category;
private String name;
private int amount;
private int minimalAmount;
@OneToMany
private Set<HandOver> handOvers;
private boolean active;
public Addon(){
this.setAmount(0);
this.active = true;
}
@PreRemove
public void deleteAddon() {
this.setActive(false);
}
}
Led 17, 2019 3:45:44 ODP. org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1064, SQLState: 42000
Led 17, 2019 3:45:44 ODP. org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: 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 'IS FALSE WHERE id = 3' at line 1
Led 17, 2019 3:45:44 ODP. org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Led 17, 2019 3:45:44 ODP. org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]
An error occured. Addon has not been deleted: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
例外情况:
@Entity
@SQLDelete(sql = "UPDATE addon SET active IS FALSE WHERE id = ?")
@Where(clause = "active = false")
public class Addon {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
private Category category;
private String name;
private int amount;
private int minimalAmount;
@OneToMany
private Set<HandOver> handOvers;
private boolean active;
public Addon(){
this.setAmount(0);
this.active = true;
}
@PreRemove
public void deleteAddon() {
this.setActive(false);
}
}
Led 17, 2019 3:45:44 ODP. org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1064, SQLState: 42000
Led 17, 2019 3:45:44 ODP. org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: 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 'IS FALSE WHERE id = 3' at line 1
Led 17, 2019 3:45:44 ODP. org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Led 17, 2019 3:45:44 ODP. org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]
An error occured. Addon has not been deleted: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
查询(hibernate配置中的“show_sql”=true)为:
我使用的是Hibernate 5和MySQL 8。试着这样写:@Where(clause=“active='false'),或者如果active=true意味着一个可访问的记录,则为true;) 我想说它应该是
active IS FALSE
而不是active FALSE
和SET active=FALSE
(true literal)而不是SET active='FALSE'
(字符串值“FALSE”,尽管MySQL很可能能够正确地转换)。仍然会引发相同的异常。您可以发布生成的SQL吗?它应该作为DEBUG fororg.hibernate.SQL
记录。顺便说一句,如果你想过滤非活动的插件,我想进一步说where子句应该是active,它是TRUE
。程序更进一步,但随后引发了另一个异常。我已经在问题中添加了异常和查询。为什么会出现这样的查询:“UPDATE addon SET active IS FALSE,其中id=?”应该是:“UPDATE addon SET active=FALSE,其中id=?”,所以写下以下内容:@SQLDelete(sql=“UPDATE addon SET active=FALSE,其中id=?”)