You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Arnav-A <ar...@gmail.com> on 2010/06/28 22:52:25 UTC

Passing Current Timestamp to Entity Object's Timestamp field variable

Hi All
    I have to insert one Invoice Object to the DB2 Table. I have
insertTimestamp field in the Invoice Entity Class. I want a value that needs
to be set to this variable so that it insert the current timestamp value of
the DB2 Database system i.e. generated insert query should have CURRENT
TIMESTAMP as value for the column corresponding to the insertTimestamp field
variable.
   
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Passing-Current-Timestamp-to-Entity-Object-s-Timestamp-field-variable-tp5232583p5232583.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.

Re: Passing Current Timestamp to Entity Object's Timestamp field variable

Posted by Fay Wang <fy...@yahoo.com>.
Hi,
   
      A workaround is to do the insert first with automatically generated timestamp and then update it with the application-provided timestamp. In order to do so, you need to remove updatable=false from the Column annotation.

@Entity
@Table(name = "INVOICE", schema = "SALES")
public class 
Invoice implements Serializable {

  @Id
   private int id;

  private String description;

   
@Column(columnDefinition="Timestamp NOT NULL WITH DEFAULT", insertable= 
false)
    private java.sql.Timestamp ts;






----- Original Message ----
From: Arnav-A <ar...@gmail.com>
To: dev@openjpa.apache.org
Sent: Wed, June 30, 2010 11:46:17 AM
Subject: Re: Passing Current Timestamp to Entity Object's Timestamp field variable


Still Waiting for the answer.

Please reply
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Passing-Current-Timestamp-to-Entity-Object-s-Timestamp-field-variable-tp5232583p5238118.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.



      


Re: Passing Current Timestamp to Entity Object's Timestamp field variable

Posted by Arnav-A <ar...@gmail.com>.
Still Waiting for the answer.

Please reply
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Passing-Current-Timestamp-to-Entity-Object-s-Timestamp-field-variable-tp5232583p5238118.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.

Re: Passing Current Timestamp to Entity Object's Timestamp field variable

Posted by Arnav-A <ar...@gmail.com>.
Thanks Fay for the response.

I tried it the way you have explained and it is working fine.

Basically, by making it insertable=false, it is not putting the the column
corresponding to ts variable in the insert statement and at the time of
insert query run, it is pulling current timestamp from the database.

My requirement to check first if ts is having some value. If ts is not null
and have valid timestamp, insert that into the table or consider the current
timestamp to insert into the table.

With this approach, it is ignoring any value that I pass to the ts and
inserting only the current timestamp to the table.

Please help me achieving above scenario.  
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Passing-Current-Timestamp-to-Entity-Object-s-Timestamp-field-variable-tp5232583p5233217.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.

Re: Passing Current Timestamp to Entity Object's Timestamp field variable

Posted by Fay Wang <fy...@yahoo.com>.
Hi,
   You can create a table with a column type Timestamp NOT NULL WITH DEFAUTLT. DB2 will automatically set the value of CURRENT TIMESTAMP to it when a sql of 
INSERT INTO SALES.INVOICE (id, description) VALUES (?, ?) [params=(int) 1, (null) null]
is executed. The following is an example:

@Entity
@Table(name = "INVOICE", schema = "SALES")
public class Invoice implements Serializable {

  @Id
   private int id;

   private String description;

   @Column(columnDefinition="Timestamp NOT NULL WITH DEFAULT", insertable= false, updatable=false)
    private java.sql.Timestamp ts;







----- Original Message ----
From: Arnav-A <ar...@gmail.com>
To: dev@openjpa.apache.org
Sent: Mon, June 28, 2010 1:52:25 PM
Subject: Passing Current Timestamp to Entity Object's Timestamp field variable


Hi All
    I have to insert one Invoice Object to the DB2 Table. I have
insertTimestamp field in the Invoice Entity Class. I want a value that needs
to be set to this variable so that it insert the current timestamp value of
the DB2 Database system i.e. generated insert query should have CURRENT
TIMESTAMP as value for the column corresponding to the insertTimestamp field
variable.
  
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Passing-Current-Timestamp-to-Entity-Object-s-Timestamp-field-variable-tp5232583p5232583.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.