Announcement Announcement Module
No announcement yet.
Dynamically decide on an EntityManager for a Repository Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamically decide on an EntityManager for a Repository

    Hey guys,

    I've got a spring setup with 2 types of EntityManagerFactories, one for transaction scoped entity managers, one for extended entity managers. They each have a different persistent unit name.
    I've also got 2 transactionmanagers, each is tied to one of the factories.

    I've also got a Repository with an entitymanager field, annotated with @PersistenceContext.
    When making a call to the repository, it will get a transaction scoped entity manager, because I've set up a default like so:

    <bean class=" enceAnnotationBeanPostProcessor" >
    <property name="defaultPersistenceUnitName" value="..."/>

    Up until now, I've only needed the repository in the way is was set up. But now, I need the repository from within a transactional POJO which has been annotated with the second type of transactionmanager (the one which is tied to the factory bean for extended EM's).

    I don't feel like making a copy of the repository, annotated with @PersistenceContext with the other persistence unit name. (Actually, I've got a bunch of repositories, so that would mean a bunch of copies.)

    Is there any way to dynamically decide on which entitymanager the repository will get. I'ld even be helped with just knowing which type of transaction I'm in, so I could composition away the entitymanager and use a strategy pattern. Or can I rely on a different kind of configuration (as opposed to having an @PersistenceContext annotated field) to easily solve my problem?

    I've noticed that the TransactionSynchronizationManager has a resource, a Proxy to the EM factory bean, that I could use, but that feels like kindof a hack. I feel that I'm on the wrong track and that I should be able to do things much simpler, but I'm not seeing it :-)

    Thanks in advance,