我想你想要這個Jpa。
實現此目的的一種方法是使用自定義JpaRepositoryFactoryBean
,將RepositoryProxyPostProcessor
添加到JpaRepositoryFactory
,禁用某些方法。
例如:
@Configuration
@EnableJpaRepositories(repositoryFactoryBeanClass = CustomJpaRepositoryFactoryBean.class)
public class MyConfig {
}
然後像
public class CustomJpaRepositoryFactoryBean extends JpaRepositoryFactoryBean {
@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
JpaRepositoryFactory factory = JpaRepositoryFactory(entityManager);
factory.addRepositoryProxyPostProcessor(new RepositoryProxyPostProcessor() {
@Override
public void postProcess(ProxyFactory factory, RepositoryInformation repositoryInformation) {
factory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if ("unsupportedMethod".equals(invocation.getMethod().getName())) {
throw new UnsupportedOperationException();
}
return invocation.proceed();
}
});
}
});
return factory;
}
}
我同意這個解決方案是,如果你的作品乾淨多了。 – tsachev
我已經嘗試過,它完全工作。 – geoand