Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to insert todays date into Database using Entity

 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved.

Regards
Sudhakae
 
Chaminda Amarasinghe
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic