How to change the default schema of postgresql in persistence.xml in a java application

3

I'm developing an application and I'm having a hard time changing the default application schema. I'm using PostgreSQL in a Java application with JPA and Hibernate as an implementation.

persistence.xml

<persistence-unit name="comunicaVisual" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>entys.Pessoa</class>

    <exclude-unlisted-classes>true</exclude-unlisted-classes>

    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/postgres" />
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
        <property name="javax.persistence.jdbc.user" value="postgres" />
        <property name="javax.persistence.jdbc.password" value="1" />

        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

        <property name="hibernate.cache.use_query_cache" value="true" />
        <property name="hibernate.cache.region.factory_class"
            value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
    </properties>
</persistence-unit>

I want to put the application instead of the default public scheme put as another name for example

<property name="javax.persistence.jdbc.user" value="postgres/dremcom_drem" />

but I'm not having success with the attempts that way.

    
asked by anonymous 19.04.2014 / 23:38

1 answer

4

I think you have been missing the property that handles schema selection

would be something like

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

Depending on the case, you may eventually need to do this setup using the
tag      persistence-unit-metadata. Take a look at this other issue here from the same OS that talks about it: link

In this case your procedure would look something like this:

1) Create a file called orm.xml, save it to the same folder where your persistence.xml is and add the following content to it

<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 orm_2_0.xsd"
version="2.0">
    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <schema>NOME_DO_SEU_SCHEMA</schema>
        </persistence-unit-defaults>
    </persistence-unit-metadata>   
</entity-mappings>

2) Modify the contents of your persistence.xml to reference orm.xml, something like this:

<persistence-unit name="comunicaVisual" transaction-type="RESOURCE_LOCAL">

 <mapping-file>orm.xml</mapping-file>   
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
 <class>entys.Pessoa</class>
     <!--- restante das configuraçoes seguem aqui -->

</persistence-unit>

3) Deploy and test the application again

A final hypothesis would be to name the schema via annotations in the entity itself, but this is unnecessary if your application only accesses a database

@Entity
@Table(name = "sua-tablea", schema = "nome-do-schema")
    
19.04.2014 / 23:43