1

我在 persistence.xml 上声明了我的 2 个 PU,如下所示:

<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>...

配置 JpaPersistenceModule 如下:

new IBSJpaRepositoryModule("myJpaUnit", "anotherJpaUnit")

绑定存储库类:

protected void bindRepositories(RepositoryBinder binder) {
    binder.bind(UserRepository.class).to("myJpaUnit");

    binder.bind(TableauUserRepository.class).to("anotherJpaUnit");
}

存储库类用 @Transacional 注释,声明它是 PU:

@Transactional(value = "myJpaUnit")
public interface UserRepository extends JpaRepository<User, String>, EntityManagerProvider {

}

@Transactional(value = "anotherJpaUnit", readOnly = true)
public interface TableauUserRepository extends JpaRepository<TableauUser, Integer> {

}

我只有一个实体映射在“anotherJpaUnit”上,这样声明:

@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;
}

但是当我启动我的应用程序时,Guice 初始化会引发错误:

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)

表“_user”仅存在于“anotherJpaUnit”上,为什么它会尝试与“MyJpaUnit”绑定?我不知道我在这里做错了什么。任何人都有多个 PU 工作的 JpaRepositoryModule 示例?

4

1 回答 1

3

发现了问题。我们必须在persistence.xml 中显式地考虑仅在其上声明的类。不扫描所有@Entity。查看正确的 persistence.xml 声明并记下标签。

<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>......
于 2014-09-04T21:45:18.200 回答