How to insert date type when integratting between Hibernate and Spring

I 'm using Hibernate framework as way of mapping from Javabean to Database for my project which applied by Spring framework.But, I don' know how to insert Date type in Hibernate

My code :

import java.util.Date;
......................................

 @Column(name = "date")
  private Date date;

 @SuppressWarnings("deprecation")
 public void setDate(String date) {
  Date date1 = new Date(date);
  this.date = date1;
}

After submitting.date fileds in databse is null. It's value can not be mapped into Account table in database

Note: Account means :

@Entity
@Table(name="Account")
public class User {

Please help me.Thanks

Upvotes: 0

Views: 4627

Answers (2)

Ken
Ken

Reputation: 11

I also has this problem.When I insert date type object into DB,it turns out null value.

My date type in DB is Datetime type.In my case,it is format problem.

Then I try to do so,then solved it,after following formatting.

Date date=new Date();
SimpleDateFormat spl=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d=spl.format(date);
date=spl.parse(d);

You can try it.

Upvotes: 1

Prasad Khode
Prasad Khode

Reputation: 6739

in your User class use the following to map the column as date type

@Column(name = "date")
@Temporal(TemporalType.DATE)
private Date date;

Upvotes: 0

Related Questions