aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes How to insert todays date into Database using Entity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "How to insert todays date into Database using Entity" Watch "How to insert todays date into Database using Entity" New topic
Author

How to insert todays date into Database using Entity

Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

Hi All,

I am trying to understand @Temporal annotation,for this i just written an entity class like below:



How do i implement the setter method to insert today's date everytime entity is persisted.

Regards
Sudhakar


Regards
Sudhakar
Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

Problem solved.

Regards
Sudhakae
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
Hope you used @PrePersist. I think that is best way.

It would be a help to others who are looking the answers for the same problem if you post your method you came across than just saying "Problem solved".
And other hand there maybe better method than you used. So others may help you in that case.

Do something for community as well.

Thanks
Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

It would be a help to others who are looking the answers for the same problem if you post your method you came across than just saying "Problem solved".


I thought the problem i faced is very silly and i was able to solve it after some googling.

I did not modify the entity,I just changed the session bean code:



With the above code i was able to test all the elements of Temporal annotation(DATE,TIME,TIMESTAMP)..

I dont know if this is useful to anybody

Regards
Sudhakar
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
Hi Karnati,

Your method will work if the client (of ejb or ejb itself) set creation date purposely. But it does not guaranteed.

So the better way is make the setCretaedDate private that ensure no one can set it, and set it in PrePersist. It is more accurate.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to insert todays date into Database using Entity
 
Similar Threads
How to write Field baed access entity
how to insert a current date into the databse colunm using hybernate ?
EJB - Everything works except persist()
HQL 2 – how to say “within X days from today”?
How to get difference between dates