Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring JpaRepository-分离和附着实体_Java_Spring_Hibernate_Jpa_Spring Data Jpa - Fatal编程技术网

Java Spring JpaRepository-分离和附着实体

Java Spring JpaRepository-分离和附着实体,java,spring,hibernate,jpa,spring-data-jpa,Java,Spring,Hibernate,Jpa,Spring Data Jpa,我在jpa上使用SpringBoot和hibernate。我正在使用JpaRepository接口来实现我的存储库。与以下UserRepository一样 public interface UserRepository extends JpaRepository<User, Long> { } 谢谢entityManager.clear()将断开所有JPA对象的连接,因此,如果您有其他对象计划保持连接,那么在所有情况下,这可能不是合适的解决方案 清除 entityManager.d

我在jpa上使用SpringBoot和hibernate。我正在使用JpaRepository接口来实现我的存储库。与以下UserRepository一样

public interface UserRepository extends JpaRepository<User, Long> {
}
谢谢

entityManager.clear()
将断开所有JPA对象的连接,因此,如果您有其他对象计划保持连接,那么在所有情况下,这可能不是合适的解决方案

清除

entityManager.detach(实体)从持久性上下文中删除给定实体

分离


如果您使用的是JPA2.0,那么可以使用从持久性上下文分离单个实体。此外,Hibernate还有一个相同的功能

由于
JpaRepository
本身不提供此功能,因此您可以对其进行类似的修改

public interface UserRepositoryCustom {
    ...
   void detachUser(User u);
    ...
}

public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {
    ...
}

@Repository
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
    ...
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void detachUser(User u) {
        entityManager.detach(u);
    }
    ...
}
公共接口UserRepositoryCustom{ ... 无效用户(用户u); ... } 公共接口UserRepository扩展了JpaRepository、UserRepositoryCustom{ ... } @存储库 公共类UserRepositoryCustomImpl实现UserRepositoryCustom{ ... @持久上下文 私人实体管理者实体管理者; @凌驾 公共用户(用户u){ 实体管理器分离(u); } ... }

我还没有尝试过这个代码,但是你应该能够让它工作。您甚至可以尝试使用
@PersistenceContext
在您的服务类(其中
updateUser()
是)中控制
EntityManager
,避免将自定义实现添加到存储库中的麻烦。

使用@Predrag Maric建议的自定义实现显然是这个问题的正确答案。然而,我发现在服务层进行分离要好得多,因为它通常知道实体是否应该分离

只需在服务中连接
@PersistenceContext

@Service
class ConsumerServiceImpl {

    @PersistenceContext
    private EntityManager entityManager
...

    entityManager.detach(en)


您应该发布步骤1至4的当前代码(以澄清)是否可以按以下顺序进行调用:1、3、4、2?谢谢,这种方法在我的Spring Boot应用程序中有效。(在示例代码中将
@Repository
添加到实现中;否则自定义存储库无法自动连接。)Crudepository可以使用相同的方法吗?@DBS没有尝试,但应该是相同的
/**
 * Remove the given entity from the persistence context, causing
 * a managed entity to become detached.  Unflushed changes made
 * to the entity if any (including removal of the entity),
 * will not be synchronized to the database.  Entities which
 * previously referenced the detached entity will continue to
 * reference it.
 * @param entity  entity instance
 * @throws IllegalArgumentException if the instance is not an
 *         entity
 * @since Java Persistence 2.0
 */
public void detach(Object entity);
public interface UserRepositoryCustom {
    ...
   void detachUser(User u);
    ...
}

public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {
    ...
}

@Repository
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
    ...
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void detachUser(User u) {
        entityManager.detach(u);
    }
    ...
}
@Service
class ConsumerServiceImpl {

    @PersistenceContext
    private EntityManager entityManager
...

    entityManager.detach(en)