Reputation: 3
Please I have this error
"org.hibernate.NonUniqueResultException: query did not return a unique result: 2"
It works perfectly when I have one result and I need to show the two(or more) results found but I don't know how!
Here is my code:
public class ContactImplDataBase implements ContactDAO {
//.......
...
public Contact getContactByType(String type) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Contact.class)
.add(Restrictions.like("type", type));
tx.commit();
return (Contact)criteria.uniqueResult();}
}
And:
public class ContactImpl implements ContactDAO {
//...
..
@Override
public Contact getContactByType(String type) {
Contact contact=null;
for(Contact c:contacts){
if(c.getType().equals(type)){
contact=c;
break;
}
}
return contact;
}
...}
And in the controller:
@RequestMapping(value="/rechercheContact")
public String rechercheContact(Model model, @RequestParam(value="type") String type){
List<Contact> liste=new ArrayList<Contact>();
liste.add(services.getContactByType(type));
model.addAttribute("listeContact", liste);
model.addAttribute("type", type);
return "ex";
}
Any help ?!!
Upvotes: 0
Views: 2449
Reputation: 2471
Let's do a little code review here:
Format your code for readability.
getContactByType
is wrong because there could be more than one contact for one type, so rename your method to getContactsByType
and let it return a List<Contact>
instead of a single Contact
.
This has to be changed also in the non provided interface ContactDAO
.
The class ContactImpl
does not make any sense to me.
Don't use transactions for database reads.
Finally you should get it to work:
public class ContactImplDataBase implements ContactDAO {
@Override
public List<Contact> getContactsByType(String type) {
Session session = HibernateUtil.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(Contact.class)
.add(Restrictions.like("type", type));
return (List<Contact>)criteria.list();
}
}
Your controller:
@RequestMapping(value="/searchContacts")
public String searchContacts(Model model, @RequestParam(value="type") String type) {
List<Contact> list = services.getContactsByType(type);
model.addAttribute("contactList", list);
model.addAttribute("type", type);
return "ex";
}
Upvotes: 0
Reputation: 1
Error message appears to be a hibernate query data return error, presumably the trigger:
return (Contact)criteria.uniqueResult();}
Show that your contact table uses the "type" field query to return multiple records, so there are two ways to solve the solution:
criteria.uniqueResult() => if (Criteria.list() > 0) return criteria.list().get(0)
Modify the number of your database records, maintain
select count(*) contact where type='xxx'
returned 1 records, more than other data deleted. Can also be used to index the database.
Upvotes: 0
Reputation: 8414
Your function here:
public Contact getContactByType(String type) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Contact.class)
.add(Restrictions.like("type", type));
tx.commit();
return (Contact)criteria.uniqueResult();} // TAKE NOTE OF THIS
}
You have set it to only return single unique result.
So that means if there is no single unique result it will thrown an exception.
You have two options here:
Upvotes: 1