代码之家  ›  专栏  ›  技术社区  ›  Gustavo Concon

具有多个持久性单元的Guice JpaRepositoryModule

  •  1
  • Gustavo Concon  · 技术社区  · 10 年前

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

    <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注释,声明其为:

    @Transactional(value = "myJpaUnit")
    public interface UserRepository extends JpaRepository<User, String>, EntityManagerProvider {
    
    }
    
    @Transactional(value = "anotherJpaUnit", readOnly = true)
    public interface TableauUserRepository extends JpaRepository<TableauUser, Integer> {
    
    }
    

    我只有一个实体映射到“另一个JpaUnit”上,声明如下:

    @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”仅存在于“另一个JpaUnit”上,为什么它试图与“MyJpaUnit“绑定?我不知道我在这里做错了什么。任何人都有多个PU工作的JpaRepositoryModule的示例?

    1 回复  |  直到 10 年前
        1
  •  3
  •   Gustavo Concon    10 年前

    发现问题。我们必须在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>......