Hibernate persistence.xml examples 

Joined:
04/09/2007
Posts:
784

April 27, 2011 08:28:31    Last update: April 27, 2011 08:37:40
  1. JBoss example with hsql:
    <persistence>
       <persistence-unit name="myapp">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <jta-data-source>java:/DefaultDS</jta-data-source>
          <properties>
             <property name="hibernate.dialect" 
    		   value="org.hibernate.dialect.HSQLDialect"/>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
          </properties>
       </persistence-unit>
    </persistence>
    

  2. MySQL example with JDBC:
    <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_1_0.xsd"
        version="1.0">
    
        <persistence-unit name="myapp">
        <properties>
    	<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
    	<property name="hibernate.hbm2ddl.auto" value="create"/>
    	<property name="hibernate.archive.autodetection" value="class, hbm"/>
    	<property name="hibernate.show_sql" value="true"/>
    	<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
    	<property name="hibernate.connection.password" value="<password>"/>
    	<property name="hibernate.connection.url" value="jdbc:mysql://<host_name>:<port_number>/<database_name>"/>
    	<property name="hibernate.connection.username" value="<username>"/>
    	<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    	<property name="hibernate.c3p0.min_size" value="5"/>
    	<property name="hibernate.c3p0.max_size" value="20"/>
    	<property name="hibernate.c3p0.timeout" value="300"/>
    	<property name="hibernate.c3p0.max_statements" value="50"/>
    	<property name="hibernate.c3p0.idle_test_period" value="3000"/>
        </properties>
        </persistence-unit>
    </persistence>
    

  3. With OpenEJB transaction manager:
    <persistence version="1.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_1_0.xsd">
    
        <persistence-unit name="myapp">
    	<provider>org.hibernate.ejb.HibernatePersistence</provider>
    	<jta-data-source>java:/jtaDS</jta-data-source>
    	<non-jta-data-source>java:/nonjtaDS</non-jta-data-source>
    
    	<properties>
    	    <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    	    <property name="hibernate.transaction.manager_lookup_class"
    		      value="org.apache.openejb.hibernate.TransactionManagerLookup"/>
    	</properties>
        </persistence-unit>
    </persistence>
    


  4. With EclipseLink:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.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_1_0.xsd">
    	<persistence-unit name="comics">
    		<class>eclipselinkrcp.Publisher</class>
    		<properties>
    			<property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
    			<property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/sample;create=true"/>
    			<property name="eclipselink.jdbc.user" value="app"/>
    			<property name="eclipselink.jdbc.password" value="app"/>
     
    			<property name="eclipselink.jdbc.read-connections.min" value="1" />
    			<property name="eclipselink.jdbc.write-connections.min" value="1" />
    			<property name="eclipselink.jdbc.batch-writing" value="JDBC" />
     
    			<!-- Logging -->
    			<property name="eclipselink.logging.level" value="FINE" />
    			<property name="eclipselink.logging.timestamp" value="false" />
    			<property name="eclipselink.logging.session" value="false" />
    			<property name="eclipselink.logging.thread" value="false" />
    		</properties>
     
    	</persistence-unit>
    </persistence>
    

Share |
| Comment  | Tags