Is there a possibility inside spring boot to write a custom delete method with an autowired bean inside it. I will try to explain it:
Basically, what I currently have is:
When my database-asset is deleted, I want to be able to also delete/modify my bucket-asset.
Idealy, I would like to be able to do:
@Repository
public abstract class AssetJpaRepository implements JpaRepository<Asset, Long> {
public abstract Asset findByUuid(String uuid);
public abstract Asset findOneByFriendlyName(String friendlyName);
@Autowired
private BlobContainerClient assetContainerClient;
@Override
public void delete(Asset asset) {
BlobClient assetClient = assetContainerClient.getBlobClient(asset.getFileReference());
assetClient.delete();
}
}
The problem now is that Spring Boot won't initialize AssetJpaRepository as a repository.
Is there a way I can do this cleanly?
Note: deleting the bucket-asset on service level is not an option, since I have parent entities which will call the asset delete method when the parent is deleted.
Thanks in advance!
首先,这不是一个好方法,您应该创建 AssetJpaRepository Interface
,然后创建一个单独的类并实现此Interface
其次,您在方法顶部创建带有@Query
(删除查询)注释的Native Query
。
You can do it as another approach.
In AssetJpaRepository.java
public interface AssetJpaRepository
implements JpaRepository<Asset, Long>, AssetJpaRepositoryCustom {
Asset findByUuid(String uuid);
Asset findOneByFriendlyName(String friendlyName);
}
In AssetJpaRepositoryCustom.java
public interface AssetJpaRepository implements AssetJpaRepositoryCustom {
void delete(Asset asset);
}
In AssetJpaRepositoryImpl.java
public class AssetJpaRepositoryImpl implements AssetJpaRepositoryCustom {
private final BlobContainerClient assetContainerClient;
public BlobContainerClient(BlobContainerClient assetContainerClient) {
this.assetContainerClient = assetContainerClient;
}
@Override
public void delete(Asset asset) {
BlobClient assetClient = assetContainerClient.getBlobClient(asset.getFileReference());
assetClient.delete();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.