Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>1. Copy the required Hibernate .jars to <code>&lt;tomee-home&gt;/lib</code></h2> <p>According to the documentation ( <a href="http://tomee.apache.org/tomee-and-hibernate.html" rel="noreferrer">http://tomee.apache.org/tomee-and-hibernate.html</a> ), the following ones are sufficient and in fact they worked for me:</p> <pre><code>&lt;tomee-home&gt;/lib/antlr-2.7.7.jar &lt;tomee-home&gt;/lib/dom4j-1.6.1.jar &lt;tomee-home&gt;/lib/hibernate-commons-annotations-4.0.2.Final.jar &lt;tomee-home&gt;/lib/hibernate-core-4.2.21.Final.jar &lt;tomee-home&gt;/lib/hibernate-entitymanager-4.2.21.Final.jar &lt;tomee-home&gt;/lib/hibernate-validator-4.3.2.Final.jar &lt;tomee-home&gt;/lib/javassist-3.18.1-GA.jar &lt;tomee-home&gt;/lib/jboss-logging-3.1.0.GA.jar </code></pre> <p>All these <code>.jars</code> are contained in the Hibernate ORM 4.2.x download ( <a href="http://hibernate.org/orm/" rel="noreferrer">http://hibernate.org/orm/</a> ), except for the Hibernate Validator, which is a separate download ( <a href="http://hibernate.org/validator/" rel="noreferrer">http://hibernate.org/validator/</a> ).</p> <h2>2. Edit your pom.xml</h2> <p>Using the <code>javaee-api</code> maven artifact with a scope of <code>provided</code> you can now use the JPA specification in your project. However, if you have been using some Hibernate specific features, classes or annotations before, you can still refer to Hibernate in your <code>pom.xml</code> to match those dependencies:</p> <pre class="lang-xml prettyprint-override"><code>&lt;!-- JPA spec (required) --&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.openejb&lt;/groupId&gt; &lt;artifactId&gt;javaee-api&lt;/artifactId&gt; &lt;version&gt;6.0-4&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;!-- Hibernate specific features (only if needed) --&gt; &lt;dependency&gt; &lt;groupId&gt;org.hibernate&lt;/groupId&gt; &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt; &lt;version&gt;4.2.21.Final&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; </code></pre> <h2>3. Define your database connection</h2> <p>Edit <code>&lt;tomee-home&gt;/conf/tomee.xml</code>:</p> <pre class="lang-xml prettyprint-override"><code>&lt;Resource id="myJtaDatabase" type="DataSource"&gt; JdbcDriver com.mysql.jdbc.Driver JdbcUrl jdbc:mysql://localhost:3306/my_dbname?autoReconnect=true UserName foo Password bar validationQuery = SELECT 1 JtaManaged true &lt;/Resource&gt; </code></pre> <p>You can also put the above <code>&lt;Resource&gt;...&lt;/Resource&gt;</code> definition into <code>WEB-INF/resources.xml</code> and ship it with your application instead:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;resources&gt; &lt;!-- Put &lt;Resource&gt; elements here --&gt; &lt;resources&gt; </code></pre> <h2>4. JTA Datasource</h2> <p>Now that you told TomEE how to establish a connection, define a JTA datasource in <code>/src/main/java/META-INF/persistence.xml</code>:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;persistence version="2.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_2_0.xsd"&gt; &lt;persistence-unit name="my_persistence_unit"&gt; &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; &lt;jta-data-source&gt;java:openejb/Resource/myJtaDatabase&lt;/jta-data-source&gt; &lt;properties&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /&gt; &lt;!-- As many hibernate properties as you need, some examples: --&gt; &lt;property name="hibernate.show_sql" value="false" /&gt; &lt;property name="hibernate.format_sql" value="true" /&gt; &lt;!-- Drop and then re-create the database schema (don't do this in production) --&gt; &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <h2>5. Start using JPA</h2> <p>Obtain an <code>EntityManager</code> in a CDI bean or EJB like this:</p> <pre><code>@PersistenceContext(unitName = "my_persistence_unit") private EntityManager em; </code></pre> <h2>Final Notes</h2> <h3>Hibernate versions 4.3+</h3> <p>I am using Hibernate 4.2.21 (JPA 2.0, Java EE 6) along with TomEE 1.7.2. Any TomEE 1.7.x, 1.6.x and 1.5.x will work. However, you cannot use Hibernate 4.3+ (JPA 2.1 / Java EE 7), as TomEE 1.7.x and below only support Java EE 6. If you really want to use Java EE 7 features along with TomEE, this blog post might be helpful: <a href="http://rmannibucau.wordpress.com/2013/07/19/little-tip-to-help-you-to-test-javaee-7-in-tomee-with-tomee-maven-plugin/" rel="noreferrer">http://rmannibucau.wordpress.com/2013/07/19/little-tip-to-help-you-to-test-javaee-7-in-tomee-with-tomee-maven-plugin/</a></p> <h3>TomEE 1.5.x</h3> <p>TomEE 1.5.x already includes a <code>javassist-&lt;version&gt;.jar</code>, so you don't have to copy one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload