How to set up default schema name in JPA configuration?

182,209

Solution 1

Don't know of JPA property for this either. But you could just add the Hibernate property (assuming you use Hibernate as provider) as

...

<property name="hibernate.default_schema" value="myschema"/>

...

Hibernate should pick that up

Solution 2

Just to save time of people who come to the post (like me, who looking for Spring config type and want you schema name be set by an external source (property file)). The configuration will work for you is

<bean id="domainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="JiraManager"/>
    <property name="dataSource" ref="domainDataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false"/>
            <property name="showSql" value="false"/>
            <property name="databasePlatform" value="${hibernate.dialect}"/>
        </bean>
    </property>
    <property name="jpaProperties">
       <props>
            <prop key="hibernate.hbm2ddl.auto">none</prop>
            <prop key="hibernate.default_schema">${yourSchema}</prop>
       </props>
</property>

</bean>

Ps : For the hibernate.hdm2ddl.auto, you could look in the post Hibernate hbm2ddl.auto possible values and what they do? I have used to set create-update,because it is convenient. However, in production, I think it is better to take control of the ddl, so I take whatever ddl generate first time, save it, rather than let it automatically create and update.

Solution 3

In order to avoid hardcoding schema in JPA Entity Java Classes we used orm.xml mapping file in Java EE application deployed in OracleApplicationServer10 (OC4J,Orion). It lays in model.jar/META-INF/ as well as persistence.xml. Mapping file orm.xml is referenced from peresistence.xml with tag

...
  <persistence-unit name="MySchemaPU"  transaction-type="JTA">
    <provider>...</provider>
    <mapping-file>META-INF/orm.xml</mapping-file>
...

File orm.xml content is cited below:

<?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">

  <persistence-unit-metadata>
    <persistence-unit-defaults>
      <schema>myschema</schema>
    </persistence-unit-defaults>
  </persistence-unit-metadata>
</entity-mappings>

Solution 4

For others who use spring-boot, java based configuration,

I set the schema value in application.properties

spring.jpa.properties.hibernate.dialect=...
spring.jpa.properties.hibernate.default_schema=...

Solution 5

For those who uses last versions of spring boot will help this:

.properties:

spring.jpa.properties.hibernate.default_schema=<name of your schema>

.yml:

spring:
    jpa:
        properties:
            hibernate:
                default_schema: <name of your schema>
Share:
182,209
Roman
Author by

Roman

Updated on July 05, 2022

Comments

  • Roman
    Roman almost 2 years

    I found that in hibernate config file we could set up parameter hibernate.default_schema:

    <hibernate-configuration> 
       <session-factory>
          ...
          <property name="hibernate.default_schema">myschema</property>
          ...
       </session-factory>
    </hibernate-configuration>
    

    Now I'm using JPA and I want to do the same. Otherwise I have to add parameter schema to each @Table annotation like:

    @Entity
    @Table (name = "projectcategory", schema = "SCHEMANAME")
    public class Category implements Serializable { ... }
    

    As I understand this parameter should be somewhere in this part of configuration:

    <bean id="domainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="JiraManager"/>
        <property name="dataSource" ref="domainDataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="false"/>
                <property name="showSql" value="false"/>
                <property name="databasePlatform" value="${hibernate.dialect}"/>
            </bean>
        </property>
    </bean>
    
    <bean id="domainDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${db.driver}" />
        <property name="jdbcUrl" value="${datasource.url}" />
        <property name="user" value="${datasource.username}" />
        <property name="password" value="${datasource.password}" />
        <property name="initialPoolSize" value="5"/>
        <property name="minPoolSize" value="5"/>
        <property name="maxPoolSize" value="15"/>
        <property name="checkoutTimeout" value="10000"/>
        <property name="maxStatements" value="150"/>
        <property name="testConnectionOnCheckin" value="true"/>
        <property name="idleConnectionTestPeriod" value="50"/>
    </bean>
    

    ... but I can't find its name in google. Any ideas?

  • Roman
    Roman about 14 years
    Should be added to <properties> section in persistent unit in persistence.xml.
  • bert
    bert about 14 years
    Sorry, did not see that you have no persistence.xml but use Spring to configure Hibernate/JPA. I would try to add this under the HibernateJpaVendorAdapter as a property. Perhaps even without the "hibernate." in front of the name.
  • Isaac
    Isaac about 10 years
    I'd prefer this solution over the others as it is standard and not implementation-specific.
  • Chexpir
    Chexpir about 10 years
    The problem here is I can't use a property as in the Spring files, as ${jdbc.schema} or similar.
  • JJ Roman
    JJ Roman over 9 years
    I got parseexceptino - needed to remove provider element from persistence-unit <persistence-unit name="MySchemaPU" transaction-type="JTA"> <mapping-file>META-INF/orm.xml</mapping-file> ...
  • Alfredo M
    Alfredo M almost 6 years
    in @formula elements, hibernate is not including the shema, and makes the query fails in sqlserver stackoverflow.com/questions/51088034/…