为什么Spring Data(JPA)的删除方法没有任何返回值?

huangapple go评论74阅读模式
英文:

Why does not delete method of spring data (JPA) have any return values?

问题

我在服务层使用了 Spring Data JPA 的删除方法,但我想知道为什么 deleteById 方法和 delete 方法都没有任何返回值。

如果仔细检查删除方法的实现,会发现有一个if语句,当要删除的实体不存在时,不返回任何内容。

public void delete(T entity) {

    Assert.notNull(entity, "Entity must not be null!");

    if (entityInformation.isNew(entity)) {
        return;
    }

    Class<?> type = ProxyUtils.getUserClass(entity);

    T existing = (T) em.find(type, entityInformation.getId(entity));

    // if the entity to be deleted doesn't exist, delete is a NOOP
    if (existing == null) {
        return;
    }

    em.remove(em.contains(entity) ? entity : em.merge(entity));
}

就个人而言,我认为在这种情况下返回一个 Boolean 值可能是一种适当的方法,因为控制器层将了解删除状态,视图层可以提供更可靠的警告消息。

英文:

I have used a delete method of Spring Data JPA in the service layer, but I wonder why neither the deleteById method nor delete method has any return values.

If we inspect the implementation of the delete method carefully, there is an if statement that when the entity to be deleted doesn't exist returns nothing.

public void delete(T entity) {

	Assert.notNull(entity, &quot;Entity must not be null!&quot;);

	if (entityInformation.isNew(entity)) {
		return;
	}

	Class&lt;?&gt; type = ProxyUtils.getUserClass(entity);

	T existing = (T) em.find(type, entityInformation.getId(entity));

	// if the entity to be deleted doesn&#39;t exist, delete is a NOOP
	if (existing == null) {
		return;
	}

	em.remove(em.contains(entity) ? entity : em.merge(entity));
}

Personally, I think returning a Boolean value could be an adequate approach in this case because the controller layer will know about the deletion status, and the view layer can be provided with the far more reliable alert message.

答案1

得分: 18

Spring Data JPA设计了一些内置方法,它们提供了一种他们认为的方式,并且还给了我们选择使用其他方式的选项。
通过使用Spring Data JPA支持的派生删除查询,您可以轻松获取已删除的记录及其数量(参考链接)。

@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
    Fruit deleteById(Long id); // 获取已删除的记录
}
@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
    Long deleteById(Long id);  // 获取已删除的记录数量
}
英文:

Spring Data JPA design some build-in methods that way they think and give us the option to use the other way also.
You can easily get deleted records and their count using derived delete query supported By Spring Data JPA (Reference)

@Repository
public interface FruitRepository extends JpaRepository&lt;Fruit, Long&gt; {
    Fruit deleteById(Long id); // To get deleted record
}
@Repository
public interface FruitRepository extends JpaRepository&lt;Fruit, Long&gt; {
    Long deleteById(Long id);  // To get deleted record count
}

答案2

得分: 5

使用@Modifying@Query,它将返回删除的行数。

@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
    @Modifying
    @Query(value = "DELETE FROM Fruit f where f.id = ?1")
    int costumDeleteById(Long id);
}
英文:

use @Modifying and @Query ant it will return number of deleted rows.

@Repository
public interface FruitRepository extends JpaRepository&lt;Fruit, Long&gt; {
    @Modifying
    @Query(value = &quot;DELETE FROM Fruit f where f.id = ?1&quot;)
    int costumDeleteById(Long id);
}

答案3

得分: 0

另一个选择是按照这个答案的建议,检查受影响的实体数量是否为1(在使用deleteById方法的情况下)。

英文:

Another option would be to follow the suggestion from this answer and check if the number of affected entities is 1 (in case of a deleteById method).

huangapple
  • 本文由 发表于 2020年4月6日 20:53:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61060324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定