Home  >  JPA

JPA persistence.xml Example

By Arvind Rai, September 06, 2013
persistence.xml handles the configuration related task. persistence.xml file is kept in META-INF. We configure our entities in persistence.xml. All the database related configuration is done in persistence.xml. Database related configuration like database driver, user, password, database connection URL are configured in persistence.xml. We also need to configure provider for JPA.

persistence-unit in persistence.xml

All the configuration are done inside persistence-unit. We need to provide a name so that it can be accessed in application. EntityManagerFactory uses persistence-unit name to access persistence.xml. Find the sample file of persistence.xml.

persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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="com.concretepage">
        <description>JPA Test</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.concretepage.entity.Farmer</class> 
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpadb"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value=""/>
        </properties>
    </persistence-unit>
</persistence>
 


Download the source code which is a demo for end to end use of persistence.xml with a JPA application.

Download Source Code
jpa-persistence-xml-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us