Back Forum Reply New

Usage of PersistenceAnnotationBeanPostProcessor

Dear all,

I have two questions to ask about using JPA with Spring, which is related to PersistenceAnnotationBeanPostProcessor.

1)
From the documents, I know that I can annotate my EntityManager attribute in my bean with @PersistenceContext, and having PersistenceAnnotationBeanPostProcessor in my app context config.  By doing so, an EntityManager will be injected to my bean, and I can use it directly and Spring will handle transaction issue of different thread.

I would want to know, if it is possible to do the injection manually? I have cases that I want to use the same DAO to instantiate two instances, each will refer to a different DB. Therefore I cannot statically state which persistence unit I am using, in the DAO.

2)
I am encountering some problem in my project and when I print the thread stack trace, I saw something is happening inside PersistenceAnnotationBeanPostProcessor which cause my program halt when startup.  Therefore I tried to remove (just for testing purpose) PersistenceAnnotationBeanPostProcessor from my app context.  However, my program continue to halt and I saw PersistenceAnnotationBeanPostProcessor in the thread stack trace, even it is no longer in my app context.

Can anyone give me idea why this post processor appears even I don't have it in my app context config?  Do any bean create it and register it automatically?

Thanks a lot

Adrian

Can anyone give me idea why this post processor appears even I don't have it in my app context config? Do any bean create it and register it automatically?

Yes. If you use context:annotation-config or context:component-scan that bean will be registered for you.

I would want to know, if it is possible to do the injection manually? I have cases that I want to use the same DAO to instantiate two instances, each will refer to a different DB. Therefore I cannot statically state which persistence unit I am using, in the DAO.

You can specify a name when using the @PersistenceContext, that name will be used to lookup the bean from the context.


Originally Posted by Marten DeinumYes. If you use context:annotation-config or context:component-scan that bean will be registered for you.

You mean having such in the appContext.xml?
However, seems I have not use such features...
(I used lt;sa:annotation-autoload /gt; and lt;tx:annotation-driven/gt; , will them cause similar effect?)

You can specify a name when using the @PersistenceContext, that name will be used to lookup the bean from the context.

Maybe I didn't make my scenario clear   

For example, I have a FooDaoImpl which use EntityManager for persistence; and I have two separate database with same schema.   Those two DBs will correspond to two different persistence units.  

My application wants to have two instances of FooDaoImpl created, each pointing to one of the DBs, can this be achieved?

I cannot put the persistence unit in @PersistenceContext because I want same class (but two instances) being used with two DBs.

Thanks a lot  

Haven't see reply on this topic...  

In fact my application is going to connect to two  databases with identical schema.  I'd like to use same DAO class to access data from each DB.

I guess what Spring injected to @PersistenceContext annotated EntityManager field is somehow a special proxy class which do lookup to the real EntityManagerFactory when it is used.  I wonder if I can self-instantiate this special EntityManager proxy and inject manually to my DAO, so that I can control (in runtime) which DB each DAO is connecting to.

Is there really no way to solve?  I wanna avoid creating a empty child class of each DAO just to annotate its EntityManager to a different persistence unit

After some search in Spring's API, it sounds that org..orm.jpa.support.SharedEntityMa  nagerBean seems what I am looking for.

Just wanna see if anyone can give me some hints that if that is the right direction?  What I am looking for is:  An entity manager that I can inject to my own bean manually in my app context config, which work in the same way as @PersistenceContext injected ones.
¥
Back Forum Reply New