The
META-INF/persistence.xml
configuration file in the ejb/src/main/resources
folder in the jboss-ejb3-ejb
subproject was created when we created the Java EE project. The persistence.xml
specifies a persistence provider to be used to map object/relational entities to the database. Specify that, the persistence unit is using the persistence-unit
element. Set the transaction-type
to JTA
(the default value). Specify the persistence provider as the Hibernate persistence provider: org.hibernate.ejb.HibernatePersistence
. Set the jta-data-source
element value to the java:jboss/datasources/MySQLDS
data source, which we created earlier. Specify the entity classes using the class
element. The DDL generation strategy is set to create-drop
using the hibernate.hbm2ddl.auto
property, which automatically validates or exports the DDL schema to the database, when the SessionFactory
class is created. With the create-drop
strategy, the required tables are created and dropped when the SessionFactory
is closed. The hibernate.show_sql
property is set to false
. Setting it to true
implies that all SQL statements be the output, which is an alternative method to debug. The hibernate.dialect
property is set to org.hibernate.dialect.MySQLDialect
for MySQL Database. Other Hibernate properties (http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html) can also be specified as required. The persistence.xml configuration file is listed in the following code:
<?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" version="2.0" xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="em" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- If you are running in a production environment, add a managed data source, the example data source is just for development and testing! --> <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source> <class>org.jboss.ejb3.model.Article</class> <class>org.jboss.ejb3.model.Catalog</class> <class>org.jboss.ejb3.model.Edition</class> <class>org.jboss.ejb3.model.Section</class> <properties> <!-- Properties for Hibernate --> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> </properties> </persistence-unit> </persistence>
The JPA specification does not mandate a persistence provider to create tables with the hibernate.hbm2ddl.auto
property set to create-drop
or create
. Hibernate persistence provider supports creating tables. In addition to the entity tables, some additional tables (such as the join tables and the sequence table) are created by the Hibernate persistence provider.