actframework / act-eclipselink

Provide JPA support via EclipseLink
Apache License 2.0
1 stars 1 forks source link

Not support eclipselink session customizer #10

Closed leeaee closed 6 years ago

leeaee commented 6 years ago

Define a EclipseLink custom id generator

import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.internal.databaseaccess.Accessor;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.sequencing.Sequence;
import org.eclipse.persistence.sessions.Session;
import org.osgl.util.S;

import java.util.Vector;

public class UuidSequence extends Sequence implements SessionCustomizer {

    public UuidSequence() {
        super();
    }

    public UuidSequence(String name) {
        super(name);
    }

    @Override
    public void customize(Session session) {
        UuidSequence sequence = new UuidSequence("system-uuid");
        session.getLogin().addSequence(sequence);
    }

    @Override
    public boolean shouldAcquireValueAfterInsert() {
        return false;
    }

    @Override
    public boolean shouldUseTransaction() {
        return false;
    }

    @Override
    public Object getGeneratedValue(Accessor accessor, AbstractSession abstractSession, String s) {
        return S.uuid().toLowerCase().replaceAll("-","");
    }

    @Override
    public Vector getGeneratedVector(Accessor accessor, AbstractSession abstractSession, String s, int i) {
        return null;
    }

    @Override
    public void onConnect() {
    }

    @Override
    public void onDisconnect() {
    }

}

Add configuration in properties like

db.sql.eclipselink.session.customizer=eclipselink.UuidSequence

Use the id generator in entity

    @Id
    @GeneratedValue(generator = "system-uuid")
    public String id;

And when doing save operation, it will throw NullPointerException:

11:23:59.650 [XNIO-1 task-1] ERROR act.handler.builtin.controller.RequestHandlerProxy - error handling request: [POST]/api/v1/users
java.lang.NullPointerException: null
    at org.eclipse.persistence.internal.sequencing.SequencingManager$Preallocation_NoTransaction_State.getNextValue(SequencingManager.java:673)
    at org.eclipse.persistence.internal.sequencing.SequencingManager.getNextValue(SequencingManager.java:1107)
    at org.eclipse.persistence.internal.sequencing.ClientSessionSequencing.getNextValue(ClientSessionSequencing.java:70)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.assignSequenceNumber(ObjectBuilder.java:366)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.assignSequenceNumber(ObjectBuilder.java:325)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.assignSequenceNumber(UnitOfWorkImpl.java:530)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNotRegisteredNewObjectForPersist(UnitOfWorkImpl.java:4386)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.registerNotRegisteredNewObjectForPersist(RepeatableWriteUnitOfWork.java:521)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4331)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:593)
    at act.db.jpa.JPADao._save(JPADao.java:146)
    at act.db.jpa.JPADao.save(JPADao.java:140)
    at me.domall.users.service.UserSerivce.saveUser(UserSerivce.java:25)
    at me.domall.users.controller.UserController.addUser(UserController.java:29)
    at me.domall.users.controller.UserControllerMethodAccess.invoke(Unknown Source)
    at act.handler.builtin.controller.impl.ReflectedHandlerInvoker.invoke(ReflectedHandlerInvoker.java:994)
    at act.handler.builtin.controller.impl.ReflectedHandlerInvoker.handle(ReflectedHandlerInvoker.java:648)
    at act.handler.builtin.controller.ControllerAction.handle(ControllerAction.java:49)
    at act.handler.builtin.controller.RequestHandlerProxy._handle(RequestHandlerProxy.java:555)
    at act.handler.builtin.controller.RequestHandlerProxy.handle(RequestHandlerProxy.java:212)
    at act.app.ActionContext.proceedWithHandler(ActionContext.java:1304)
    at act.route.Router$ContextualHandler.handle(Router.java:1603)
    at act.xio.NetworkHandler$3.run(NetworkHandler.java:165)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
11:24:23.826 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Pool stats (total=10, active=0, idle=10, waiting=0)

Offical link: https://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing

greenlaw110 commented 6 years ago

@leeaee The issue is in the implementation of UuidSequence, you must overwrite shouldUsePreallocation and return false:

    @Override
    public boolean shouldUsePreallocation() {
        return false;
    }

See https://github.com/actframework/act-eclipselink/blob/master/testapps/GH10/src/main/java/test/UuidSequence.java

leeaee commented 6 years ago

@greenlaw110 Add the code, it works. Thank you for point the correct usage of the eclipselink session customizer.