How to set up default schema name in JPA configuration? How to set up default schema name in JPA configuration? spring spring

How to set up default schema name in JPA configuration?


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


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.


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