2014-09-04 54 views
1

我宣佈我的2層上的persistence.xml如下的PU:吉斯JpaRepositoryModule與多個持久化單元

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

但是,當我開始我的應用程序,吉斯初始化拋出錯誤:

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」?我不知道我在這裏做錯了什麼。任何人都有一個JpaRepositoryModule的例子與多個PU工作?

回答

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

絕對是一個很好的瞭解。 – lumpynose 2014-10-15 17:53:05