开发者

Using Hibernate outside the context of Tomcat

开发者 https://www.devze.com 2022-12-24 08:41 出处:网络
Anyone used开发者_如何学JAVA 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

Anyone used开发者_如何学JAVA 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.

@Entity
@Table(name="PERSON")
public class SimpleHibernate {
    @Id
    private Long id;
    private String name;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        AnnotationConfiguration cfg = new AnnotationConfiguration();
        cfg.configure("/hibernate.cfg.xml");
        cfg.addAnnotatedClass(SimpleHibernate.class);
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        Session session = sessionFactory.openSession();
        System.out.println(session.createQuery(
             "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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号