-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Hibernate Sytem date insert in SQL Server
PostPosted: Thu May 04, 2006 11:42 pm 
Newbie

Joined: Thu May 04, 2006 11:34 pm
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:SQL SERver

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

    I want to know how to enter system date in a datetime field in sqlserver database.When i insert the object of the table the object is having null value for the required date field as it has to be system date.I am new to hibernate so want to know how i can insert a system date into the table


It worked for inserting date for one time.
To modify date everytime any action is done is used
<timestamp column="LastModDt" name="lastModDate" type="timestamp" source="vm" />
and defined my property as a Date with getter and setters.

Iam getting the following errror
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1455)
Caused by:
org.xml.sax.SAXParseException: Attribute "type" must be declared for element type "timestamp".

Could you please help me


Last edited by unnisa on Fri May 05, 2006 9:58 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 12:14 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
My approach for this sort of field depends on whether or not the field has to be updated on every write.

If the field is set once and only once, on initial row creation, then map the field with insert="false" update="false", and set the DB default value for the column to be the system date (that's getdate() for SQLServer, though that varies by DBMS).

If the field has to be updated every time the row is modified, then use the hibernate <timestemp> element. See refdocs, section 5.1.8.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 10:17 am 
Newbie

Joined: Thu May 04, 2006 11:34 pm
Posts: 6
tenwit wrote:
My approach for this sort of field depends on whether or not the field has to be updated on every write.

If the field is set once and only once, on initial row creation, then map the field with insert="false" update="false", and set the DB default value for the column to be the system date (that's getdate() for SQLServer, though that varies by DBMS).

If the field has to be updated every time the row is modified, then use the hibernate <timestemp> element. See refdocs, section 5.1.8.


Thanks for your help I have one more question

I got it to working for one time insert by using the following code
<property name="createDate" column="createDate" type="java.util.Date" update="false" insert="false"/>

to insert every time a record is modified didnt work
<timestamp column="LastModDt" name="lastModDate" type="timestamp" />
and defined lastmoddate property as date property in the object
I am getting the following erro:
org.xml.sax.SAXParseException: Attribute "type" must be declared for element type "timestamp".

Can you please help me


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 1:06 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
unnisa wrote:
I got it to working for one time insert by using the following code
<property name="createDate" column="createDate" type="java.util.Date" update="false" insert="false"/>

to insert every time a record is modified didnt work


To store a date information everytime a record is modified, I suggest have another column called updateDate created in your schema. Values will be updated in such column with the following in your mapping file.
Code:
<property name="updateDate" column="updateDate" type="java.util.Date" />


Remember having update="false" on a property will not update that column everytime.

unnisa wrote:
<timestamp column="LastModDt" name="lastModDate" type="timestamp" />
and defined lastmoddate property as date property in the object
I am getting the following erro:
org.xml.sax.SAXParseException: Attribute "type" must be declared for element type "timestamp".

Can you please help me


<timestamp> element doesnt have "type" attribute defined in hibernate-mapping-3.0.dtd file. So remove the "type" attribute from the element. This will remove the SAXParseException.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 3:51 pm 
Newbie

Joined: Thu May 04, 2006 11:34 pm
Posts: 6
bkmr_77 wrote:
unnisa wrote:
I got it to working for one time insert by using the following code
<property name="createDate" column="createDate" type="java.util.Date" update="false" insert="false"/>

to insert every time a record is modified didnt work


To store a date information everytime a record is modified, I suggest have another column called updateDate created in your schema. Values will be updated in such column with the following in your mapping file.
Code:
<property name="updateDate" column="updateDate" type="java.util.Date" />


Remember having update="false" on a property will not update that column everytime.

unnisa wrote:
<timestamp column="LastModDt" name="lastModDate" type="timestamp" />
and defined lastmoddate property as date property in the object
I am getting the following erro:
org.xml.sax.SAXParseException: Attribute "type" must be declared for element type "timestamp".

Can you please help me


<timestamp> element doesnt have "type" attribute defined in hibernate-mapping-3.0.dtd file. So remove the "type" attribute from the element. This will remove the SAXParseException.


If I define like this my database column is becoming null when I do an update or insert.
I need to enter the system date when record is modified or inserted.How can i do that???
<property name="updateDate" column="updateDate" type="java.util.Date" />


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 8:11 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Code:
<property name="updateDate" column="updateDate" type="timestamp" />


This kind of declaration is working fine with me. Declaring type="java.util.Date" should not make such difference, as working in my case and not working in your case. Dont know why? Its beyond my knowledge. Posting your code how you are modifying and persisting to DB will help in coming to an explanation.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 07, 2006 7:02 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Using a <property type="timestamp"> means that the time that goes into the database is the java VM time: you have to put the current time into the object before saving/updating. This is usually not appropriate, though if it's good enough for you, then great.

If you use <timestamp use=db="true"> (or equivalently, <version type="dbtimestamp">), then the time is generated by the DB server, and that's usually better.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.