How to set default schema name in JPA configuration?

I found that in the hibernate configuration file, you can configure the hibernate.default_schema parameter:

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

Now I am using JPA and I want to do the same. Otherwise, I need to add a schema parameter to each @Table annotation, for example:

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

As I understand it, this parameter should be somewhere in this part of the 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 not find his name in google. Any ideas?

+57
java spring hibernate jpa configuration
Apr 29 '10 at 12:55
source share
6 answers

I do not know the JPA properties for this. But you can just add the Hibernate property (assuming you are using Hibernate as a provider) as

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

Hibernation should choose this

+75
Apr 29 '10 at 13:11
source share

Just to save time for people who come to mail (for example, I am looking for a Spring configuration type and want the schema name to be set by an external source (properties file)). The configuration will work for you

 <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 hibernate.hdm2ddl.auto, you can see the possible values in the Hibernate hbm2ddl.auto message and what do they do? I used to create create-update, because it is convenient. However, in production, I think it’s better to take control of ddl, so I take everything that ddl will generate for the first time, save it, and not let it automatically create and update.

+34
Nov 09 2018-12-12T00:
source share

To avoid the hard coding scheme in Java Java JPA classes, we used the orm.xml mapping file in the Java EE application deployed to OracleApplicationServer10 (OC4J, Orion). It lies in model.jar / META-INF / as well as persistence.xml. The orm.xml mapping file refers to peresistence.xml with the tag

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

The contents of the orm.xml file are shown 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> 
+26
Apr 27 '11 at 13:45
source share

For other users using spring-boot, a java based configuration,

I set the value of the schema in application.properties

 spring.jpa.properties.hibernate.dialect=... spring.jpa.properties.hibernate.default_schema=... 
+15
May 11 '17 at 9:12 a.m.
source share

For those using the latest spring boot, this will help:

.properties:

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

.yml:

 spring: jpa: properties: hibernate: default_schema: <name of your schema> 
+1
Jun 09 '19 at 16:31 on
source share

Use it

 @Table (name = "Test", schema = "\"schema\"") 

instead of @Table (name = "Test", schema = "schema")

If you are on postgresql, the request is:

 SELECT * FROM "schema".test 

no:

 SELECT * FROM schema.test 

PS: test is a table

0
Sep 25 '19 at 21:36
source share



All Articles