Java – How to mention persistenceUnitName when packagesToScan property

hibernatejavajpaspring

I have two datasources and two entityManagerFactory instance. I was trying to use the new feature of 3.1 (Bootstrapping JPA entityManagerFactory without persistence.xml by using packagesToScan property).

In order to use the right entity manager factory instance, i have to distinguish using Persistence unit name and defining the PU name in persistence.xml is stopping the spring package scanning feature.

How to give the PU name while using packagesToScan feature?

My question is more duplicate of Is there a way to give persistenceUnitName for Spring's LocalContainerEntityManagerFactoryBean without persistence.xml?

I couldn't find the answer or comment on the above post. So reposting as new question.

Best Answer

If I understand your question correctly, you would like to set the name of the persistenceUnit backing an EntityManagerFactory, when defined without a persistence.xml?

When you declare the entityManagerFactory, there is a persistenceUnitName property that you can set. For example:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
  <property name="dataSource" ref="dataSource"/>

  <property name="persistenceUnitName" value="yourPersistenceUnitName"/>

  <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
  <property name="packagesToScan">
    <list>
      <value>..</value>
      ...
    </list>
  </property>
</bean>