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/
I didn’t understand the negative, I thought the question was good. Besides, I don’t know if the
persistence.xmlhas to do with Hibernate, this file is not general JPA? Hibernate does not use thehibernate.cfg.xml?– Renan Gomes
I don’t know, I know Hibernate needs him, I know very little about this file.
– gato