我試圖做一個搜索頁面,使用JPA 2.0 Criteria API從我的數據庫中檢索一些數據。每次我嘗試執行搜索時,都會收到相同的異常錯誤。java.lang.IllegalArgumentException:來自託管類型[T]的屬性[A]不存在
這裏是我的搜索方法:
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();
}
當它試圖執行此部分:
equipmentName = builder.like(matchesRoot.get("ID_RECORDER_FILES.EQUIPMENT_NAME").as(String.class), searchCommercialsDTO.getEquipmentName());
它拋出以下異常:
java.lang.IllegalArgumentException: The attribute [ID_RECORDER_FILES.EQUIPMENT_NAME] from the managed type [[email protected]: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
這是怎麼引起的我該如何解決它?
向我們展示您的ejb異常跟蹤。 – 2013-03-22 13:04:36
對不起!異常追蹤現在存在問題。 我認爲這個問題是關於這個事實,我需要訪問一些數據庫中的其他表與主錶鏈接,在這種情況下,匹配。這就是爲什麼我的matchesRoot.get與我的代碼的某些部分中的字符串「idRecorderFiles.someattribute」,但我不知道這是否真的有效! – 2013-03-22 14:59:40