-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to close Hibernate on exiting application?
PostPosted: Tue Feb 14, 2017 10:25 am 
Newbie

Joined: Tue Feb 14, 2017 10:19 am
Posts: 2
I am beginning with hibernate and JPA and I have been following several tutorials on JPA with eclipse link on Netbeans.

However, while still working on Netbeans I downloaded the hibernate-release-5.2.7.Final.zip manually from Hibernate web page and as stated here: http://docs.jboss.org/hibernate/orm/5.2 ... _artifacts.

I added the lib/required directory and the lib/jpa-metamodel-generator/ to my java application and mysql-connector jar. After that, I created my persistence.xml as in gist.

Then I created a sample Entity Prueba and I added a JPA ControllerClass from Entity through NetBeans wizard next, to that I created a quick Dao for testing Hibernate.

After creating my main class everything works fine the object is added to MySQL but the app doesn't close on finish I show the log and everything on gist https://gist.github.com/Paulwerito/111b ... c0452b8b2e


Top
 Profile  
 
 Post subject: Re: Java Hibernate JPA app adds objects to the database but the
PostPosted: Tue Feb 14, 2017 11:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That's because you didn't close the EntityManagerFactory.

Add the following line

Code:
pbdao.destroy();


at the end of your test code:

Code:
public static void main(String[] args) {
        Prueba pb = new Prueba();
        PruebaDao pbdao = new PruebaDao();
       
        pb.setNombre("elexit");
       
        pbdao.addPrueba(pb);

        pbdao.destroy();               
}


where the destroy method in PruebaDao looks like this:

Code:
public void destroy() {
        this.em.close();
}


Top
 Profile  
 
 Post subject: Re: Java Hibernate JPA app adds objects to the database but the
PostPosted: Sat Nov 25, 2017 3:25 pm 
Newbie

Joined: Sat Nov 25, 2017 3:16 pm
Posts: 1
vlad wrote:
That's because you didn't close the EntityManagerFactory.

Add the following line

Code:
pbdao.destroy();


at the end of your test code:

Code:
public static void main(String[] args) {
        Prueba pb = new PruebaDao();
        PruebaDao pbdao = new PruebaDao();
       
        pb.setNombre("elexit");
       
        pbdao.addPrueba(pb);

        pbdao.destroy();               
}


where the destroy method in PruebaDao looks like this:

Code:
public void destroy() {
        this.em.close();
}


I was encountering a similar error but got it solved with your reference. Thanks a lot for the solution @vlad :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.