File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Inserting current date into the database using hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Inserting current date into the database using hibernate" Watch "Inserting current date into the database using hibernate" New topic
Author

Inserting current date into the database using hibernate

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 618
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?


kunal
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

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

Joined: Jun 05, 2010
Posts: 618
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

Joined: Jul 29, 2008
Posts: 275

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.


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Rajit vreddi
Greenhorn

Joined: Jul 05, 2012
Posts: 17
Change type in .hbm.xml from Date to Timestamp .This will work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting current date into the database using hibernate
 
Similar Threads
Rows not getting inserted: Hibernate
hbm.xml file does an update instead of an insert- Helppppp
How to persiste a Date type
problem with one-to-many child insertion
ORA-00001: unique constraint exception