spring CRUDRespository 中的 Update 或 SaveorUpdate,是否有可用的选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24420572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update or SaveorUpdate in CRUDRespository, Is there any options available
提问by Kul Bhushan Prasad
I am trying to do CRUD operations with My Entity bean. CRUDRepository
provide standard methods to find
, delete
and save
but there is no generic method available like saveOrUpdate(Entity entity)
that in turn calls Hibernate
or HibernateTemplate
sessions saveorUpdate()
methods.
我正在尝试使用我的实体 bean 执行 CRUD 操作。CRUDRepository
提供标准的方法find
,delete
而save
但它有没有像一般的方法saveOrUpdate(Entity entity)
是依次呼叫Hibernate
或HibernateTemplate
会话saveorUpdate()
的方法。
The way CRUDRepository
provides this functionality is to
use like this
CRUDRepository
提供此功能的方式是这样使用
@Modifying
@Query("UPDATE Space c SET c.owner = :name WHERE c.id = :id")
Integer setNameForId(@Param("name") String name, @Param("id")
but this is not generic and needs to be written for complete form fields. Please let me know if there is any way or i can get session of Hibernate or object of Spring HibernateTemplate to solve this issue.
但这不是通用的,需要为完整的表单字段编写。请让我知道是否有任何方法或者我可以获得 Hibernate 会话或 Spring HibernateTemplate 对象来解决这个问题。
回答by geoand
The implementation of the method
方法的实现
<S extends T> S save(S entity)
<S extends T> S save(S entity)
from interface
从接口
CrudRepository<T, ID extends Serializable> extends Repository<T, ID>
CrudRepository<T, ID extends Serializable> extends Repository<T, ID>
automatically does what you want. If the entity is new it will call persist
on the entity manager
, otherwise it will call merge
自动做你想做的事。如果实体是新的,它会调用persist
的entity manager
,否则它会调用merge
The code looks like this:
代码如下所示:
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
and can be found here. Note that SimpleJpaRepository
is the class that automatically implements CrudRepository
in Spring Data JPA.
可以在这里找到。请注意,这SimpleJpaRepository
是CrudRepository
在 Spring Data JPA中自动实现的类。
Therefore, there is no need to supply a custom saveOrUpdate()
method. Spring Data JPA has you covered.
因此,无需提供自定义saveOrUpdate()
方法。Spring Data JPA 已满足您的需求。
回答by Kul Bhushan Prasad
The issue is "i am using thymeleaf UI template and The Bean that i am trying to persist that is Form bean not Entity bean and that's why Spring boot is not saving it. Now i have to convert the entire Form bean into Entity bean with changed values and try to persist it." I got the solution for the specific problem i faced in my project, The solution is use @ModelAttribute to convert the form bean to entity.
问题是“我正在使用 thymeleaf UI 模板和我试图保留的 Bean 是 Form bean 而不是 Entity bean,这就是 Spring boot 没有保存它的原因。现在我必须将整个 Form bean 转换为 Entity bean 并更改价值观并努力坚持下去。” 我得到了我在项目中遇到的特定问题的解决方案,解决方案是使用 @ModelAttribute 将表单 bean 转换为实体。
@ModelAttribute("spaceSummary")
public SpaceSummary getSpaceSummary(int id){
return this.spaceSummaryService.getSpaceSummary(id);
}
and
和
@RequestMapping(value="/mgr/editSpace", method = RequestMethod.POST)
public ModelAndView editSpace(@ModelAttribute("spaceSummary") SpaceSummary
spaceSummary, BindingResult result, RedirectAttributes redirect, ModelAndView model) {
}