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
Update or saveorUpdate in CRUDRepository
提问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
CrudRepository
has only save
but it acts as update
as well.
CrudRepository
只有save
但它也起作用update
。
- When you do
save
on entity with emptyid
it will do asave
. - When you do
save
on entity with existingid
it will do anupdate
that means that after you usedfindById
for example and changed something in your object, you can callsave
on this object and it will actually do anupdate
because afterfindById
you get an object with populatedid
that exist in your DB. save
inCrudRepository
can accept a single entity orIterable
of your entity type.
- 当您
save
对空实体执行操作时id
,它将执行save
. - 当您
save
对现有实体进行id
操作时,update
这意味着在您使用findById
例如并更改了对象中的某些内容之后,您可以调用save
此对象,它实际上会执行一个操作,update
因为在findById
您获得id
存在于您的D B。 save
inCrudRepository
可以接受单个实体或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());