JPA存储库未发送删除语句

时间:2015-10-18 14:33:21

标签: spring spring-data-jpa

我有一个集成测试在子实体上执行删除操作,但是在日志中看不到delete语句,当然测试失败。

日志说:

2015-10-18 16:19:28,633 DEBUG  [TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource] Adding transactional method 'deleteById' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2015-10-18 16:19:28,633 DEBUG  [JpaTransactionManager] Creating new transaction with name [com.thalasoft.data.jpa.repository.GenericRepositoryImpl.deleteById]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2015-10-18 16:19:28,634 DEBUG  [JpaTransactionManager] Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@5e020dd1] for JPA transaction
2015-10-18 16:19:28,635 DEBUG  [JpaTransactionManager] Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@f0c1ae1]
Hibernate:
    select
        userrole0_.id as id1_1_0_,
        userrole0_.version as version2_1_0_,
        userrole0_.role as role3_1_0_,
        userrole0_.user_id as user_id4_1_0_,
        user1_.id as id1_0_1_,
        user1_.version as version2_0_1_,
        user1_.confirmed_email as confirme3_0_1_,
        user1_.email as email4_0_1_,
        user1_.firstname as firstnam5_0_1_,
        user1_.lastname as lastname6_0_1_,
        user1_.password as password7_0_1_,
        user1_.password_salt as password8_0_1_,
        user1_.readable_password as readable9_0_1_,
        user1_.work_phone as work_ph10_0_1_
    from
        user_role userrole0_
    inner join
        user_account user1_
            on userrole0_.user_id=user1_.id
    where
        userrole0_.id=?
2015-10-18 16:19:28,636 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
3. select userrole0_.id as id1_1_0_, userrole0_.version as version2_1_0_, userrole0_.role as role3_1_0_,
userrole0_.user_id as user_id4_1_0_, user1_.id as id1_0_1_, user1_.version as version2_0_1_,
user1_.confirmed_email as confirme3_0_1_, user1_.email as email4_0_1_, user1_.firstname as
firstnam5_0_1_, user1_.lastname as lastname6_0_1_, user1_.password as password7_0_1_, user1_.password_salt
as password8_0_1_, user1_.readable_password as readable9_0_1_, user1_.work_phone as work_ph10_0_1_
from user_role userrole0_ inner join user_account user1_ on userrole0_.user_id=user1_.id where
userrole0_.id=3183
2015-10-18 16:19:28,636 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
3. select userrole0_.id as id1_1_0_, userrole0_.version as version2_1_0_, userrole0_.role as role3_1_0_,
userrole0_.user_id as user_id4_1_0_, user1_.id as id1_0_1_, user1_.version as version2_0_1_,
user1_.confirmed_email as confirme3_0_1_, user1_.email as email4_0_1_, user1_.firstname as
firstnam5_0_1_, user1_.lastname as lastname6_0_1_, user1_.password as password7_0_1_, user1_.password_salt
as password8_0_1_, user1_.readable_password as readable9_0_1_, user1_.work_phone as work_ph10_0_1_
from user_role userrole0_ inner join user_account user1_ on userrole0_.user_id=user1_.id where
userrole0_.id=3183
Hibernate:
    select
        userroles0_.user_id as user_id4_0_0_,
        userroles0_.id as id1_1_0_,
        userroles0_.id as id1_1_1_,
        userroles0_.version as version2_1_1_,
        userroles0_.role as role3_1_1_,
        userroles0_.user_id as user_id4_1_1_
    from
        user_role userroles0_
    where
        userroles0_.user_id=?
2015-10-18 16:19:28,654 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
3. select userroles0_.user_id as user_id4_0_0_, userroles0_.id as id1_1_0_, userroles0_.id as
id1_1_1_, userroles0_.version as version2_1_1_, userroles0_.role as role3_1_1_, userroles0_.user_id
as user_id4_1_1_ from user_role userroles0_ where userroles0_.user_id=6842
2015-10-18 16:19:28,654 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
3. select userroles0_.user_id as user_id4_0_0_, userroles0_.id as id1_1_0_, userroles0_.id as
id1_1_1_, userroles0_.version as version2_1_1_, userroles0_.role as role3_1_1_, userroles0_.user_id
as user_id4_1_1_ from user_role userroles0_ where userroles0_.user_id=6842
2015-10-18 16:19:28,664 DEBUG  [JpaTransactionManager] Initiating transaction commit
2015-10-18 16:19:28,665 DEBUG  [JpaTransactionManager] Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@5e020dd1]
2015-10-18 16:19:28,667 DEBUG  [JpaTransactionManager] Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@5e020dd1] after transaction
2015-10-18 16:19:28,667 DEBUG  [EntityManagerFactoryUtils] Closing JPA EntityManager

测试本身不是交易性的:

@Test
public void testDeleteByUserRoleId() {
    UserRole loadedUserRole = userRoleRepository.findOne(userRole0.getId());
    assertNotNull(loadedUserRole);
    loadedUserRole = userRoleRepository.deleteById(userRole0.getId());
    UserRole myloadedUserRole = userRoleRepository.findOne(userRole0.getId());
    assertNull(myloadedUserRole);
}

域名是:

@Entity
@SequenceGenerator(name = "id_generator", sequenceName = "sq_id_user_role")
public class UserRole extends BaseEntity {

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    @JsonBackReference
    private User user;
    @Column(nullable = false)
    private String role;

    public UserRole() {
    }

    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getRole() {
        return this.role;
    }

    public void setRole(String role) {
        this.role = role;
    }

}

@Entity
@Table(name = "user_account")
@SequenceGenerator(name = "id_generator", sequenceName = "sq_id_user")
public class User extends BaseEntity {

  @Column(nullable = false)
  private String firstname;
  @Column(nullable = false)
  private String lastname;
  @Column(nullable = false, unique = true)
  private EmailAddress email;
  @Column(nullable = false)
  private boolean confirmedEmail;
  @Column(length = 100)
  private String password;
  @Column(length = 50)
  private String passwordSalt;
  @Column(length = 50)
  private String readablePassword;
  private String workPhone;
  @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user", fetch = FetchType.EAGER)
  @JsonManagedReference
  private Set<UserRole> userRoles = new HashSet<>();

  public User() {
  }

  public String getFirstname() {
    return firstname;
  }

  public void setFirstname(String firstname) {
    this.firstname = firstname;
  }

  public String getLastname() {
    return lastname;
  }

  public void setLastname(String lastname) {
    this.lastname = lastname;
  }

  public EmailAddress getEmail() {
    return email;
  }

  public void setEmail(EmailAddress email) {
    this.email = email;
  }

  public boolean isConfirmedEmail() {
    return confirmedEmail;
  }

  public void setConfirmedEmail(boolean confirmedEmail) {
    this.confirmedEmail = confirmedEmail;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getPasswordSalt() {
    return passwordSalt;
  }

  public void setPasswordSalt(String passwordSalt) {
    this.passwordSalt = passwordSalt;
  }

  public String getReadablePassword() {
    return readablePassword;
  }

  public void setReadablePassword(String readablePassword) {
    this.readablePassword = readablePassword;
  }

  public String getWorkPhone() {
    return workPhone;
  }

  public void setWorkPhone(String workPhone) {
    this.workPhone = workPhone;
  }

  public Set<UserRole> getUserRoles() {
    return userRoles;
  }

  public void setUserRoles(Set<UserRole> userRoles) {
    if (this.userRoles == null) {
      this.userRoles = userRoles;
    } else {
      this.userRoles.clear();
      this.userRoles.addAll(userRoles);
    }
  }

}

如果我将它与一个非常相似的集成测试进行比较,这个删除用户实体而不是用户角色,日志显示它正在调用delete语句:

2015-10-18 16:19:08,394 DEBUG  [TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource] Adding transactional method 'deleteById' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2015-10-18 16:19:08,395 DEBUG  [JpaTransactionManager] Creating new transaction with name [com.thalasoft.data.jpa.repository.GenericRepositoryImpl.deleteById]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2015-10-18 16:19:08,395 DEBUG  [JpaTransactionManager] Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4086d8fb] for JPA transaction
2015-10-18 16:19:08,396 DEBUG  [JpaTransactionManager] Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@2e3572e8]
Hibernate:
    select
        user0_.id as id1_0_0_,
        user0_.version as version2_0_0_,
        user0_.confirmed_email as confirme3_0_0_,
        user0_.email as email4_0_0_,
        user0_.firstname as firstnam5_0_0_,
        user0_.lastname as lastname6_0_0_,
        user0_.password as password7_0_0_,
        user0_.password_salt as password8_0_0_,
        user0_.readable_password as readable9_0_0_,
        user0_.work_phone as work_ph10_0_0_,
        userroles1_.user_id as user_id4_0_1_,
        userroles1_.id as id1_1_1_,
        userroles1_.id as id1_1_2_,
        userroles1_.version as version2_1_2_,
        userroles1_.role as role3_1_2_,
        userroles1_.user_id as user_id4_1_2_
    from
        user_account user0_
    left outer join
        user_role userroles1_
            on user0_.id=userroles1_.user_id
    where
        user0_.id=?
2015-10-18 16:19:08,403 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
3. select user0_.id as id1_0_0_, user0_.version as version2_0_0_, user0_.confirmed_email as confirme3_0_0_,
user0_.email as email4_0_0_, user0_.firstname as firstnam5_0_0_, user0_.lastname as lastname6_0_0_,
user0_.password as password7_0_0_, user0_.password_salt as password8_0_0_, user0_.readable_password
as readable9_0_0_, user0_.work_phone as work_ph10_0_0_, userroles1_.user_id as user_id4_0_1_,
userroles1_.id as id1_1_1_, userroles1_.id as id1_1_2_, userroles1_.version as version2_1_2_,
userroles1_.role as role3_1_2_, userroles1_.user_id as user_id4_1_2_ from user_account user0_
left outer join user_role userroles1_ on user0_.id=userroles1_.user_id where user0_.id=6486
2015-10-18 16:19:08,403 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
3. select user0_.id as id1_0_0_, user0_.version as version2_0_0_, user0_.confirmed_email as confirme3_0_0_,
user0_.email as email4_0_0_, user0_.firstname as firstnam5_0_0_, user0_.lastname as lastname6_0_0_,
user0_.password as password7_0_0_, user0_.password_salt as password8_0_0_, user0_.readable_password
as readable9_0_0_, user0_.work_phone as work_ph10_0_0_, userroles1_.user_id as user_id4_0_1_,
userroles1_.id as id1_1_1_, userroles1_.id as id1_1_2_, userroles1_.version as version2_1_2_,
userroles1_.role as role3_1_2_, userroles1_.user_id as user_id4_1_2_ from user_account user0_
left outer join user_role userroles1_ on user0_.id=userroles1_.user_id where user0_.id=6486
2015-10-18 16:19:08,426 DEBUG  [JpaTransactionManager] Initiating transaction commit
2015-10-18 16:19:08,426 DEBUG  [JpaTransactionManager] Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4086d8fb]
Hibernate:
    delete
    from
        user_account
    where
        id=?
        and version=?
2015-10-18 16:19:08,435 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147)
3. delete from user_account where id=6486 and version=0
2015-10-18 16:19:08,435 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147)
3. delete from user_account where id=6486 and version=0
2015-10-18 16:19:08,449 DEBUG  [JpaTransactionManager] Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4086d8fb] after transaction
2015-10-18 16:19:08,449 DEBUG  [EntityManagerFactoryUtils] Closing JPA EntityManager
2015-10-18 16:19:08,450 DEBUG  [JpaTransactionManager] Creating new transaction with name [com.thalasoft.data.jpa.repository.GenericRepositoryImpl.findOne]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
2015-10-18 16:19:08,451 DEBUG  [JpaTransactionManager] Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@1f3b992] for JPA transaction
2015-10-18 16:19:08,451 DEBUG  [DataSourceUtils] Setting JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@6b63e6ad] read-only
2015-10-18 16:19:08,454 DEBUG  [JpaTransactionManager] Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@7beae796]
Hibernate:
    select
        user0_.id as id1_0_0_,
        user0_.version as version2_0_0_,
        user0_.confirmed_email as confirme3_0_0_,
        user0_.email as email4_0_0_,
        user0_.firstname as firstnam5_0_0_,
        user0_.lastname as lastname6_0_0_,
        user0_.password as password7_0_0_,
        user0_.password_salt as password8_0_0_,
        user0_.readable_password as readable9_0_0_,
        user0_.work_phone as work_ph10_0_0_,
        userroles1_.user_id as user_id4_0_1_,
        userroles1_.id as id1_1_1_,
        userroles1_.id as id1_1_2_,
        userroles1_.version as version2_1_2_,
        userroles1_.role as role3_1_2_,
        userroles1_.user_id as user_id4_1_2_
    from
        user_account user0_
    left outer join
        user_role userroles1_
            on user0_.id=userroles1_.user_id
    where
        user0_.id=?
2015-10-18 16:19:08,456 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
3. select user0_.id as id1_0_0_, user0_.version as version2_0_0_, user0_.confirmed_email as confirme3_0_0_,
user0_.email as email4_0_0_, user0_.firstname as firstnam5_0_0_, user0_.lastname as lastname6_0_0_,
user0_.password as password7_0_0_, user0_.password_salt as password8_0_0_, user0_.readable_password
as readable9_0_0_, user0_.work_phone as work_ph10_0_0_, userroles1_.user_id as user_id4_0_1_,
userroles1_.id as id1_1_1_, userroles1_.id as id1_1_2_, userroles1_.version as version2_1_2_,
userroles1_.role as role3_1_2_, userroles1_.user_id as user_id4_1_2_ from user_account user0_
left outer join user_role userroles1_ on user0_.id=userroles1_.user_id where user0_.id=6486

2015-10-18 16:19:08,456 DEBUG  [sqlonly]  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)
3. select user0_.id as id1_0_0_, user0_.version as version2_0_0_, user0_.confirmed_email as confirme3_0_0_,
user0_.email as email4_0_0_, user0_.firstname as firstnam5_0_0_, user0_.lastname as lastname6_0_0_,
user0_.password as password7_0_0_, user0_.password_salt as password8_0_0_, user0_.readable_password
as readable9_0_0_, user0_.work_phone as work_ph10_0_0_, userroles1_.user_id as user_id4_0_1_,
userroles1_.id as id1_1_1_, userroles1_.id as id1_1_2_, userroles1_.version as version2_1_2_,
userroles1_.role as role3_1_2_, userroles1_.user_id as user_id4_1_2_ from user_account user0_
left outer join user_role userroles1_ on user0_.id=userroles1_.user_id where user0_.id=6486

2015-10-18 16:19:08,463 DEBUG  [JpaTransactionManager] Initiating transaction commit
2015-10-18 16:19:08,464 DEBUG  [JpaTransactionManager] Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@1f3b992]
2015-10-18 16:19:08,465 DEBUG  [DataSourceUtils] Resetting read-only flag of JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@6b63e6ad]
2015-10-18 16:19:08,466 DEBUG  [JpaTransactionManager] Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@1f3b992] after transaction
2015-10-18 16:19:08,466 DEBUG  [EntityManagerFactoryUtils] Closing JPA EntityManager

请注意,deleteById方法是事务性的,并且由用户和用户角色删除集成测试使用:

@Override
@Transactional
public T deleteById(ID id) throws EntityNotFoundException {
    T entity = findOne(id);
    if (entity != null) {
        delete(entity);
    } else {
        throw new EntityNotFoundException("The entity could not be found and was not deleted");
    }
    return entity;
}

2 个答案:

答案 0 :(得分:0)

在删除陈述

后添加entityManager.flush()
@PersistenceContext
EntityManager em;

@Test
public void testDeleteByUserRoleId() {
    UserRole loadedUserRole = userRoleRepository.findOne(userRole0.getId());
    assertNotNull(loadedUserRole);
    loadedUserRole = userRoleRepository.deleteById(userRole0.getId());

    em.flush();    

    UserRole myloadedUserRole = userRoleRepository.findOne(userRole0.getId());
    assertNull(myloadedUserRole);
}

答案 1 :(得分:0)

这并不是真正的解决方案,而是更多有关此问题的更新。

将项目升级到Spring Boot 2 2.0.3.RELEASE并使用assertEquals(Optional.empty(),myloadedUserRole);后,测试成功通过。

@Test
public void testDeleteById() {
    Optional<UserRole> loadedUserRole = userRoleRepository.findById(userRole0.getId());
    assertNotNull(loadedUserRole.get());
    UserRole deletedUserRole = userRoleRepository.deleteByUserRoleId(userRole0.getId());
    Optional<UserRole> myloadedUserRole = userRoleRepository.findById(userRole0.getId());
    assertEquals(Optional.empty(),myloadedUserRole);
}