Using Hibernate outside the context of Tomcat

Anyone used Hibernate to access a DB in a pure java app (not a web application)? What was your experience like, did it take long to set up and get going?


I've found it be very useful even when using pure SQL (no mappings) because it handles query parameters and type conversions better than straight JDBC.

Here is a simple self-contained Hibernate class that assumes a table called Person with an id and anme. Just define hibernate.cfg.xml to set the connection url, driver, etc; set the Hibernate jars on the classpath; and run it.

The trick is definitely getting all the right jars in your classpath. If you download the Hibernate distribution, as of 3.3 there was a readme file in the lib folder that explained each JAR file and whether you needed it at runtime.

public class SimpleHibernate {
    private Long id;
    private String name;
    public Long getId() {
        return id;
    public void setId(Long id) { = id;
    public String getName() {
        return name;
    public void setName(String name) { = name;

    public static void main(String[] args) {
        AnnotationConfiguration cfg = new AnnotationConfiguration();
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        Session session = sessionFactory.openSession();
             "select count(*) from SimpleHibernate").uniqueResult());

Hibernate can be used with equivalent setup time in any java application. Important thing is to get the jars in the classpath instead of web app class loader. If jars are available properly rest of API does not depend on the type of application.

Done that couple of times

If its a stand alone server side app- I would suggest using Spring_hibernate.

To answer your specific question- it was pretty simple and straight forward. Didnt take any longer than it would take a web app

The configuration part is not that different. Just use Hibernate built-in connection pool and there you go. However, the session management may be very different from the open session in view pattern. But it's hard to be more precise without more details on your application.

I have used hibernate in a few Swing apps.

It's very similar to it's use in a webapp except that you don't have the possibility to use a filter to close the Session and that you need to be careful about what you do with persistent object between two transactions.

I have found that it's easier to keep a reference on a persistent object, and try to use it after the transaction that produced it. In a webapp, a transaction often ends at the end of a request.

Need Your Help

How to display the images from Horizontal scroll view In android as slide view using picasso

android json

I have an horizontal scroll views for different categories of images. On click of image thumbnail it will display that image. Now on click of thumbnail need to display the image along with slide vi...

How to select dgvRow by rightClick?

c# winforms datagridview

I have a contextMenu on a DataGridView.