LocalDateTime not storing in database

Viewed 11149

I am working on Spring data JPA, I have created an entity in which I am taking a date as LocalDateTime (Java 8). but while storing in DB it is not storing the actual date, it is storing some binary type value in DB.

Should I use LocalDateTime while in DB or not, if not when should I use this java 8 date API.

Entity File :-

import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 
 * @author Ravat
 *
 */

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {

    //@CreatedDate
    @Column(name = "created_date", updatable = false)
    private LocalDateTime createdDate = LocalDateTime.now();

    //@LastModifiedDate
    @Column(name = "updated_date")
    private LocalDateTime updatedDate = LocalDateTime.now();

}
2 Answers

Reference Link How to persist LocalDate and LocalDateTime with JPA

JPA will map it to a BLOB instead of a DATE or TIMESTAMP. That means the database is not aware of the date object and cannot apply any optimization for it. That’s not the way we should or want to do it.

But that doesn’t mean that you can’t use the Date and Time API. You just have to decide how you want to add the support for it.

If you want to store a LocalDate attribute in a DATE column or a LocalDateTime in a TIMESTAMP column, you need to define the mapping to java.sql.Date or java.sql.Timestamp yourself.

Thanks to the attribute converter, one of several new features in JPA 2.1, this can be achieved with just a few lines of code which can be seen in a post here.

e.g.,

@Converter(autoApply = true)
public class LocalDateAttributeConverter implements
        AttributeConverter<LocalDate, Date> {

    @Override
    public Date convertToDatabaseColumn(LocalDate locDate) {
        return (locDate == null ? null : Date.valueOf(locDate));
    }

    @Override
    public LocalDate convertToEntityAttribute(Date sqlDate) {
        return (sqlDate == null ? null : sqlDate.toLocalDate());
    }
}

You need to implement the AttributeConverter interface with its two methods convertToDatabaseColumn and convertToEntityAttribute. As you can see on the method names, one of them defines the conversion from the type of the entity attribute (LocalDate) to the database column type (Date) and the other one the inverse conversion. The conversion itself is very simple because java.sql.Date already provides the methods to do the conversion to and from a LocalDate.

Additionally the attribute converter needs to be annotated with the @Converter annotation. Due to the optional autoApply=true property, the converter will be applied to all attributes of type LocalDate

Hi if your sole purpose is to do auditing why not use spring Timestamps annotations.

public abstract class BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@CreationTimestamp
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "create_date")
private Date createdAt;

@UpdateTimestamp
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modify_date")
private Date updatedAt;
Related