I wanted to know if the {save}
method in CrudRepository
do an update if it finds already the entry in the database like :
我想知道如果CrudRepository中的{save}方法已经在数据库中找到了条目,那么它是否会进行更新:
@Repository
public interface ProjectDAO extends CrudRepository<Project, Integer> {}
@Service
public class ProjectServiceImpl {
@Autowired private ProjectDAO pDAO;
public void save(Project p) { pDAO.save(p); } }
So if I call that method on an already registred entry, it'll update it if it finds a changed attribute ?
因此,如果我在已注册的条目上调用该方法,它会在找到更改的属性时更新它吗?
Thanks.
谢谢。
5 个解决方案
#1
10
I wanted to know if the {save} method in CrudRepository do an update if it finds already the entry in the database
我想知道如果CrudRepository中的{save}方法已找到数据库中的条目,它是否会进行更新
The Spring documentation about it is not precise :
关于它的Spring文档不准确:
Saves a given entity. Use the returned instance for further operations as the save operation might have changed the entity instance completely.
保存给定的实体。使用返回的实例进行进一步操作,因为save操作可能已完全更改了实体实例。
But as the CrudRepository
interface doesn't propose another method with an explicit naming for updating an entity, we may suppose that yes since CRUD is expected to do all CRUD operations (CREATE, READ, UPDATE, DELETE).
但由于CrudRepository接口没有提出另一种具有更新实体的显式命名的方法,我们可能会认为是,因为CRUD应该执行所有CRUD操作(CREATE,READ,UPDATE,DELETE)。
This supposition is confirmed by the implementation of the SimpleJpaRepository
class which is the default implementation of CrudRepository
which shows that both cases are handled by the method :
这个假设通过SimpleJpaRepository类的实现来确认,SimpleJpaRepository类是CrudRepository的默认实现,它表明这两种情况都由方法处理:
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
So if I call that method on an already registered entry, it'll update it if it finds a changed attribute?
因此,如果我在已注册的条目上调用该方法,它会在找到更改的属性时更新它吗?
It will do a merge operation in this case. So all fields are updated according to how the merging cascade and read-only option are set.
在这种情况下,它将执行合并操作。因此,根据如何设置合并级联和只读选项来更新所有字段。
#2
7
Looking at the default implemantation of CrudRepository interface
查看CrudRepository接口的默认实现
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
*/
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
Save method manage two situations:
Save方法管理两种情况:
-If the person Id is null (a new entity is created) then save will call persist method => insert query will be executed.
- 如果人员Id为空(创建新实体),则保存将调用persist方法=>将执行插入查询。
-If the person id is not null then save will call merge: fetch the existing entity from entityManagerFactory(from the 2 level cache if it doesn't exist then it will be fetched from the database) and comparing the detached entity with the managed and finally propagate the changes to the database by calling update query.
- 如果person id不为null,则save将调用merge:从entityManagerFactory获取现有实体(如果它不存在则从2级缓存获取,然后从数据库中获取)并将分离的实体与managed和最后通过调用更新查询将更改传播到数据库。
#3
0
I wanted to know if the {save} method in CrudRepository do an update if it finds already the entry in the database:
我想知道如果CrudRepository中的{save}方法已经找到数据库中的条目,它是否会进行更新:
The Answer is Yes, It will update if it finds an entry:
答案是肯定的,如果找到条目,它将更新:
From Spring Documentation: Herehttps://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html?
来自Spring Documentation:Herehttps://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html?
Saving an entity can be performed via the CrudRepository.save(…)-Method. It will persist or merge the given entity using the underlying JPA EntityManager. If the entity has not been persisted yet Spring Data JPA will save the entity via a call to the entityManager.persist(…)-Method, otherwise the entityManager.merge(…)-Method will be called.
可以通过CrudRepository.save(...)-Method执行保存实体。它将使用基础JPA EntityManager持久化或合并给定实体。如果实体尚未持久化,Spring Data JPA将通过调用entityManager.persist(...)-Method来保存实体,否则将调用entityManager.merge(...)-Method。
#4
0
To be precise, the save(obj) method will treat obj as a new record if the id is empty (therefore will do an insert) and will treat obj as an existing record if the id is filled in (therefore will do the merge).
Why is this important?
Let's say the Project object contains an auto-generated id and also a person_id which must be unique. You make a Project object and fill in the person_id but not the id and then try to save. Hibernate will try to insert this record, since the id is empty, but if that person exists in the database already, you will get a duplicate key exception.
How to handle
Either do a findByPersonId(id) to check if the obj is in the db already, and get the id from that if it is found,
Or just try the save and catch the exception in which case you know it's in the db already and you need to get and set the id before saving.
确切地说,如果id为空(因此将执行插入),则save(obj)方法将obj视为新记录,并且如果填充了id,则将obj视为现有记录(因此将执行合并) 。为什么这很重要?假设Project对象包含一个自动生成的id以及一个必须唯一的person_id。您创建一个Project对象并填写person_id但不填写id,然后尝试保存。 Hibernate将尝试插入此记录,因为id为空,但如果该人员已存在,则会出现重复的键异常。如何处理或者执行findByPersonId(id)以检查obj是否已经在db中,并从中获取id,如果找到它,或者只是尝试保存并捕获异常,在这种情况下你知道它在db中已经你需要在保存之前获取并设置id。
#5
0
In my case I had to add the id property to the Entity, and put the annotation @Id like this.
在我的情况下,我必须将id属性添加到Entity,并将注释@Id这样。
@Id
private String id;
This way when you get the object has the Id of the entity in the database, and does the Update operation instead of the Create.
这样,当您获得对象具有数据库中实体的Id时,并执行Update操作而不是Create。
#1
10
I wanted to know if the {save} method in CrudRepository do an update if it finds already the entry in the database
我想知道如果CrudRepository中的{save}方法已找到数据库中的条目,它是否会进行更新
The Spring documentation about it is not precise :
关于它的Spring文档不准确:
Saves a given entity. Use the returned instance for further operations as the save operation might have changed the entity instance completely.
保存给定的实体。使用返回的实例进行进一步操作,因为save操作可能已完全更改了实体实例。
But as the CrudRepository
interface doesn't propose another method with an explicit naming for updating an entity, we may suppose that yes since CRUD is expected to do all CRUD operations (CREATE, READ, UPDATE, DELETE).
但由于CrudRepository接口没有提出另一种具有更新实体的显式命名的方法,我们可能会认为是,因为CRUD应该执行所有CRUD操作(CREATE,READ,UPDATE,DELETE)。
This supposition is confirmed by the implementation of the SimpleJpaRepository
class which is the default implementation of CrudRepository
which shows that both cases are handled by the method :
这个假设通过SimpleJpaRepository类的实现来确认,SimpleJpaRepository类是CrudRepository的默认实现,它表明这两种情况都由方法处理:
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
So if I call that method on an already registered entry, it'll update it if it finds a changed attribute?
因此,如果我在已注册的条目上调用该方法,它会在找到更改的属性时更新它吗?
It will do a merge operation in this case. So all fields are updated according to how the merging cascade and read-only option are set.
在这种情况下,它将执行合并操作。因此,根据如何设置合并级联和只读选项来更新所有字段。
#2
7
Looking at the default implemantation of CrudRepository interface
查看CrudRepository接口的默认实现
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
*/
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
Save method manage two situations:
Save方法管理两种情况:
-If the person Id is null (a new entity is created) then save will call persist method => insert query will be executed.
- 如果人员Id为空(创建新实体),则保存将调用persist方法=>将执行插入查询。
-If the person id is not null then save will call merge: fetch the existing entity from entityManagerFactory(from the 2 level cache if it doesn't exist then it will be fetched from the database) and comparing the detached entity with the managed and finally propagate the changes to the database by calling update query.
- 如果person id不为null,则save将调用merge:从entityManagerFactory获取现有实体(如果它不存在则从2级缓存获取,然后从数据库中获取)并将分离的实体与managed和最后通过调用更新查询将更改传播到数据库。
#3
0
I wanted to know if the {save} method in CrudRepository do an update if it finds already the entry in the database:
我想知道如果CrudRepository中的{save}方法已经找到数据库中的条目,它是否会进行更新:
The Answer is Yes, It will update if it finds an entry:
答案是肯定的,如果找到条目,它将更新:
From Spring Documentation: Herehttps://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html?
来自Spring Documentation:Herehttps://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html?
Saving an entity can be performed via the CrudRepository.save(…)-Method. It will persist or merge the given entity using the underlying JPA EntityManager. If the entity has not been persisted yet Spring Data JPA will save the entity via a call to the entityManager.persist(…)-Method, otherwise the entityManager.merge(…)-Method will be called.
可以通过CrudRepository.save(...)-Method执行保存实体。它将使用基础JPA EntityManager持久化或合并给定实体。如果实体尚未持久化,Spring Data JPA将通过调用entityManager.persist(...)-Method来保存实体,否则将调用entityManager.merge(...)-Method。
#4
0
To be precise, the save(obj) method will treat obj as a new record if the id is empty (therefore will do an insert) and will treat obj as an existing record if the id is filled in (therefore will do the merge).
Why is this important?
Let's say the Project object contains an auto-generated id and also a person_id which must be unique. You make a Project object and fill in the person_id but not the id and then try to save. Hibernate will try to insert this record, since the id is empty, but if that person exists in the database already, you will get a duplicate key exception.
How to handle
Either do a findByPersonId(id) to check if the obj is in the db already, and get the id from that if it is found,
Or just try the save and catch the exception in which case you know it's in the db already and you need to get and set the id before saving.
确切地说,如果id为空(因此将执行插入),则save(obj)方法将obj视为新记录,并且如果填充了id,则将obj视为现有记录(因此将执行合并) 。为什么这很重要?假设Project对象包含一个自动生成的id以及一个必须唯一的person_id。您创建一个Project对象并填写person_id但不填写id,然后尝试保存。 Hibernate将尝试插入此记录,因为id为空,但如果该人员已存在,则会出现重复的键异常。如何处理或者执行findByPersonId(id)以检查obj是否已经在db中,并从中获取id,如果找到它,或者只是尝试保存并捕获异常,在这种情况下你知道它在db中已经你需要在保存之前获取并设置id。
#5
0
In my case I had to add the id property to the Entity, and put the annotation @Id like this.
在我的情况下,我必须将id属性添加到Entity,并将注释@Id这样。
@Id
private String id;
This way when you get the object has the Id of the entity in the database, and does the Update operation instead of the Create.
这样,当您获得对象具有数据库中实体的Id时,并执行Update操作而不是Create。