Using javax.persistence.Table @Table annotation in roo project, how can I use maven (or spring) to set the schema from a build profile?

StackOverflow https://stackoverflow.com/questions/14898316

Question

Pretty much what it says in the title. I would like to have maven (or roo) manage which schema the @Table annotation uses at build time.

I tried

@Table(schema = ${env.schema},name = "MYTABLE")

but came to the conclusion that this wasn't how maven was meant to work.

Was it helpful?

Solution

@Table is a JPA annotation, it has nothing to do with Roo (a tool for creating applications) or Maven (a tool for compile applications, including the management of the dependencies)

If the schema is the same in all the environments, just use the name (the only option available for the attribute of the annotation)

However, it's possible to achieve what you're asking for with Spring: to have schema names parametrized.

You can configure the jpaProperties attribute in org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean

For instance, in Hibernate set hibernate.default_schema to a variable whose value can be managed by a PropertyPlaceHolderConfigurer (in a database.properties external file or an environment property)

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaProperties">
       <props>
        <prop key="hibernate.default_schema">${database.schema}</prop>
    </property>
</bean>

The value is not obtained from a build profile, but I hope it helps you. I've been using this approach for a long time.

OTHER TIPS

Could you set up another persistence unit and leave schema out of your table annotation? I don't know how the roo shell would react to that though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top