change the OptimisticLockPolicy to use local-time

帅比萌擦擦* 提交于 2019-12-11 14:09:19

问题


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:


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)


来源:https://stackoverflow.com/questions/47997401/change-the-optimisticlockpolicy-to-use-local-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!