是否可以阻止hibernate自动更新持久对象?
@Transactional
public ResultTO updateRecord(RequestTO requestTO) {
Entity entity = dao.getEntityById(requestTO.getId());
// now update the entity based on the data in the requestTO
ValidationResult validationResult = runValidation(entity);
if(validationResult.hasErrors()) {
// return ResultTO with validation errors
} else {
dao.persist(entity);
}
}
Run Code Online (Sandbox Code Playgroud)
这是代码中发生的事情,我检索hibernate将被认为处于持久状态的实体,然后我更新实体中的一些字段,然后将实体传递给验证.如果验证失败,则不要udpate,如果验证成功,则持久化实体.
这是这个流程的主要问题:因为我更新了它在验证中使用的实体,无论我是否调用persist()方法(在DAO上)都没关系,因为hibernate将永远更新记录检测到实体已被更改并标记它以进行更新.
请记住,我可以改变验证的方式并解决问题,所以我对解决方法不感兴趣.我很想知道如何能够自动更新持久对象的hibernate功能.
请记住我正在使用hibernates的JPA实现.所以Hibernate处理hibernate特定API的具体答案对我来说不起作用.
我试图寻找hibernate配置,看看我是否可以设置任何配置来阻止这种行为,但没有运气.
谢谢
--EDIT ---我找不到解决方案,所以我选择回滚事务而不抛出任何RuntimeException,即使我在声明事务中使用:
TransactionInterceptor.currentTransactionStatus()使用setRollbackOnly();
这就像一个魅力.
我阅读了 CSS 样式部分(https://vaadin.com/docs/v14/flow/styling/styling-components),它提到全局 CSS 不会影响 shadow DOM 中的“INPUT”字段,因此样式必须是添加到 shdaow DOM,但不幸的是没有明确说明如何将样式添加到 shadow DOM。注意。我主要使用 Flow 纯 Java 和一些 CSS。
我尝试从组件中检索 elementt,然后检索 shadowRoot,然后从根检索“输入”元素以向其添加样式,不幸的是这不起作用,shadowroot 为空(此代码从视图类中的 onAttach() 方法执行)
private void setTextAlignCenterForTextFields(TextField textField) {
//find the internal 'Input' and set the styling to text-align=center, unfortunately
// you cant do that with global css, since the 'input' element is in shadow root
textField.getElement()
.getShadowRoot()
.get()
.getChildren()
.filter( elem -> "INPUT".equalsIgnoreCase(elem.getTag()))
.forEach(inputElem -> inputElem.getStyle().set("text-align", "center"));
}
Run Code Online (Sandbox Code Playgroud)
任何想法,将不胜感激。我正在使用 Vaadin 版本 14.5.1。