'How get all the rows value in hibernate?
I am writing the following code to get the values from the Data base using Hibernate
try {
System.out.println("In getDetails() try block");
configuration = new Configuration();
configuration.configure("resources\\hibernate.cfg.xml");
sessionFactory = cfg.buildSessionFactory();
session = sessionFactory.openSession();
Query query = session.createQuery("from ServiceManagerDetails");
List l = query.list();
Iterator i1 = l.iterator();
ServiceManagerDetails smd = null;
while (i1.hasNext()) {
smd = (ServiceManagerDetails)i1.next();
System.out.println(smd.getServiceMode());
}
} catch (Exception e) {
System.out.println("Error:- "+e);
//tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
There are two records but I am getting the value of one row twice. But I have get the value of both the rows. Please help me find out what I am missing. Thanks
Solution 1:[1]
I am suspecting that you have a one-to-many association mapped that you have specified a fetch="select" method. Hibernate with this will retrieve the parent object for each of the associated records.
Query query = session.createQuery("from ServiceManagerDetails")
.SetResultTransformer(CriteriaSpecification.DistinctRootEntity);
Should do the trick.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brett Ryan |
