Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EntityManager.find() returns null

 
dnyan ginde
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have even tried using new Date(long date) constructor. I still face the same issue.
 
dnyan ginde
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The date from the database is exactly the same as the date i am searching with.
 
dnyan ginde
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

I have managed to solve this problem. After adding @Temporal(TemporalType.DATE) annotation it works fine.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic