Last active
July 30, 2019 10:09
-
-
Save thjanssen/ae6ae10564285b791dca to your computer and use it in GitHub Desktop.
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
@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(); | |
} | |
} |
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
@Converter(autoApply = true) | |
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> { | |
@Override | |
public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) { | |
return locDateTime == null ? null : Timestamp.valueOf(locDateTime); | |
} | |
@Override | |
public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) { | |
return sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime(); | |
} | |
} |
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
@Entity | |
public class MyEntity { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = "id", updatable = false, nullable = false) | |
private Long id; | |
@Column | |
private LocalDate date; | |
@Column | |
private LocalDateTime dateTime; | |
... | |
} |
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
LocalDate date = LocalDate.of(2015, 8, 11); | |
TypedQuery<MyEntity> query = this.em.createQuery("SELECT e FROM MyEntity e WHERE date BETWEEN :start AND :end", MyEntity.class); | |
query.setParameter("start", date.minusDays(2)); | |
query.setParameter("end", date.plusDays(7)); | |
MyEntity e = query.getSingleResult(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yeeahh