1

Thanks to this thread, I was able to register and use a custom Converter for org.joda.time.DateTime using JPA EclipseLink. Here is a sample use (only the relevant parts):

@Converter(name = "jodaTimeConverter", converterClass = JodaDateTimeConverter.class)
public class MyEntity{

    @Column(name = "creationdate")
    @Temporal(TemporalType.TIMESTAMP)
    @Convert("jodaTimeConverter")
    private DateTime creationdate; 
}

I have many entity classes an most of them have a DateTime field. My question is thus: is it possible to register the converter once somewhere, so that all DateTime fields are automatically converted ?

I could obviously copy-paste the annotations everywhere, but a more DRY method would be appreciated.

Community
  • 1
  • 1
Derlin
  • 9,572
  • 2
  • 32
  • 53
  • A JPA 2.1 `AttributeConverter` can be set to "autoApply", hence applying to all instances of the type in question. And using that you are portable, unlike using EclipseLink extension classes ... – Neil Stockton Apr 06 '17 at 17:13
  • Thank you, that was exactly what I was looking for. Do you want to answer it properly so I can upvote ? – Derlin Apr 06 '17 at 18:31

2 Answers2

1

What you're trying to use is a proprietary mechanism that would only work in EclipseLink, so leaving your code non-portable.

A better option, if using JPA 2.1, is to make use of AttributeConverter, and set the converter itself to "autoApply". This means that it will be applied to all fields of the specified type without having to annotate every field. And with that you get portability too

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
0

if you are using spring boot and the AttributeConverter.

in Application.java
@EntityScan(basePackageClasses = {Application.class, JpaConverters.class})


public class JpaConverters {

    @Converter(
        autoApply = true
    )
    public static class DateTimeOffsetToOffsetDateTimeConverter implements AttributeConverter<OffsetDateTime,
                                                                                                DateTimeOffset> {
        @Override
        public OffsetDateTime convertToEntityAttribute(DateTimeOffset dateTimeOffset) {
            if (dateTimeOffset == null) {
                return null;
            }

            OffsetDateTime utc = OffsetDateTime.ofInstant(dateTimeOffset.getTimestamp().toInstant(), UTC);
            int offsetSeconds = Math.toIntExact(MINUTES.toSeconds(dateTimeOffset.getMinutesOffset()));
            ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSeconds);

            OffsetDateTime offsetDateTime = utc.withOffsetSameInstant(offset);
            return offsetDateTime;
        }

        @Override
        public DateTimeOffset convertToDatabaseColumn(OffsetDateTime date) {
            if (date == null) {
                return null;
            }

            Timestamp timestamp = Timestamp.from(date.toInstant());
            int offsetSeconds = date.getOffset().getTotalSeconds();
            DateTimeOffset dateTimeOffset = DateTimeOffset.valueOf(timestamp, Math.toIntExact(SECONDS.toMinutes(offsetSeconds)));
            return dateTimeOffset ;
        }
    }
}

lfalcantar
  • 18
  • 5