使用继承时如何防止休眠状态删除子级?

时间:2018-08-13 18:18:17

标签: hibernate

我有这两节课:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@SQLDelete(sql = "UPDATE products SET deleted_at = now() WHERE id = ?")
@Table(name = "products")
public class Product extends DateAudit {

这:

@Entity
@Table(name = "tshirts")
public class Tshirt extends Product {

我有两个不同的表。一种用于产品,另一种用于T恤。我不想从T恤中删除任何内容。我只需要在产品上设置Deleted_at就可以了。但是,当我尝试删除产品时,会发生以下情况:

2018-08-13 11:13:52.632 DEBUG 22788 --- [nio-5000-exec-3] org.hibernate.SQL                        : delete from tshirts where id=?
2018-08-13 11:13:52.633 DEBUG 22788 --- [nio-5000-exec-3] org.hibernate.SQL                        : UPDATE products SET deleted_at = now() WHERE id = ?

我尝试将其添加到我的Tshirt子类中,但仍然得到相同的“从tshirt中删除”:

@OnDelete(action = OnDeleteAction.NO_ACTION)

这就是我调用delete方法的方式:

@Autowired
    private ProductRepository productRepository;
    @Override
        public void delete(Product product) {
            productRepository.delete(product);
        }

这是productRepository的实现:

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
    List<Product> findByStoreId(Long id);
    Boolean existsByIdAndStoreId(Long id, Long storeId);
    Page<Product> findByStoreIdAndNameContainsOrSkuContains(Long storeId, String name, String sku, Pageable pageRequest);
    Page<Product> findByStoreId(Long storeId, Pageable pageRequest);
    Page<Product> findByStoreIdAndInStock(Long storeId, Boolean inStock, Pageable pageRequest);
    Product findByIdAndStoreId(Long productId, Long storeId);
}

0 个答案:

没有答案