I've declared my 2 PUs on persistence.xml as below:
<persistence-unit name="myJpaUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- JPA entities must be registered here -->
<class>MyUserClass</class>...
<persistence-unit name="anotherJpaUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- JPA entities must be registered here -->
<class>MyAnotherClass</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"></property>...
Configured JpaPersistenceModule as below:
new IBSJpaRepositoryModule("myJpaUnit", "anotherJpaUnit")
Binded the repository classes:
protected void bindRepositories(RepositoryBinder binder) {
binder.bind(UserRepository.class).to("myJpaUnit");
binder.bind(TableauUserRepository.class).to("anotherJpaUnit");
}
Repository classes are annotated with @Transacional declaring wich PU it is:
@Transactional(value = "myJpaUnit")
public interface UserRepository extends JpaRepository<User, String>, EntityManagerProvider {
}
@Transactional(value = "anotherJpaUnit", readOnly = true)
public interface TableauUserRepository extends JpaRepository<TableauUser, Integer> {
}
I have only one Entity mapped on "anotherJpaUnit", declared this way:
@PersistenceContext(unitName="anotherJpaUnit")
@Entity(name = "_user")
@Data
public class TableauUser {
@Id
private int id;
@Column(length = 255)
private String name;
@Column(name = "url_namespace", length = 255)
private String urlNamespace;
@Column(length = 255)
private String status;
}
But when I start my application, Guice initialization throws errors:
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: myJpaUnit] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:287)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 38 more
Caused by: org.hibernate.HibernateException: Missing table: _user
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1265)
The table "_user" only exists on "anotherJpaUnit", why it tries to bind with "MyJpaUnit"? I don't know what I'm doing wrong here. Anyone has an example of JpaRepositoryModule with multiple PU working?
Found the problem. We must explicit at persistence.xml to consider only classes declared on it. Not scan all @Entity. See the correct persistence.xml declaration and note the tag.
<persistence-unit name="tableauJpaUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- JPA entities must be registered here -->
<class>MyUserClass</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>......