change the OptimisticLockPolicy to use local-time

2019-08-19 02:40发布

I'm using Eclipselink JPA, I have an Entity with a Timestamp field annotated with @Version por optimistic locking.

By default, this sets the entitymanager to use database time, so, if I have to do a batch update it doesn't work properly as it query the database for time each time it wants to do an insert.

How can I change the TimestampLockingPolicy to use LOCAL_TIME?

The class org.eclipse.persistence.descriptors.TimestampLockingPolicy.class has a public method useLocalTime() but I dont know how to use or, from where should I call it.

1条回答
做自己的国王
2楼-- · 2019-08-19 03:23

Found the answer:

first lets create a DescriptorCustomizer

public class LocalDateTimeCustomizer implements DescriptorCustomizer {
    @Override
    public void customize(ClassDescriptor descriptor) throws Exception {
        OptimisticLockingPolicy policy = descriptor.getOptimisticLockingPolicy();
        if (policy instanceof TimestampLockingPolicy) {
            TimestampLockingPolicy p = (TimestampLockingPolicy) policy;
            p.useLocalTime();
        }
    }
}

then annotate the entity that has the @Version with

@Customizer(LocalDateTimeCustomizer.class)
查看更多
登录 后发表回答