Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a persistence.xml file for JPA and Hibernate?

I'm trying to use Hibernate JPA but I need to create my persistence.xml (so I can use the entity manager correctly). I am unsure of what to create and where to place it.

This is how my hibernate.cfg.xml in 'Core' mode configured. I'm using Eclipse Java EE IDE Web Developers (Indigo Release):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">XXXXXX</property>
        <property name="hibernate.connection.url">jdbc:mysql://<hostname>/<database></property>
        <property name="hibernate.connection.username">XXXXX</property>
        <property name="hibernate.default_schema">XXXXXX</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    </session-factory>
</hibernate-configuration>
like image 692
Valter Silva Avatar asked Aug 31 '11 01:08

Valter Silva


People also ask

Where do we have to create a persistence XML file?

The persistence. xml file is a standard configuration file in JPA. It has to be included in the META-INF directory inside the JAR file that contains the entity beans.

Is persistence xml required for JPA?

xml configuration. You can use JPA with a very short, basic configuration. You only need a persistence element as the root element and a persistence-unit element with a name attribute.

What is the role of the persistence XML file in JPA?

The persistence. xml configuration file is used to configure a given JPA Persistence Unit. The Persistence Unit defines all the metadata required to bootstrap an EntityManagerFactory , like entity mappings, data source, and transaction settings, as well as JPA provider configuration properties.

What is the use of persistence xml in Hibernate?

xml File. This file is used to override the default Hibernate settings and to add support for database types that are not out of the box (OOB database types are Oracle Server, Microsoft SQL Server, and MySQL).


1 Answers

Create a persistence.xml file that resides in the META-INF folder.

Example:

<persistence 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"
         version="2.0">
<persistence-unit name="sample">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <jta-data-source>java:/DefaultDS</jta-data-source>
  <mapping-file>ormap.xml</mapping-file>
  <jar-file>MyApp.jar</jar-file>
  <class>org.acme.Employee</class>
  <class>org.acme.Person</class>
  <class>org.acme.Address</class>
  <properties>
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
     <property name="hibernate.connection.password">XXXXXX</property>
     <property name="hibernate.connection.url">jdbc:mysql://<hostname>/<database></property>
     <property name="hibernate.connection.username">XXXXX</property>
     <property name="hibernate.default_schema">XXXXXX</property>
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   </properties>
  </persistence-unit>
</persistence>
like image 172
nir Avatar answered Oct 20 '22 02:10

nir