2015-06-16 16 views
0

我有一個春天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代碼庫之前,有什麼辦法「準備連接」?

+0

通過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 –

+0

這很有意思,現在試着... – h22

+0

如果你可以得到當前的EntityManager的句柄,那麼你應該能夠得到底層連接: http://stackoverflow.com/questions/4148231/how-can-i-get-the-session-object-if-i-have-the-entitymanager –

回答

1

使用AOP似乎是一種方法:使用AOP豐富春數據倉庫的例子可以在下面找到:

https://github.com/spring-projects/spring-data-jpa-examples

如果你能得到內注入的EntityManager參考建議,那麼你應該能夠得到從這裏詳述的方法,使用一個底層連接:

How can i get the session object if i have the entitymanager

要到EntityMan參考埃傑您可能必須創建從所有倉庫繼承自定義庫:

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-behaviour-for-all-repositories

相關問題