EntityManager.find() returns null

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:

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

posted Jun 17, 2008 02:55:00

0

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

posted Jun 17, 2008 03:20:00

0

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

posted Jun 17, 2008 03:38:00

0

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

posted Jun 17, 2008 04:25:00

0

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

posted Jun 24, 2008 00:40:00

0

HI,

I have managed to solve this problem. After adding @Temporal(TemporalType.DATE) annotation it works fine.