Spring Data JPA如何在插入之前忽略选择表 [英] Spring Data JPA how to ignore select table before insert
问题描述
我的实体类具有id字段,并且使用@Id
且其策略为null.默认策略为@GeneratedValue(strategy = GenerationType.AUTO))
.
My Entity Class has id field and it using @Id
and it's strategy is null. Default Strategy is @GeneratedValue(strategy = GenerationType.AUTO))
.
当我调用JPA保存方法时,它总是在插入之前先调用sql select ,查询日志就像下面这样:
when I call the JPA save method, it always invoke sql select before insert into ,query log is like bellow:
首次查询:
Hibernate: select dataeviden0_.id as id1_0_0_, dataeviden0_.block_hash as block_ha2_0_0_ from table1 dataeviden0_ where dataeviden0_.id=?
第二个查询:
Hibernate: insert into table1 (block_hash, id) values (?, ?)
我可以自己控制id,我希望jpa在插入之前忽略select,怎么办?
I could control the id myself, I want the jpa ignore select before insert ,how to do ?
推荐答案
@Alien现在不再删除的答案基本上是正确的.该语句来自使用merge
的Spring Data JPA,如果您使用persist
,则该语句将消失.
The now no longer deleted answer by @Alien is basically correct. The statement comes from Spring Data JPA using merge
and if you'd use persist
it should go away.
为此,您必须在存储库中公开persist
方法.使用自定义,这应该很容易方法实现和注入的EntityManager
.如果仅使用该存储库保存新实例,则可以根据需要甚至为现有的save
方法提供自定义实现.
For this, you'd have to expose the persist
method in your repository. Which should be easy with a custom method implementation and an injected EntityManager
. If you only save new instances with that repository you can even provide the custom implementation for the existing save
method if you want.
这篇关于Spring Data JPA如何在插入之前忽略选择表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!