Hi, I am looking for Spring/Summer - 2015 Internships. I am also interested in full time opportunities starting June 2015. My Resume

Search This Blog

Loading...

Saturday, 6 April 2013

Setting schema information

In all my posts on JPA I have used a PostgreSQL database. The database is composed of multiple schemas (Unlike PostgreSQL, MySQL database supports only a single schema). So we need to specify to our application the schema being used. I did that by setting the schema attribute of the Table annotation.
@Entity()
@Table(name = "OWNER",schema="firstOne")
public class Owner {
This value had to be set in every entity. so if there ever was the need to change the schema being used for execution, or to run the code on a totally different database it meant making changes in the java code. This is not a suitable solution. The JPA team very smartly provides a solution for this scenario:
Create a file named orm.xml and add the below content in it.I placed the file in the same META-INF folder as persistence.xml
<?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>firstOne</schema>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>
Now if we remove the schema attribute and run the code, all works fine. If we need to change the schema, the change simply needs to be in this single xml file. No new compilation is necessary.
the persistence-unit-defaults element allows us to set default values for all the entities. Along with schema, it also includes elements to set the database catalog, how the properties of entities should  be accessed, cascade settings and any entity listeners.
Does the file or the annotation have a higher priority ?
I put different values in the annotation and the xml to see which is preferred. It was seen that the value in the Table annotation was given preference over the xml file.
This file can also be used if we prefer to use xml style mapping of the entities over annotations.

2 comments:

  1. As a general rule XML is to override the values in annotations. DataNucleus JPA certainly does that, and I'd expect the (majority of) other JPA implementations to do the same. Hibernate has a chapter "Overriding metadata through XML" implying the same. So no you can't draw your conclusion *for JPA*.

    ReplyDelete
  2. Hi,
    The thing I observed is that when both values were provided (annotation attribute and xml), the annotation value was given preference.
    The orm.xml file provides mapping metadata defaults and not override values (See http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jpt.doc.user%2Ftask_manage_orm.htm_).
    I guess you are talking of 'entity'-orm.xml files.

    ReplyDelete