简体   繁体   English

如果使用Spring数据存储库(Spring 4),如何从持久性上下文中分离实体

[英]How to detach entity from persistence context, if using Spring Data Repositories (Spring 4)

I need to detach an entity from persistence context, within a spring-boot application. 我需要在Spring Boot应用程序中将实体与持久性上下文分离。

I have the following base repository: 我有以下基本存储库:

interface EntityRepository extends CrudRepository<Entity, Long> 

Obviously this is not offering any detach(..) -operation. 显然,这不提供任何detach(..)操作。 I found an answer, which is actually not working for me: SO Post . 我找到了一个答案,实际上对我不起作用: SO Post

I tried the same, but it seems that my entity is not detached(as if im changing any field, it gets still persisted) 我尝试了相同的操作,但是似乎我的实体没有分离(好像即时通讯正在更改任何字段,它仍然存在)

Custom Repo: 自定义回购:

interface MyCustomEntityRepository {
  void detach(Entity ent)
}

Interface Impl: 接口Impl:

class MyCustomEntityRepositoryImpl implements MyCustomEntityRepository{
     @PersistenceContext
     private EntityManager em;

     public void detach(Entity ent) {
         em.detach(ent);
     }
}

But I cant extend EntityRepository with MyCustomEntityReposity , as this results in: 但是我无法使用MyCustomEntityReposity扩展EntityRepository ,因为这导致:

No property detach found for type Entity! I managed to get it compiled without errors, by not extending EntityRepository. 通过不扩展EntityRepository,我设法使它编译无误。 Also changin CrudRepository to JpaRepository But still my entity is not getting detached, but in linked post, the QA says, that it is working for him/her. 也将JpaRepository CrudRepositoryJpaRepository但我的实体仍然没有脱离,但质量保证人员表示,在链接的帖子中,该实体正在为他(她)工作。 The actual reason for detaching the object, is to be able to perform some validations within an @EntityListener , by checking the currently stored entity in db, with the currently changed entity instance, which should be detached. 分离对象的实际原因是,能够通过使用当前更改的应分离的实体实例检查db中当前存储的实体,从而在@EntityListener执行一些验证。

Does anyone see some errors or give me a clue, what Im doing wrong ? 有谁看到错误或给我一个提示,我在做什么错?

Using: Spring-boot(1.4.0-release), Spring 4, JPA 使用:Spring-boot(1.4.0-发行版),Spring 4,JPA

Does anyone see some errors or give me a clue, what Im doing wrong? 有谁看到错误或给我一个提示,我在做什么错?

Although it is hard to tell, what might be part of your actual problem and what is just sloppy question editing. 尽管很难说,但是什么可能是您实际问题的一部分,什么仅仅是草率的问题编辑。

  1. Your interface does not compile 您的界面无法编译

     interface MyCustomEntityRepository { detach(Entity ent) } 

    should probably be 应该是

     interface MyCustomEntityRepository { void detach(Entity ent) } 
  2. Your implementation of your custom interface should implement the interface: 您对自定义接口的实现应实现以下接口:

     class MyCustomEntityRepositoryImpl { 

    should probably be 应该是

     class MyCustomEntityRepositoryImpl implements MyCustomEntityRepository { 

If problems persist, please show the actual code you are using with the actual exception, including the stacktrace. 如果问题仍然存在,请显示您正在使用的实际代码以及实际异常,包括stacktrace。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM