[英]JPA Repository Inheritance - Extend Multiple Interfaces
我的JPA存儲庫擴展了一個自定義接口,該接口帶有用於以通用方式處理授權的注釋。
public interface MultiTenantCrudRepo<T, ID> extends CrudRepository<T, ID>
此接口將@ PreAuthorize,@ PostAuthorize,@ PreFilter和@PostFilter批注添加到CrudRepository的方法中。
此外,對於某些實體,我需要實現軟刪除。 為此,我創建了一個“ SoftDeleteRepository”,如下所示:
public interface SoftDeleteRepository<T extends BaseEntity<I> & SoftDeletable, I extends Serializable> extends CrudRepository<T, I> {
@Query("update #{#entityName} e set e.isDeleted = true where e.id = ?#{#entity.id}")
@Modifying
@Override
public void delete(@Param("entity") T entity);
您可以看到它添加了@Query批注以實現我需要的功能。
這兩個接口都可以按預期獨立工作,但是當存儲庫需要這樣的兩個屬性(授權和軟刪除)時,
public interface FooRepo extends SoftDeleteRepository<Foo, Long>, MultiTenantCrudRepo<Foo, Long> {
似乎只有第一個接口的注釋在“擴展”之后才有效。 因此,在這種情況下,我得到了一個支持軟偏轉但沒有授權驗證的FooRepo。
使兩者同時工作的最佳方法是什么?
猜測這是一件棘手的事情,因為它實際上是Java不支持的多繼承事物,例如參見this 。
例如,如果有兩個相同的注釋具有不同的參數,該怎么辦?
許多框架(例如Spring數據)在檢查注釋的繼承時都很好,但是只有在沒有多重繼承和/或具有相同注釋的情況下才能猜測。 這些框架可能使用反射來進入“實現樹”,但由於上述原因或者如果實施得當會引發異常,它們可能只會選擇一條路徑。
因此,恐怕您需要執行以下操作:
public interface SoftDeleteMultitenantRepository
extends MultiTenantCrudRepo<Foo, Long> {
// a copy of your soft delete method here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.