gpt4 book ai didi

Spring Jpa 将自定义功能添加到所有存储库,同时将其他自定义功能添加到单个存储库

转载 作者:行者123 更新时间:2023-12-04 10:39:16 28 4
gpt4 key购买 nike

此处的 Spring 文档 http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.custom-implementations给出了将自定义功能添加到所有存储库或单个存储库的示例,而不是两者。

假设我想将一些自定义函数添加到所有存储库(使用自定义存储库工厂 Bean)和其他一些仅添加到单个存储库(文档说使用自定义接口(interface)和自定义 Impl);我怎样才能做到这一点?

我在所有存储库中添加了“setCurrentTenansInSession”方法的一些示例代码;现在我想添加一个自定义方法,例如“newCustomMethod”,到单个存储库(即 MyJpaRepository,对于我的自定义存储库工厂)。我该怎么做呢?

自定义行为接口(interface):

@NoRepositoryBean
public interface MyJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
public void setCurrentTenantInSession(Object object);
}

自定义行为实现:
public class MultiTenantSimpleJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements MyJpaRepository<T, ID> {
public void setCurrentTenantInSession(Object object) {
//custom impl
}
}

自定义存储库工厂 bean:
public class MultiTenantJpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T, S, ID> {

@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new MultiTenantJpaRepositoryFactory(entityManager);
}
}

最后是自定义存储库工厂:
public class MultiTenantJpaRepositoryFactory extends JpaRepositoryFactory {
public MultiTenantJpaRepositoryFactory(EntityManager entityManager) {
super(entityManager);
}

@Override
protected JpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata, EntityManager entityManager) {
final JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());

final SimpleJpaRepository<?, ?> repo = new MultiTenantSimpleJpaRepository(entityInformation, entityManager);

repo.setLockMetadataProvider(LockModeRepositoryPostProcessor.INSTANCE.getLockMetadataProvider());
return repo;
}

@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return MultiTenantSimpleJpaRepository.class;
}
}

最佳答案

您只需要结合您提到的文档页面上的方法。让 Car成为您想要为其拥有自定义存储库的实体。
CommonCustomRepository定义添加到所有 repos 的方法:

@NoRepositoryBean
public interface CommonCustomRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
String getCustomValue();
}

这个 repo 的实现:
public class CommonCustomRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements CommonCustomRepository<T, ID> {

public CommonCustomRepositoryImpl(Class<T> domainClass, EntityManager em) {
super(domainClass, em);
}

public CommonCustomRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
}

@Override
public String getCustomValue() {
return "CustomValue";
}

}
CarRepository 的自定义方法
@NoRepositoryBean
public interface CustomCarRepository {

public String getCustomCarValue();
}

定制车相关方法的实现
public class CarRepositoryImpl implements CustomCarRepository {

@PersistenceContext
private EntityManager em;

@Override
public String getCustomCarValue() {
return "CustomCarValue";
}
}
CarRepository 的组合接口(interface)
public interface CarRepository extends CommonCustomRepository<Car, Long>, CustomCarRepository {
}

自定义仓库工厂,就像在文档中一样
public class CustomRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends
JpaRepositoryFactoryBean<R, T, I> {

@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {

return new CustomRepositoryFactory(entityManager);
}

private static class CustomRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {

private EntityManager entityManager;

public CustomRepositoryFactory(EntityManager entityManager) {
super(entityManager);

this.entityManager = entityManager;
}

@Override
protected Object getTargetRepository(RepositoryMetadata metadata) {

return new CommonCustomRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager);
}

@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {

// The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
// to check for QueryDslJpaRepository's which is out of scope.
return CommonCustomRepositoryImpl.class;
}
}
}

最后一点配置,就像在文档中一样
<jpa:repositories base-package="com.example" factory-class="com.example.CustomRepositoryFactoryBean"/>

关于Spring Jpa 将自定义功能添加到所有存储库,同时将其他自定义功能添加到单个存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19765684/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com