Spring Data JPA 插入而不是更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27673615/
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
Spring Data JPA inserting instead of Update
提问by Rana_S
Hi I am new to Sprig Data JPAand I am wondering even though I pass the Id to the entity, the Spring data jpa is inserting instead of merge. I thought when I implement the Persistableinterface and implement the two methods:
嗨,我是Sprig Data JPA 的新手,我想知道即使我将 Id 传递给实体,Spring 数据 jpa 还是插入而不是合并。我在实现Persistable接口的时候就想到了,实现了两个方法:
public Long getId();
public Boolean isNew();
It will automatically merge instead of persist.
它将自动合并而不是持久化。
I have an entity class called User like:
我有一个名为 User 的实体类,例如:
@Entity
@Table(name = "T_USER")
public class User implements Serializable, Persistable<Long> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "USER_ID")
private Long id;
@Column(name = "CREATION_TIME", nullable = false)
private Date creationTime;
@Column(name = "FIRST_NAME", nullable = false)
private String firstName;
@Column(name = "LAST_NAME", nullable = false)
private String lastName;
@Column(name = "MODIFICATION_TIME", nullable = false)
private Date modificationTime;
And have another class
再上一堂课
@Entity
@Table(name = "T_USER_ROLE")
public class UserRole implements Serializable, Persistable<Long> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long roleId;
@Column(name = "ROLE_NAME")
private String userRole;
}
I have a custom repository called UserRepostory extending JpaReopistory. I am hitting the save for merge and persist as I see the implementation demonstrate that Spring Data Jpa uses above two methods to either update or insert.
我有一个名为 UserRepostory 的自定义存储库,用于扩展 JpaReopistory。当我看到实现表明 Spring Data Jpa 使用上述两种方法来更新或插入时,我正在点击保存合并和持久化。
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
I have been trying to figure out but didn't get any clue. Maybe you guys can help.
我一直试图弄清楚,但没有得到任何线索。也许你们可以帮忙。
回答by John Shields
I ran into this issue, tried to implement Persistable
to no avail, and then looked into the Spring Data JPA source. I don't necessarily see this in your example code, but I have a @Version
field in my entity. If there is a @Version
field Spring Data will test thatvalue to determine if the entity is new or not. If the @Version
field is not a primitive and is null then the entity is considered new.
遇到这个问题,尝试实现Persistable
无果,然后查看Spring Data JPA源码。我不一定会在您的示例代码中看到这一点,但@Version
我的实体中有一个字段。如果有一个@Version
字段 Spring Data 将测试该值以确定实体是否是新的。如果该@Version
字段不是原始字段并且为空,则该实体被认为是新的。
This threw me for a long time in my tests because I was not setting the version field in my representation but only on the persisted entity. I also don't see this documented in the otherwise helpful Spring Data docs (which is another issue...).
这让我在测试中花了很长时间,因为我没有在我的表示中设置 version 字段,而只在持久化实体上设置。我也没有在其他有用的 Spring Data 文档中看到这一点(这是另一个问题......)。
Hope that helps someone!
希望对某人有所帮助!
回答by njjnex
By default Spring Data JPA inspects the identifier property of the given entity. If the identifier property is null
, then the entity will be assumed as new, otherwise as not new. It's Id-Property inspection
Reference
默认情况下,Spring Data JPA 检查给定实体的 identifier 属性。如果标识符属性是null
,则实体将被假定为新的,否则为不是新的。这是Id-Property inspection
参考
If you are using Spring JPA with EntityManager
calling .merge()
will update your entity and .persist()
will insert.
如果您使用带有EntityManager
调用的Spring JPA.merge()
将更新您的实体并.persist()
插入。
@PersistenceContext
private EntityManager em;
@Override
@Transactional
public User save(User user) {
if (user.getId() == null) {
em.persist(user);
return user;
} else {
return em.merge(user);
}
}
There is no need to implement the Persistable
interface.
不需要实现Persistable
接口。
回答by Edor Linus
Just implement equals and hashCode methods for your entity class and JpaRepository will work as expected.
只需为您的实体类实现 equals 和 hashCode 方法,JpaRepository 就会按预期工作。
Hope this helps someone.
希望这可以帮助某人。