我有一個春天CrudRepository
那只是一個接口,而我在那裏我已經定義我的數據源的持久化上下文類:是否有可能有「連接的準備」同春/ JPA持續性
@Configuration
@EnableTransactionManagement
public class PersistenceContext {
@Bean(name="dataSource", destroyMethod = "close")
public DataSource dataSource() throws SQLException {
return ...
public interface DataRepository extends CrudRepository<Data, Long> {
Data findById(long id);
}
然後
@Autowired
protected DataRepository repository;
...
Data data = repository.findById(1234);
一切工作正常,但數據庫模型是這樣的,我確實需要從使用代碼調用findById
之前調用同一連接對存儲過程。這個過程必須接受一個調用代碼將會知道的參數,但是它會在調用之間有所不同,所以不可能只是重寫DataSource.getConnection並返回「準備好的連接」。
在訪問Spring代碼庫之前,有什麼辦法「準備連接」?
通過AOP?請參閱這裏包括「如何使用AOP豐富存儲庫的示例」https://github.com/spring-projects/spring-data-jpa-examples/blob/master/spring-data-jpa-interceptors/src/main /java/org/spring/data/jpa/sample/interceptors/ApplicationConfiguration.java –
這很有意思,現在試着... – h22
如果你可以得到當前的EntityManager的句柄,那麼你應該能夠得到底層連接: http://stackoverflow.com/questions/4148231/how-can-i-get-the-session-object-if-i-have-the-entitymanager –