1

I'm using Spring Boot with Spring Data JPA to map an Entity to a table in a SQL Server database for which I've created an @Embeddable composite key. There's a column I'd like to use as part of the key and according to SQuirreL its type name is datetime and the class name is java.sql.Timestamp. The key class looks like this:

@Embeddable
public class MyEntityIdentifier implements Serializable {

    @Column(name = "LastUpdateDateTime")
    private Timestamp lastUpdateDateTime;

...but the lastUpdateDateTime property always resolves to null without error. I've checked and there are no null fields for this column. I've also tried resolving to java.util.Date without success. Is there another type I should be using or something I'm doing wrong?

5
  • How are you mapping to the table? Could you be using a query or view that isn't including that column? Commented Jan 17, 2018 at 18:08
  • With the @Table annotation - column is definitely there :/ Commented Jan 17, 2018 at 18:10
  • 2
    the sql server jdbc driver docs say that java.sql.timestamp is the correct type to map to learn.microsoft.com/en-us/sql/connect/jdbc/… Commented Jan 17, 2018 at 18:14
  • Yeah, it all looks fine. I'm stumped. Commented Jan 17, 2018 at 18:19
  • nailed the little sucker, see answer below in case you ever run into this. Commented Jan 17, 2018 at 18:23

1 Answer 1

3

Hibernate will internally convert to a native Java type (i.e. java.util.Date as opposed to java.sql.Timestamp) by adding the @Temporal annotation.

@Column(name = "LastUpdateDateTime")
@Temporal(TemporalType.DATE)
private Date lastUpdateDateTime;
Sign up to request clarification or add additional context in comments.

2 Comments

Just wanted to add that this date treatment is not Hibernate-specific, but mandated for any JPA implementation
Also look into the @DateTimeFormat annotation provided by Spring in case you are using Spring JPA in your project.

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.