Hey,
i´m new here and new to Hibernate (since Yesterday)
I want to write a Method “queryRead” which i want use from the whole Project to give me a List of Objects from a class
I have this
public List<?> queryRead(final String query)
{
Query<?> q;
Session session = getSession();
try
{
session.beginTransaction();
q = session.createQuery(query);
} finally
{
session.close();
}
return q.list();
}
For get the Session
public synchronized Session getSession() {
if (sessionFactory == null) {
try {
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (HibernateException e)
{
throw new RuntimeException("(HibernateHelper) Hibernate session factory creation failed", e);
}
}
return sessionFactory.openSession();
}
Other Methods like write, delte, update are correct so i think to get the Session is fine
Now i want to use the Method to get a List from my Homepage.java like
HibernateHelper hh = new HibernateHelper(Book.class);^
List<Book> books = (List<Book>) hh.queryRead("from Book");
for(Book b : books)
{
System.out.println(b);
}
Now the Error is Entity/SessionManager is closed.
I don´t know why because the Session generated in queryRead
If if write this by HomePage.java it works
Session session = hh.getSession();
Query<Book> q = session.createQuery("from Book");
List <Book> books = q.list();
for(Book b : books)
{
System.out.println(b);
}
But i don´t want to generate a Session in Homepage.java i want it generated in HibernateHelper himself.
Sorry for the English
Greetings