Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know about Spring, so you will have to update my answer a little if you really want to use it (for instance Spring has its own mechanism for initializing the entity manager).</p> <p><strong>Dependencies</strong></p> <p>This is the configuration I used for a recent desktop project of mine (some versions may have evolved since), <strong>that uses Hibernate over JPA</strong> (i.e. it uses an EntityManager) :</p> <pre><code>org.hibernate:hibernate:3.2.7.ga org.hibernate:hibernate-annotations:3.4.0.GA org.hibernate:hibernate-entitymanager:3.4.0.GA </code></pre> <p>You may also need :</p> <pre><code>commons-collections:commons-collections:3.2.1 asm:asm:3.2 cglib:cglib:2.2 dom4j:dom4j:1.6.1 antlr:antlr:2.7.7 c3p0:c3p0:0.9.1.2 </code></pre> <p>You can find all them on <a href="http://search.maven.org/#browse" rel="noreferrer">maven central</a>.</p> <p><strong>Configuration</strong></p> <p>You need a valid persistence.xml in META-INF folder :</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"&gt; &lt;persistence-unit name="NONJTAPersistenceUnit" transaction-type="RESOURCE_LOCAL"&gt; &lt;class&gt;com.yourpackage.EntityClass1&lt;/class&gt; &lt;class&gt;com.yourpackage.EntityClass2&lt;/class&gt; &lt;class&gt;com.yourpackage.EntityClass3&lt;/class&gt; &lt;properties&gt; &lt;property name="hibernate.show_sql" value="true"/&gt; &lt;property name="hibernate.format_sql" value="true"/&gt; &lt;property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/&gt; &lt;property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://hostXYZ/yourdatabase"/&gt; &lt;property name="hibernate.connection.username" value="sa"/&gt; &lt;property name="hibernate.connection.password" value=""/&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/&gt; &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <p>Update it with your own database info/driver.</p> <p><strong>Usage</strong></p> <p>Create Entity classes the usual way for <em>EntityClass1</em>, <em>EntityClass2</em>, <em>EntityClass3</em> registered in the <em>persitence.xml</em> file above. </p> <p>Then, for the <em>EntityManager</em>... since your are not in a EE environment, you must get an instance of it from the <em>EntityManagerFactoty</em> :</p> <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory("NONJTAPersistenceUnit"); EntityManager em = emf.createEntityManager(); </code></pre> <p>(Again, Spring may provide an other way to get it, check on the documentation).</p> <p>From there you can perform, for instance a persist operation, this way :</p> <pre><code>em.getTransaction().begin(); em.persist(entity); em.getTransaction().commit(); </code></pre> <p>You have a little documentation reading to do to make the whole thing sticks with Spring.</p> <p><strong>EDIT</strong> </p> <p>A sample query :</p> <pre><code>Query query = em.createQuery("select e from EntityClass1 where e.name = :name"); query.setParameter(:name, "foo"); List results = query.getResultList(); </code></pre> <p><strong>EDIT</strong> </p> <p>Updated versions : </p> <pre><code>hibernate-core-3.5.1-Final hibernate-entitymanager-3.5.1-Final hibernate-jpa-2.0-api-1.0.0.Final hibernate-annotations-3.5.1-Final hibernate-commons-annotations-3.2.0.Final dom4j-1.6.1 slf4j-api-1.6.4 slf4j-log4j12-1.6.4 ... </code></pre>
    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. 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