Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure hibernate in standalone (Swing) application in eclipse?

i usually use hibernate with spring in web applications so i use DI and maven for configuration, now i want to use hibernate in desktop/swing application that doesn't use maven or spring, and i was wondering about the following:

  1. What jars do i need ?
  2. How to configure the hibernate, and how to make a sample query ?

please advise, thanks.

like image 953
Mahmoud Saleh Avatar asked May 14 '12 11:05

Mahmoud Saleh


1 Answers

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).

Dependencies

This is the configuration I used for a recent desktop project of mine (some versions may have evolved since), that uses Hibernate over JPA (i.e. it uses an EntityManager) :

org.hibernate:hibernate:3.2.7.ga
org.hibernate:hibernate-annotations:3.4.0.GA
org.hibernate:hibernate-entitymanager:3.4.0.GA

You may also need :

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

You can find all them on maven central.

Configuration

You need a valid persistence.xml in META-INF folder :

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="NONJTAPersistenceUnit" transaction-type="RESOURCE_LOCAL">

        <class>com.yourpackage.EntityClass1</class>
        <class>com.yourpackage.EntityClass2</class>
        <class>com.yourpackage.EntityClass3</class>

        <properties>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
            <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://hostXYZ/yourdatabase"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

Update it with your own database info/driver.

Usage

Create Entity classes the usual way for EntityClass1, EntityClass2, EntityClass3 registered in the persitence.xml file above.

Then, for the EntityManager... since your are not in a EE environment, you must get an instance of it from the EntityManagerFactoty :

EntityManagerFactory emf = Persistence.createEntityManagerFactory("NONJTAPersistenceUnit");
EntityManager em = emf.createEntityManager();

(Again, Spring may provide an other way to get it, check on the documentation).

From there you can perform, for instance a persist operation, this way :

em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();

You have a little documentation reading to do to make the whole thing sticks with Spring.

EDIT

A sample query :

Query query = em.createQuery("select e from EntityClass1 where e.name = :name");
query.setParameter(:name, "foo");
List results = query.getResultList();

EDIT

Updated versions :

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
...
like image 187
Yanflea Avatar answered Oct 21 '22 09:10

Yanflea