javahibernate

java.sql.Date not inserted into datatables


I have a class which contains:

@Column(name = "end_date")
private Date endDate;

and its setter

public void setEndDate() {
    endDate = new java.sql.Date(Calendar.getInstance().getTime().getTime());
    System.out.println(endDate);
}

When I'm calling the setter, and check again if the value is correct, everything's fine.(even getEndDate().getClass() ). However, when I'm saving my object into PostgreSQL (myObject.persist()), there's no value on END_TIME column. Other values are correct.

Does anyone know what's the problem?

Also, I have to mention that hibernate is set to create SQL tables

Setter and getter:

public void setEndDate(Date endDate) {
    setEndDate();
}
public Date getEndDate() {
    return endDate;
}

Solution

    1. import java.util.Date instead of import java.sql.date
    2. assign current date as per following code
    3. default date assign while creating object of this entity if user need to change then using setter on run time
    4. try this it will work

      @Column(name = "END_TIME")
      private Date endDate = new Date();
      
      public void setEndDate(Date endDate) {
        this.endDate = endDate;
      }
      
      public Date getEndDate() {
          return endDate;
      }