Skip to main content

How to auto insert Current DATE in SQL with Java / Hibernate



I need to add automatically the current date into my Database when I create a new OperantionBank. I'm using Hibernate. Thanks







import java.io.Serializable;

import java.sql.Date;

import javax.persistence.*;

import org.hibernate.annotations.Generated;

import org.hibernate.annotations.GenerationTime;



@Entity

public class OperationBank implements Serializable {

private static final long serialVersionUID = 1L;



@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private Long id;



private String wordring;



private Double amount;



@Generated(GenerationTime.ALWAYS)

@Temporal(javax.persistence.TemporalType.DATE)

private Date dateoperation = new java.sql.Date(new java.util.Date().getTime());



@OneToOne

private Account account;




Comments

  1. This answer might help you:

    http://stackoverflow.com/a/221827/355499

    In essence you use @PrePersist and/or @PreUpdate annotation to generate the dates when needed.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?