是否可以在Hibernate中为@SQLDelete覆盖添加自定义参数?我不想编辑Hibernate源代码。在下面的示例中,我尝试在删除查询中包含lastUpdatedBy列。
感谢您的帮助。
@Entity
@Table(name = "user")
@SQLDelete(sql = "UPDATE user SET deleted='Y', version=version+1 WHERE id=? AND version=? AND lastUpdatedBy = ?")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "deleted")
@Type(type = "yes_no")
protected boolean deleted;
@Version
@Column(name = "version", nullable = false)
protected Integer version;
@Column(name = "last_updated_by")
protected Long lastUpdatedBy;
...
}
答案 0 :(得分:0)
下面是示例代码,您可以按照手动覆盖删除事件的代码:
public class CustomDeleteEventListener extends DefaultDeleteEventListener implements DeleteEventListener {
private static final Logger log = Logger.getLogger(CustomDeleteEventListener.class);
@Autowired
private UserService userService;
/**
* Override the default delete logic here, handle the given delete event.
*
* @param event DeleteEvent object which can get hibernate session or entity
* @param transientEntities The cache of entities already deleted
* @throws HibernateException
*/
@Override
public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
// get current deleting entity from event
Object entity = event.getObject();
if (log.isDebugEnabled()) {
log.debug("entity class type is " + entity.getClass());
}
if (entity instanceof User) {
if (log.isDebugEnabled()) {
log.debug("Detect delete User event, delegate to user service to do soft deletion. entity type: " + entity.getClass());
}
userService.removeUser((User) entity);
} else {
// other entities still follow their default logic
super.onDelete(event, transientEntities);
}
}
}