• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EntityManager.find() returns null

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

I have managed to solve this problem. After adding @Temporal(TemporalType.DATE) annotation it works fine.
reply
    Bookmark Topic Watch Topic
  • New Topic