Problem javax.persistence.NoResultException: No entity found for query
Amirr Rafique
Ranch Hand
Joined: Nov 14, 2005
Posts: 324
posted
0
Hi mates
I am getting following exception. Please help me out in understanding the reason
Below is my bean code.
"Know where to find the solution and how to use it - that's the secret of success."
Niranjan Deshpande
Ranch Hand
Joined: Oct 16, 2005
Posts: 1277
posted
0
Are you missing the SELECT clause in the query?
SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ] Performance is a compulsion, not a option, if my existence is to be justified.
Promod kumar
Ranch Hand
Joined: Jun 26, 2006
Posts: 90
posted
0
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
Joined: Oct 16, 2005
Posts: 1277
posted
0
how does this work even if there is no SELECT clause?
Promod kumar
Ranch Hand
Joined: Jun 26, 2006
Posts: 90
posted
0
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
Joined: Jul 05, 2005
Posts: 1936
posted
0
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 .
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Amirr Rafique
Ranch Hand
Joined: Nov 14, 2005
Posts: 324
posted
0
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
Joined: Nov 14, 2005
Posts: 324
posted
0
Following is my persistence.xml
Promod kumar
Ranch Hand
Joined: Jun 26, 2006
Posts: 90
posted
0
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
Joined: Jul 05, 2005
Posts: 1936
posted
0
When persisted, have the transaction been committed?
Can you post your codes?
Promod kumar
Ranch Hand
Joined: Jun 26, 2006
Posts: 90
posted
0
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();
"Promod Mahajan " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.
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
Joined: Jun 26, 2006
Posts: 90
posted
0
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
Joined: Jul 05, 2005
Posts: 1936
posted
0
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
Joined: Jun 26, 2006
Posts: 90
posted
0
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
Joined: Nov 14, 2005
Posts: 324
posted
0
Following code has solved the problem
Thanks for your help
Hong Anderson
Ranch Hand
Joined: Jul 05, 2005
Posts: 1936
posted
0
Promod, I got confused, I actually wanted to ask the topic creator .
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Problem javax.persistence.NoResultException: No entity found for query