Created
November 15, 2016 15:28
-
-
Save codecitizen/e78865f68e9fd727c3e2ccb591ffe430 to your computer and use it in GitHub Desktop.
JPA Attribute Converter for java.time.LocalDateTime to java.sql.Instant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.gist.codecitizen.jpa.converters; | |
import javax.persistence.AttributeConverter; | |
import javax.persistence.Converter; | |
import java.sql.Timestamp; | |
import java.time.LocalDateTime; | |
/** | |
* Maps {@link java.time.LocalDateTime} entity attributes to {@link java.sql.Timestamp} values | |
* so a temporal value instead of {@code bytea} is written to the database. | |
* | |
* @author codecitizen (Github) | |
*/ | |
@SuppressWarnings(value = "unused") | |
@Converter(autoApply = true) | |
public class LocalDateTimePersistenceConverter implements AttributeConverter<LocalDateTime, Timestamp> { | |
@Override | |
public Timestamp convertToDatabaseColumn(LocalDateTime attribute) { | |
return (attribute == null)? null : Timestamp.valueOf(attribute); | |
} | |
@Override | |
public LocalDateTime convertToEntityAttribute(Timestamp dbData) { | |
return (dbData == null) ? null : dbData.toLocalDateTime(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment