Java CRUDRepository 中的更新或 saveorUpdate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43719828/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:07:56  来源:igfitidea点击:

Update or saveorUpdate in CRUDRepository

javaspringspring-bootspring-data

提问by Anuraag Mishra

I know there's already a similar question answered previously, but my problem is implementing save with update while there are 3 methods in the interface. I'm currently using the following methods in my project and don't know how to make saveOrUpdate in this. The following are my classes:

我知道之前已经回答过类似的问题,但我的问题是在接口中有 3 个方法时实现带更新的保存。我目前在我的项目中使用以下方法,但不知道如何在其中进行 saveOrUpdate。以下是我的课程:

   public interface CompanyRepository extends CrudRepository<Company,Long>{
   Company findByCompanyName (String companyName);
   List<Company> findAll();
   Company findById(Long id);
   }

The following is part of my Company Class

以下是我公司课程的一部分

@Entity
public class Company extends BaseEntity{
 @NotNull
@Size(min = 2, max = 16)
private String companyName;
@Length(max = 60)
private String desc;
//TODO max: add LOGO class later for pic saving
@OneToMany
private List<MobileModel> mobileModels;

public Company()
{
  super();
  mobileModels = new ArrayList<>();
  }

//Getters n setters
 }

The following is my baseEntity clas

以下是我的 baseEntity 类

@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy =  GenerationType.AUTO)
protected final Long id;

@Version
private Long version;  
//Getters n setters
}

Thanks in advance. I read everywhere and tried so many things for 5 hours. I just want CompanyRepository to implement all 3 methods without me overriding them in some other class but if I have too then explain how because part of my code is dependent on CompanyRepository. I just wish to add save with update, please explain with respect to my code.

提前致谢。我到处阅读,并在 5 个小时内尝试了很多东西。我只希望 CompanyRepository 实现所有 3 个方法,而无需在其他类中覆盖它们,但如果我也这样做了,请解释如何,因为我的部分代码依赖于 CompanyRepository。我只想添加带更新的保存,请解释我的代码。

回答by Tom

CrudRepositoryhas only savebut it acts as updateas well.

CrudRepository只有save但它也起作用update

  • When you do saveon entity with empty idit will do a save.
  • When you do saveon entity with existing idit will do an updatethat means that after you used findByIdfor example and changed something in your object, you can call saveon this object and it will actually do an updatebecause after findByIdyou get an object with populated idthat exist in your DB.
  • savein CrudRepositorycan accept a single entity or Iterableof your entity type.
  • 当您save对空实体执行操作时id,它将执行save.
  • 当您save对现有实体进行id操作时,update这意味着在您使用findById例如并更改了对象中的某些内容之后,您可以调用save此对象,它实际上会执行一个操作,update因为在findById您获得id存在于您的D B。
  • saveinCrudRepository可以接受单个实体或Iterable您的实体类型。

回答by Zahid Indher

putting below if check resolve my issue and save method is working as save and update both when i pass id it updates the record in database and when i dont pass id it save new record in database place is incoming object in my case and new place is new object of place in which i am setting the place id

放在下面,如果检查解决了我的问题,并且 save 方法在我传递 id 时它会更新数据库中的记录,并且当我不传递 id 时它会在数据库中保存新记录,那么 save 方法正在作为 save 和 update 工作,在我的情况下,它是传入对象,新的地方是我在其中设置地点 ID 的地点的新对象

    if(place.getPlaceId()!=0)
        newPlace.setPlaceId(place.getPlaceId());