What is the purpose of the "persistence.xml" file?

Asked

Viewed 5,194 times

3

I’m studying about the Hibernate, And along the way I came up with several doubts, one of my doubts, and that is important for my learning, is about the purpose of the archive persistence.xml which is my unit of persistence, I have read about it, but I cannot understand its purpose.

I would like to know what is the purpose of this file in relation to my application and what is the relevance it has in my project?

  • 1

    I didn’t understand the negative, I thought the question was good. Besides, I don’t know if the persistence.xml has to do with Hibernate, this file is not general JPA? Hibernate does not use the hibernate.cfg.xml?

  • I don’t know, I know Hibernate needs him, I know very little about this file.

2 answers

3


The persistence.xml is a configuration file used by JPA, Hibernate is one of many implementations of JPA. The contents of this file have information like connection url, user, password. Besides containing mapping of the classes that became the tables, and this mapping is done through the annotations.

Example of a file persistence.xml:

 <?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="Hello" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/Hello"/>
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

In <persistence-unit name=”Hello” transaction-type=”RESOURCE_LOCAL”> two important settings are defined here.

In unit name defines for the application the configuration that will be used. In the same file we can have several different types, such as transaction-type that defines how our transaction type will be, in our case local. If it were a web application we would not need to control the transaction, containner (Jboss, Glassfish,) would do this job.

In <provider>org.hibernate.ejb.HibernatePersistence</provider> indicates which will be the previous of our application. In our case, Hibernate.

The option hibernate.show_sql enables the display of the generated SQL in the console.

In javax.persistence.jdbc.driver define which will be the connection agent with the database we will be using, we will use Mysql, javax.persistence.jdbc.url the URL of our database along with the schema of our database.

In javax.persistence.jdbc.user and javax.persistence.jdbc.password database user and password. These attributes were defined in the Mysql installation. I used the values root and root.

In hibernate.dialect the dialect that Hibernate will use. Let’s discuss more about this option in future posts, for example, how to generate database script automatically.

This option hibernate.hbm2ddl.auto set as enabled, Hibernate will update its tables when necessary. For example, if the table does not exist it will create (CREATE TABLE), if you added a new column (ALTER TABLE).

References:
http://www.inf.ufg.br/~Fabrizzio/as/aulas/aula7.pdf
http://uaihebert.com/tutorial-hibernate-3-com-jpa-2/

  • Yes, it is part of the JPA implementation.

  • Yes, it is in this file that the JPA settings are

1

Persistence.xml is a file for JPA configuration, Hibernate is one of the implementations, either for Hibernate or another JPA implementation if it is necessary to use this file, I do not know what was your doubt, but that’s there; This brings details of these settings is used the eclipseLink as implementation, but I believe it can help http://uaihebert.com/jpa-mini-livro-primeiros-passos-e-conceitos-detalhados/4/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.