wood burning stoves*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Problem javax.persistence.NoResultException: No entity found for query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Problem javax.persistence.NoResultException: No entity found for query" Watch "Problem javax.persistence.NoResultException: No entity found for query" New topic
Author

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

Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
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
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
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
how does this work even if there is no SELECT clause?
Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
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
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
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
Following is my persistence.xml

Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
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
When persisted, have the transaction been committed?
Can you post your codes?
Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

"Promod Mahajan " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.


SCJP, SCWCD.
|Asking Good Questions|
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
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
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
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
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
Following code has solved the problem


Thanks for your help
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Promod, I got confused, I actually wanted to ask the topic creator .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem javax.persistence.NoResultException: No entity found for query
 
Similar Threads
GenericJDBCException: Cannot open connection
Problem in persisting Entity with Composite Key
Jboss 5.1 EJBTransactionRolledbackException
Requesting help in connecting a DataSource(DSSDEV)
EJB3 entity bean