Hallo all,
I am having a big trouble. What I am trying to do (I tried it with HQL at first) is to generate an SQL statement which adds AND clause during a loop. We are having a search mask which adds new lines for new search filters and for each added filter I have to extend the SQL.
So here is my code:
Code:
public Set<StructureItem> getDetailedAssetWithSingleAssociations(final List<String> metadataLabels, final List<String> criteriaList, final List<String> texts, final boolean loadAncestors) throws EntityNotFoundException {
Set<StructureItem> resultset = new LinkedHashSet<StructureItem>();
Criteria criteria = getSession().createCriteria(StructureItem.class);
Criteria[] itemData = new Criteria[metadataLabels.size()];
Criteria[] criteriaMetadata = new Criteria[metadataLabels.size()];
Criteria[] criteriaText = new Criteria[metadataLabels.size()];
for (int i = 0; i < metadataLabels.size(); i++) {
String restriction = criteriaList.get(i);
String itemDataString = "itemDatas"+i;
String critMetadata = "metadata" + i;
String textItem = "text" + i;
itemData[i] = criteria.createCriteria("itemDatas",itemDataString);
criteriaMetadata[i]= itemData[i].createCriteria("metadata",critMetadata);
criteriaText[i] = itemData[i].createCriteria("text",textItem);
if (restriction.equals("=")) {
criteriaMetadata[i]
.add(Restrictions.eq("label", metadataLabels.get(i)));
criteriaText[i]
.add(Restrictions.eq("text", texts.get(i)));
} else if (restriction.equals("<")) {
criteriaMetadata[i]
.add(Restrictions.lt("label", metadataLabels.get(i)));
criteriaText[i]
.add(Restrictions.lt("text", texts.get(i)));
}else if (restriction.equals("<=")){
criteriaMetadata[i]
.add(Restrictions.le("label", metadataLabels.get(i)));
criteriaText[i]
.add(Restrictions.le("text", texts.get(i)));
}else if (restriction.equals(">")){
criteriaMetadata[i]
.add(Restrictions.gt("label", metadataLabels.get(i)));
criteriaText[i]
.add(Restrictions.gt("text", texts.get(i)));
}else if(restriction.equals(">=")){
criteriaMetadata[i]
.add(Restrictions.ge("label", metadataLabels.get(i)));
criteriaText[i]
.add(Restrictions.ge("text", texts.get(i)));
}else if(restriction.equals("like")){
criteriaMetadata[i]
.add(Restrictions.like("label", metadataLabels.get(i)));
criteriaText[i]
.add(Restrictions.like("text", texts.get(i)));
}
}
List<StructureItem> results = criteria.list();
resultset = new LinkedHashSet<StructureItem>(results);
return resultset;
}
What I am getting is this error message:
ERROR (MED) -> Unexpected error!
org.springframework.orm.hibernate3.HibernateQueryException: duplicate association path: itemDatas; nested exception is org.hibernate.QueryException: duplicate association path: itemDatas
org.hibernate.QueryException: duplicate association path: itemDatas
at org.hibernate.loader.criteria.CriteriaQueryTranslator.createAssociationPathCriteriaMap(CriteriaQueryTranslator.java:138)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:80)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:69)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1303)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at geneon.duf.server.services.RepositoryFacadeImpl.getDetailedAssetWithSingleAssociations(RepositoryFacadeImpl.java:360)
at geneon.duf.server.services.RepositoryFacadeImpl.searchExtMedien(RepositoryFacadeImpl.java:1402)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:99)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy7.searchExtMedien(Unknown Source)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:68)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy8.searchExtMedien(Unknown Source)
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.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:179)
at geneon.gecont3.server.remoting.rmi.SecurityRemoteInvocationExecutor.invoke(SecurityRemoteInvocationExecutor.java:58)
at org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:71)
at org.springframework.remoting.rmi.RmiServiceExporter.invoke(RmiServiceExporter.java:365)
at org.springframework.remoting.rmi.RmiInvocationWrapper.invoke(RmiInvocationWrapper.java:48)
at sun.reflect.GeneratedMethodAccessor55.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:754)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
So I know that I cannot duplicate a criteria but I need a valid solution. I am a hibernate beginner.
Thanks for your help.