• 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

Inserting current date into the database using hibernate

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Foreman
Posts: 275
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change type in .hbm.xml from Date to Timestamp .This will work.
reply
    Bookmark Topic Watch Topic
  • New Topic