2016-09-21 65 views
0

我有一个Hibernate/Spring 4应用程序,其中已经配置了实体类的默认架构,例如,他们最终被映射为testschema.tableprodschema.table将Hibernate实体额外架构设置为默认架构配置

然而,还有另一个实体类需要它自己的可配置模式,例如,只有该实体必须映射到testschema2.anothertableprodschema2.anothertable

尼斯将是这样的:

@Entity 
@Table(name="anothertable", schema = "${db.AntherEntitySchema}") 
public class AnotherEntity { 
    // .. 
} 

在模式会从属性文件注射,但这样的功能似乎只与@Value注释工作。 任何想法如何进行?

回答

0

我们这个applicationContextPersistence.ctx.xml

<bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="persistenceXmlLocation" 
       value="classpath:META-INF/persistence-${env}.xml" /> 
    <!-- .. --> 
</bean> 

这里春天可以从属性文件替换为$ {}表达式解决它。

然后我们有一个持久化dev.xml等其特点是特定的ORM dev.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence 
    version="2.0" 
    xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
         http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
    <persistence-unit name="fooUnit" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
     <mapping-file>META-INF/orm-dev.xml</mapping-file> 
    </persistence-unit> 
    <!-- .. --> 
</persistence> 

的ORM-dev.xml现在是实体映射负责:

<?xml version="1.0" encoding="UTF-8" ?> 

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm  
    http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" 
    version="1.0"> 

    <description> XML Mapping file</description> 

    <package>foo.server.model</package> 

    <entity class="AnotherEntity"> 
     <table schema="testschema2" name="anothertable" /> 
     <attributes> 
      <!-- .. --> 
     </attributes> 
    </entity> 
</entity-mappings> 

最后我们从AnotherEntity POJO中移除映射注释,因为现在通过orm-dev.xml文件映射这个映射注释。其他的实体类保留了他们的注释。

注意:我们使用Eclipse的Spring Tool Suite风格。此IDE需要一个persistence.xml,因此为了摆脱错误消息,我们保留了一个最小的persistence.xml,不必记住停用验证程序的IDE选项。