Hi, I am new to this version of Hibernate, though I used it a long time ago. I downloaded the tutoral, which contains the deprecated BuildSession method. I updated to the currently recommended way of creating a session public static SessionFactory getSessionFactory() { if (sessionFactory == null) { // loads configuration and mappings Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); // builds a session factory from the service registry sessionFactory = configuration.buildSessionFactory(serviceRegistry); } return sessionFactory; }
and tried the test code in AnnotationsIllustrationTest.java.
As long as this property is set in the hibernate.cfg.xml file <property name="hbm2ddl.auto">create</property> the test code works. But if I remove it, or call the same code sequence from a regular main method, the code hangs AFTER the final close This is the code public static void main(String args[]) { Session session = HibernateUtil.getSession(); session.beginTransaction(); session.save(new Event("Our very first event!", new Date())); session.save(new Event("A follow up event", new Date())); session.getTransaction().commit(); session.close();
// now lets pull events from the database and list them session = HibernateUtil.getSession(); session.beginTransaction(); List result = session.createQuery("from Event").list(); for (Event event : (List<Event>) result) { System.out.println("Event (" + event.getDate() + ") : " + event.getTitle()); } session.getTransaction().commit(); session.close(); }
Everything runs to the end, but a thread keeps running so nothing every completes. If I forcibly close, using System.exit(0), then the program stops, but I hate to do that.
Something seems terribly wrong if even the basic tutorial code doesn't run correctly. And the tutorial is out of date with deprecated methods. I notice a lot of people asking about the deprecated methods in the tutorial on this forum, with no response. Have people just given up on Hibernate?
|