1

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;
}
5
  • 1
    Am I getting correctly that the column is called END_TIME on the table and end_date in the @Column tag? This might explain … :-) Commented Mar 23, 2016 at 11:58
  • With a private field, hibernate will use java bean standard setters and getters. Can you post the other setters (that take an argument) and getters you have for that field on that bean? Commented Mar 23, 2016 at 12:02
  • OleV.V , it was me that wrote end_time uppercased. @AndyN, I don't have a setter wich takes a parameter, I guess there is the problem Commented Mar 23, 2016 at 12:06
  • I edited te post, and added setter/getter. It's still not working Commented Mar 23, 2016 at 12:13
  • @Adyzds It is not the case that @Olev.V was commenting on. The annotation is end_date. If the column is end_time, the annotation does not match. Commented Mar 23, 2016 at 12:27

2 Answers 2

1
  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;
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

It's working, thank's man! Still don't understand why my approach wasn't working
0

I use java.util.Date for this case, and change the mapping of your @Column as

@Column(name = "end_time")
private Date endDate = new Date();

public void setEndDate(Date endDate) {
  this.endDate = endDate;
}

public Date getEndDate() {
    return endDate;
}

4 Comments

the same thing. no value in database
I see you mark the column with end_date but checking the value in END_TIME, is that anything wrong?\
I told you above that I uppercased the column name. In database it's "end_time"
I edit answer and change the @column to end_time, logically, this column name is not case-sensitive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.