简体   繁体   中英

java.lang.IllegalArgumentException: The attribute [A] from the managed type [T] is not present

I'm trying to do a search page that retrieves some data from my database using JPA 2.0 Criteria API. I'm getting the same exception error everytime I try to do the search.

Here is my search method:

public List<Matches> search(SearchCommercialsDTO searchCommercialsDTO) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Matches> criteria = builder.createQuery( Matches.class );
    Root<Matches> matchesRoot = criteria.from( Matches.class );
    criteria.select( matchesRoot );

    List<Predicate> predicateList = new ArrayList<Predicate>();

    Predicate date, equipmentName, channelCode, advertiserName, agencyName, productName, duration;

    if(!ObjectUtil.isEmpty(searchCommercialsDTO.getEquipmentName())) {
        equipmentName = builder.like(matchesRoot.get("ID_RECORDER_FILES.EQUIPMENT_NAME").as(String.class), searchCommercialsDTO.getEquipmentName());
        predicateList.add(equipmentName);
    }
    if(!ObjectUtil.isEmpty(searchCommercialsDTO.getChannelCode())) {
        channelCode = builder.equal(matchesRoot.get("ID_RECORDER_FILES.CHANNEL_CODE"), searchCommercialsDTO.getChannelCode());
        predicateList.add(channelCode);
    }
    if(!ObjectUtil.isEmpty(searchCommercialsDTO.getAdvertiserName())) {
        advertiserName = builder.equal(matchesRoot.get("ID_SOURCE_MATERIAL.ADVERTISER_NAME"), searchCommercialsDTO.getAdvertiserName());
        predicateList.add(advertiserName);
    }
    if(!ObjectUtil.isEmpty(searchCommercialsDTO.getAgencyName())) {
        agencyName = builder.equal(matchesRoot.get("ID_SOURCE_MATERIAL.AGENCY_NAME"), searchCommercialsDTO.getAgencyName());
        predicateList.add(agencyName);
    }
    if(!ObjectUtil.isEmpty(searchCommercialsDTO.getProductName())) {
        productName = builder.equal(matchesRoot.get("ID_SOURCE_MATERIAL.PRODUCT_NAME"), searchCommercialsDTO.getProductName());
        predicateList.add(productName);
    }

    Predicate[] predicates = new Predicate[predicateList.size()];
    predicateList.toArray(predicates);
    criteria.where(predicates);

    return em.createQuery(criteria).getResultList();
}

When it tries to execute this part:

equipmentName = builder.like(matchesRoot.get("ID_RECORDER_FILES.EQUIPMENT_NAME").as(String.class), searchCommercialsDTO.getEquipmentName());

It throws the following exception:

java.lang.IllegalArgumentException: The attribute [ID_RECORDER_FILES.EQUIPMENT_NAME] from the managed type [EntityTypeImpl@112477145:Matches [ javaType: class net.checkmidia.auditoria.entity.Matches descriptor: RelationalDescriptor(net.checkmidia.auditoria.entity.Matches --> [DatabaseTable(MATCHES)]), mappings: 12]] is not present.
    at org.eclipse.persistence.internal.jpa.metamodel.ManagedTypeImpl.getAttribute(ManagedTypeImpl.java:147)
    at org.eclipse.persistence.internal.jpa.querydef.FromImpl.get(FromImpl.java:312)
    at net.checkmidia.auditoria.business.MatchesBO.search(MatchesBO.java:50)
    at net.checkmidia.auditoria.session.MatchesSession.searchMatches(MatchesSession.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:42)
    at sun.reflect.GeneratedMethodAccessor126.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
    at sun.reflect.GeneratedMethodAccessor72.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)
    ... 51 more

How is this caused and how can I solve it?

Change

matchesRoot.get("ID_RECORDER_FILES.EQUIPMENT_NAME")

to

matchesRoot.get("ID_RECORDER_FILES").get("EQUIPMENT_NAME")

and also verify that "ID_RECORDER_FILES" is the name of a field of your Java class Matches , and "EQUIPMENT_NAME" is the name of a field in the class of the "ID_RECORDER_FILES" field.

The method get() takes the name of an attribute, so you must pass just the id and then use the resulting Path to get the field contained in that object.

matchesRoot.get(“ID_RECORDER_FILES”)。get(“EQUIPMENT_NAME”)为我做了诀窍。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM