JadiraOrg / jadira

Jadira Framework
Apache License 2.0
74 stars 44 forks source link

Migrate from Jadira 7.0.0.CR1 to Java 17 #121

Open rcbandit111 opened 1 year ago

rcbandit111 commented 1 year ago

I use 'org.jadira.usertype:usertype.core:7.0.0.CR1' into my project. I want to migrate to Java 17. I have the following columns definitions:

  @Column(name = "update_date")
  @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
  private DateTime requestDate;

Do you know how I can replace the code for latest Java 17/Hibernate/Spring Boot?

theigl commented 1 year ago

it looks like this project has been abandoned.

I recently finished migrating my project to Hibernate 6. You have a couple of options:

  1. Use java.time.OffsetDateTime instead of DateTime
  2. Write your own custom type for DateTime

Something like this:

public class DateTimeJavaType extends AbstractClassJavaType<DateTime> {

    public static final DateTimeJavaType INSTANCE = new DateTimeJavaType();

    public DateTimeJavaType() {
        super(DateTime.class, ImmutableMutabilityPlan.INSTANCE);
    }

    @Override
    public <X> X unwrap(DateTime value, Class<X> type, WrapperOptions options) {
        if (value == null) {
            return null;
        }
        if (DateTime.class.isAssignableFrom(type)) {
            return (X) value;
        }
        if (Timestamp.class.isAssignableFrom(type)) {
            return (X) new Timestamp(value.toDate().getTime());
        }
        throw unknownUnwrap(type);
    }

    @Override
    public <X> DateTime wrap(X value, WrapperOptions options) {
        if (value == null) {
            return null;
        }
        if (value instanceof Timestamp) {
            return new DateTime(value);
        }
        if (value instanceof DateTime) {
            return (DateTime) value;
        }
        throw unknownWrap(value.getClass());
    }

    @Override
    public JdbcType getRecommendedJdbcType(JdbcTypeIndicators indicators) {
        return indicators.getTypeConfiguration()
                .getJdbcTypeRegistry()
                .getDescriptor(Types.TIMESTAMP);
    }
}

Then you can use it like this:

  @JavaType(DateTimeJavaType.class)
  private DateTime until;
rcbandit111 commented 1 year ago

@theigl Thanks for the reply. Using java.time.OffsetDateTime looks good enough. Any issues that I might face?

yili001 commented 1 year ago

I have the similar problem, but it's about currency mapping:

@Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmount")
private Money fee;

anyone gets any idea to work around this?