jmix-framework / jmix

Jmix framework
https://www.jmix.io
Apache License 2.0
694 stars 124 forks source link

findTopXxxx in Jmix Repository didn't seem to follow Spring Data's specifications #329

Closed codingxu97 closed 2 years ago

codingxu97 commented 2 years ago

Here is my code,I want to get entity object whose priorityNo was max.

@Repository
interface HedgingStrategyRepository : JmixDataRepository<HedgingStrategyEntity, Long> {
    // HedgingStrategyEntity, HedgingStrategyEntity? is the same
    fun findTopByOrderByPriorityNoDesc(): Optional<HedgingStrategyEntity>
}

In Spring Data Common's document, it was legal.

But it occurred exceptions in Jmix, like this:

Caused by: java.lang.ClassCastException: class java.util.Vector cannot be cast to class com.fospot.trade.pricingsystem.console.entity.hedge.HedgingStrategyEntity (java.util.Vector is in module java.base of loader 'bootstrap'; com.fospot.trade.pricingsystem.console.entity.hedge.HedgingStrategyEntity is in unnamed module of loader 'app')
    at com.fospot.trade.pricingsystem.console.service.impl.HedgingStrategyServiceImpl.genPriorityNo(HedgingStrategyServiceImpl.kt:180) ~[main/:na]
    at com.fospot.trade.pricingsystem.console.service.impl.HedgingStrategyServiceImpl$$FastClassBySpringCGLIB$$b04405c5.invoke(<generated>) ~[main/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.13.jar:5.3.13]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:783) ~[spring-aop-5.3.13.jar:5.3.13]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.13.jar:5.3.13]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753) ~[spring-aop-5.3.13.jar:5.3.13]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.13.jar:5.3.13]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.13.jar:5.3.13]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.13.jar:5.3.13]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.13.jar:5.3.13]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753) ~[spring-aop-5.3.13.jar:5.3.13]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698) ~[spring-aop-5.3.13.jar:5.3.13]
    at com.fospot.trade.pricingsystem.console.service.impl.HedgingStrategyServiceImpl$$EnhancerBySpringCGLIB$$5d02ab54.genPriorityNo(<generated>) ~[main/:na]
    at com.fospot.trade.pricingsystem.console.screen.hedge.hedgingstrategy.HedgingStrategyEdit.onBeforeCommitChanges(HedgingStrategyEdit.kt:17) ~[main/:na]
    at io.jmix.core.common.event.EventHub.publish(EventHub.java:170) ~[jmix-core-1.1.2.jar:na]
    at io.jmix.ui.screen.Screen.fireEvent(Screen.java:124) ~[jmix-ui-1.1.2.jar:na]
    at io.jmix.ui.screen.StandardEditor.commitChanges(StandardEditor.java:445) ~[jmix-ui-1.1.2.jar:na]
    at io.jmix.ui.screen.StandardEditor.closeWithCommit(StandardEditor.java:616) ~[jmix-ui-1.1.2.jar:na]
    at io.jmix.ui.screen.StandardEditor.commitAndClose(StandardEditor.java:570) ~[jmix-ui-1.1.2.jar:na]
    at io.jmix.core.common.event.EventHub.publish(EventHub.java:170) ~[jmix-core-1.1.2.jar:na]
    at io.jmix.ui.action.BaseAction.actionPerform(BaseAction.java:220) ~[jmix-ui-1.1.2.jar:na]
    at io.jmix.ui.component.impl.ButtonImpl.buttonClicked(ButtonImpl.java:75) ~[jmix-ui-1.1.2.jar:na]
    at io.jmix.ui.widget.JmixButton.fireClick(JmixButton.java:77) ~[jmix-ui-1.1.2.jar:na]
    at com.vaadin.ui.Button$1.click(Button.java:57) ~[vaadin-server-8.14.1-1-jmix.jar:8.14.1-1-jmix]
    ... 88 common frames omitted

If I modified reture type to List<HedgingStrategyEntity>, it works.

I had debugged source code, it seems that the following code in class JmixQueryLookupStrategy led to the exceptions:

@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata, ProjectionFactory factory, NamedQueries namedQueries) {
    Query query = method.getDeclaredAnnotation(Query.class);
    JmixAbstractQuery resolvedQuery;
    if (query != null) {
        String qryString = query.value();
        resolvedQuery = new JmixCustomLoadQuery(dataManager, jmixMetadata, method, repositoryMetadata, factory, qryString);
    } else {
        PartTree qryTree = new PartTree(method.getName(), repositoryMetadata.getDomainType());
        if (qryTree.isDelete()) {
            resolvedQuery = new JmixDeleteQuery(dataManager, jmixMetadata, method, repositoryMetadata, factory, qryTree);
        } else if (qryTree.isCountProjection()) {
            resolvedQuery = new JmixCountQuery(dataManager, jmixMetadata, method, repositoryMetadata, factory, qryTree);
        } else if (qryTree.isExistsProjection()) {
            resolvedQuery = new JmixExistsQuery(dataManager, jmixMetadata, method, repositoryMetadata, factory, qryTree);
        } else {
            resolvedQuery = new JmixListQuery(dataManager, jmixMetadata, method, repositoryMetadata, factory, qryTree);
        }
    }

    if (log.isDebugEnabled()) {
        log.debug(String.format("Query for %s resolved: %s", method, resolvedQuery.toString()));
    }

   return resolvedQuery;

}

QA Notes:

  1. Open project
  2. Change jmix version to required one.
  3. Run test. ER: test should be passed
natfirst commented 2 years ago

verified on 1.2.999 SNAPSHOT 1.3.999 SNAPSHOT jmix versions

knstvk commented 2 years ago

@dtaimanov - remove org.omg.CORBA.portable.Streamable import