File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting current date into the database using hibernate

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using swing with Hibernate
Here are my code, and the problem is mentioned at the end of this post
Entity class




abc.hbm.xml



service class



My service class gets the date in required format, but , also converts it into String. Column in database is of 'Date' type. If i insert this String (i will have to change the datatype in my entity class as String), can i make operations related to Date in database and while generating reports? Is it a good approach or i should only insert it as a Date object?
 
Anurag Verma
Ranch Hand
Posts: 167
Hibernate Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good thing will be inserting the date as Date Object, formatting can be done as & when it is getting displayed in UI. This way you have date operations without any complications of parsing it every time, also it will be helpful for managing of Time Zone if required (may be in future).
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Anurag.

The user enters date in the textfield. I get it via getText() and parse it to Date object. But i need to validate the date entered before inserting it into db
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:Column in database is of 'Date' type.

can i make operations related to Date in database and while generating reports?

As far as the database columns are date, all Db operations meant for reporting should work.
Am thinking from a simple SQL point of view.
If your inserts are successful, and date information is available in the DB for your records, SQL that creates reports should work.

Also, if you use hibernate for generating reports, criteria/HQL should be able to read it .. for them its a date column in the DB.
The fetcher would not know how you inserted.

But.. if your POJO( entity class ) has a string for the value.. you'll have to handle the hassles of converting date in DB -> String in hibernate -> date to generate reports
I think hibernate will handle the first half of above.
 
Rajit vreddi
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change type in .hbm.xml from Date to Timestamp .This will work.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic