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>
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.
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.
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.
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).
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With