GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes EntityManager.find() returns null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EntityManager.find() returns null" Watch "EntityManager.find() returns null" New topic
Author

EntityManager.find() returns null

dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
Hello All,

I have been facing an issue with the find() method of the EntityManager(EJB 3.0). I have an entity bean which has composite primary key. When i try to find an object with the primary key the find() method of the EntityManager returns null. When i check the database the entity that i am searching for exists. The user with which i am creating the entity in the DB is the same as the user with which i am searching for it. Can anybody let me know whether i am missing anything? Is there something incorrect that i am doing?
The code works fine when i have entity beans without composite primary keys.
Its a Java SE application.
The entity bean and its corresponding primary key class are as shown below:

The code for testing the bean is as shown below
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
I don't know if it's the reason of your problem, but you must have all your annotations either in the attributes or in the getter methods. You cannot mix.
Either you put the @EmbededId annotation before the pk attribute or put the @Column annotation before the getDatbis() method.
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
I tried to put all the annotations in the attributes and then i tried putting them before the getter methods but both these solutions dont seem to work. I still get null from the find() method.
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
When you set the date in the pk using pk.setDatab(new Date()), you're setting the current date. Is it the exactly date that's stored in the database? (Considering hours, minutes and seconds)

You can configure the log to show the SQL query that's being executed, and then check which values it's using in the parameters. If it's hibernate, add the following to the persistence.xml

<property name="hibernate.show-sql" value="true"/>
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
I have even tried using new Date(long date) constructor. I still face the same issue.
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
The date from the database is exactly the same as the date i am searching with.
dnyan ginde
Ranch Hand

Joined: Jan 17, 2006
Posts: 68
HI,

I have managed to solve this problem. After adding @Temporal(TemporalType.DATE) annotation it works fine.
 
Don't get me started about those stupid light bulbs.
 
subject: EntityManager.find() returns null