• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem javax.persistence.NoResultException: No entity found for query

 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mates

I am getting following exception. Please help me out in understanding the reason



Below is my bean code.

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you missing the SELECT clause in the query?
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see this in the stack, Caused by: javax.persistence.NoResultException: No entity found for query. If there is no result to your query, then a NoResultExceptions is thrown. Try replacing this (since cabin id=1 was persisted using manager)


with

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how does this work even if there is no SELECT clause?
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niranjan,
I just tried it and it works, I guess it defaults to "select entity" if select is missing. I was not aware of it before reading this thread. I am pasting the HQL and the generated SQL from my jboss log.

2009-04-15 11:51:38,025 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] HQL: FROM com.ejb3inaction.actionbazaar.persistence.Category c WHERE c.categoryName =?1
2009-04-15 11:51:38,025 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] SQL: select category0_.CATEGORY_ID as CATEGORY1_2_, category0_.CREATED_BY as CREATED4_2_, category0_.CATEGORY_NAME as CATEGORY2_2_, category0_.PARENT_ID as PARENT5_2_, category0_.CREATE_DATE as CREATE3_2_ from CATEGORIES category0_ where category0_.CATEGORY_NAME=?
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next time, you can take a look at SQL log. This is one drawback of ORM, you don't write SQL, it generates for you, sometimes you don't know what is going on. Actually it's both drawback and advantage .
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys I am not able to understand it. I have two entity managers(manager and createdMngr) both are connected to same persistence unit. I am using manager to persistence cabin with id 1 and using createdMngr I am trying to query using other persistence manager. Why I am not able to find it
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is my persistence.xml

 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amirr,
Each EntityManager has an associated PersistenceContext. In this case there are 2 EntityManagers and they have two separate PersistenceContexts which are unaware of each other even though their PersistenceUnits are the same. So to get it to work if you do something like this then it will work since the cabin will be saved to the DB and the query retrieves it from DB when you use the query with createdMngr.



I have relied mostly on testing to come to the above conclusion, I would like someone else to validate if this explanation is accurate.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When persisted, have the transaction been committed?
Can you post your codes?
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj,

Here is my code. I verified the DB and the sysout statements.

public void addCustomer(String firstName, String lastName) {
System.out.println("In addCustomer");
Customer cust = new Customer();
cust.setFirstName(firstName);
cust.setLastName(lastName);
EntityManager createdMngr = emf.createEntityManager();
em.persist(cust);
em.flush();
Query query = createdMngr
.createQuery("select c from Customer c where c.firstName = ?1");
query.setParameter(1, "Vijay");
Customer customer = (Customer) query.getSingleResult();
System.out.println("The customerlast name is: "
+ customer.getLastName());
createdMngr.close();

}
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Promod Mahajan " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Promod,
You should query from another method, not within the same method with persisting.
And you haven't closed entity manager, using Application Managed Entity Manager, you have to close it yourself.
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kengkaj,
You can run the query from within the same method/transaction. In this case before executing the query, the EntityManager first flushes all the pending updates and then runs the query.
Yes, I did forget to close the em, Thanks for pointing it out.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know, your code seems correct excepts you need to put createdMngr.close(); in finally block.
Try to check generated SQL and copy/paste SQL to run against DB.
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj,
I think there is some confusion here. My code works and is giving the expected result. I was trying to answer Amirr.
When you asked to post the code, I thought you were asking me, may be you were asking the original poster Amirr.
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following code has solved the problem


Thanks for your help
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Promod, I got confused, I actually wanted to ask the topic creator .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic