Issue
Im moving to java from C#, this is my frist try at actually writting something and I'm trying to setup my data layer, but I think I'm missing something like DataContext in LINQ to SQL in .NET
Im trying to query database using Hibernate but it seems like I'm missing something, I downloaded Hibernate 4 and QueryDSL for type safe queries.
Next I connected to my postgreSQL database and generated DAO objects using Dali(I think? in menu I selected JPA enteties from tables), written queries that I need but I am unsure which object I have to execute my query on (something like connection manager or something?)
Im missing something here I think:
EmployeesRepository repo = new EmployeesRepository();
List<Employees> employees = repo.GetByName("Steve");
Employees s = employees.get(0);
String g = s.getName();
My entity repository:
public class EmployeesRepository {
public List<Employees> GetByName(String ename)
{
HibernateQuery qry = createQuery(employees);
qry.where(employees.name.like(ename));
return qry.list(employees);
}
private HibernateQuery createQuery(QEmployees employee)
{
return new HibernateQuery().from(employee);
}
}
Im getting:
java.lang.UnsupportedOperationException: No session in detached Query available
Solution
You need to create/open a hibernate session before executing query.
Example:
Session session = HibernateUtil.getSessionFactory().openSession();
When you are done with query execution, you need to close.
session.close();
Here is step by step example.
Note: There may be a hibernate version mismatch between what you are using and the one described in above example, you may need to just tweek it.
Answered By - kosa
Answer Checked By - Marilyn (JavaFixing Volunteer)