我创建了一个自定义存储库以覆盖持久性方法,并尝试按照spring docs中的说明进行连接。我没有收到任何错误,所有实体和存储库都在启动时找到,并且当我调用repo.saveAll(entities)时,持久性正常工作。但是,从未调用我的自定义代码。我在代码中添加了日志语句,甚至引发了RuntimeExceptions,只是为了查看它是否正在执行,但是肯定会忽略它。我错过了哪一步?
@Configuration
@Profile("test")
@EnableJpaRepositories(repositoryBaseClass = SetClientInfoRepositoryImpl.class,
basePackages = {"gov.penndot.hwy.apras.common.repository" },
entityManagerFactoryRef = "serviceEntityManagerFactory",
transactionManagerRef = "serviceTransactionManager")
public class TestDatabaseConfig {
@Bean(name = "serviceDataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa");
dataSource.setPassword("sa");
return dataSource;
}
@Bean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<String, Object>(), null);
}
@Bean(name = "serviceEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean serviceEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("serviceDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("stuff")
.persistenceUnit("service")
.build();
}
@Bean(name = "serviceTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("serviceEntityManagerFactory") EntityManagerFactory serviceEntityManagerFactory) {
return new JpaTransactionManager(serviceEntityManagerFactory);
}
存储库:
@NoRepositoryBean
public class SetClientInfoRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> {
private static final Logger log = LoggerFactory.getLogger(SetClientInfoRepositoryImpl.class);
private final EntityManager em;
public SetClientInfoRepositoryImpl(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.em = entityManager;
}
@Transactional
@Override
public <S extends T> S save(S entity) {
setClientInfo();
return super.save(entity);
}
@Transactional
@Override
public void deleteById(ID id) {
setClientInfo();
super.deleteById(id);
}
@Transactional
@Override
public void delete(T entity) {
setClientInfo();
super.delete(entity);
}
@Transactional
@Override
public void deleteAll(Iterable<? extends T> entities) {
setClientInfo();
super.deleteAll(entities);
}
@Transactional
@Override
public void deleteInBatch(Iterable<T> entities) {
setClientInfo();
super.deleteInBatch(entities);
}
@Transactional
@Override
public void deleteAll() {
setClientInfo();
super.deleteAll();
}
@Transactional
@Override
public void deleteAllInBatch() {
setClientInfo();
super.deleteAllInBatch();
}
@Transactional
@Override
public <S extends T> S saveAndFlush(S entity) {
setClientInfo();
return super.saveAndFlush(entity);
}
@Transactional
@Override
public <S extends T> List<S> saveAll(Iterable<S> entities) {
setClientInfo();
super.saveAll(entities);
throw new RuntimeException("foo");
}
private void setClientInfo() {
log.debug("Entering setClientInfo method");
[stuff]
}
}
答案 0 :(得分:0)
好的,这是一个绝望的主意,但是值得一试...
创建自定义存储库界面:
public interface SetClientInfoRepository<T, ID> extends JpaRepository<T, ID> {
}
通过您的自定义基本存储库来实现此存储库界面:
public class SetClientInfoRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements SetClientInfoRepository<T, ID> {
}
...,最后通过您的存储库接口而不是JpaRepository
通过这种方式,Spring 必须从您的实现中创建存储库代理,因为它无法使用其他任何类。另外,如果由于某种原因它不能创建存储库,则在启动过程中会得到更多信息。
使用自定义存储库接口本身并不是一件坏事,因为您总是有很大的机会希望以后向存储库中添加一些通用的自定义方法,然后它会派上用场。