简体   繁体   中英

Why this Hibernate JPQL query can't work?

I am absolutly new in Hibernate and in JPQL queries and I have some problem trying to actuate the following query. The situation is the following one, I have:

1) An entity class named KMCountryArea that map the KM_COUNTRY_AREA table on my database:

@NamedQueries({
        //@NamedQuery(name = "kmCountryListByName", query = "SELECT c FROM KMCountryArea c WHERE c.nomeFolder  = :nomeFolder order by c.idCountryArea")
        @NamedQuery(name = "kmCountryListByName", query = "SELECT c FROM KMCountryArea c WHERE c.nomeFolder  = :nomeFolder order by c.idCountryArea")
})

/*@NamedQuery(name = "kmCountryListByName", query = "SELECT country FROM KMCountryArea country WHERE country.nomeFolder  = :nomeFolder order by country.idCountry")*/

@Entity
@Table(name = "KM_COUNTRY_AREA")
public class KMCountryArea implements Serializable {

    @Id
    @GeneratedValue
    private Long idCountryArea;

    @Column(name = "nomeFolder")
    private String nomeFolder;

    //@Column(name = "country")
    //@OneToOne(mappedBy = "country", cascade = CascadeType.ALL)
    @OneToOne
    private KMCountry country;

    public Long getIdCountryArea() {
        return idCountryArea;
    }

    public void setIdCountryArea(Long idCountryArea) {
        this.idCountryArea = idCountryArea;
    }

    public String getNomeFolder() {
        return nomeFolder;
    }

    public void setNomeFolder(String nomeFolder) {
        this.nomeFolder = nomeFolder;
    }

    public KMCountry getCountry() {
        return country;
    }

    public void setCountry(KMCountry country) {
        this.country = country;
    }
}

As you can see this class and the related table have only 3 fields:

  • The row id :

     @Id @GeneratedValue private Long idCountryArea; 
  • A nomeFolder String that match with the nomeFolder column on the table:

     @Column(name = "nomeFolder") private String nomeFolder; 
  • The KMCountry country that represent a one to one relation with another KMCountry entity class definied into my project that itself have definied this field as id private Long idCountry;

     @OneToOne private KMCountry country; 

Infact this Hibernate entity class correctly generate the KM_COUNTRY table inside my database, this is the KM_COUNTRY table fields that I see inside the DB:

COLUMN NAME          DATA_TYPE           NULLABLE    DATA_DEFAULT   COLUMN_ID
--------------------------------------------------------------------------------
IDCOUNTRYAREA        NUMBER(19,0)          No           (null)             1    
NOMEFOLDER           VARCHAR2(255 CHAR)    Yes          (null)             2    
COUNTRY_IDCOUNTRY    NUMBER(19,0)          Yes          (null)             3    

I think that it is correct because it seems to me that the 3 fields definied into the KMCountryArea entity class are correctly mapped inside the KM_COUNTRY table.

So I have manually inserted a row inside the KM_COUNTRY table (by an insert query, and performing this simple select query (inside the Oracle SQL Developer that is the DBMS IDE):

select * from KM_COUNTRY_AREA where nomefolder = 'BRAZIL';

I obtain the following correct result:

IDCOUNTRYAREA        NOMEFOLDER      COUNTRY_IDCOUNTRY
------------------------------------------------------------
     2                 BRAZIL              715

Now I have to do something like this query using JPQL but instead obtain a set of rows (in the previous case it is only one but I can obtain more rows) I want obtain the list of the KMCountry object having the definied id (inside the entity) equals to the COUNTRY_IDCOUNTRY value.

So, as you can see, I implemented this JPQL named query :

@NamedQuery(name = "kmCountryListByName", query = "SELECT c FROM KMCountryArea c WHERE c.nomeFolder  = :nomeFolder order by c.idCountryArea")

Then, in my project, I have definied the DAO service that perform the previous query, implementing this concrete class:

@Repository("kmCountryAreaService")
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class KMCountryAreaServiceImpl extends AbstractService implements KMCountryAreaService {

    public List<KMCountry> getCountryListByName(String folderName){
        List<KMCountry> list = getHibernateTemplate().findByNamedQuery("kmCountryListByName");
        return list;
    }

}

As you can see it is definied as a Spring repository and implements a KMCountryAreaService interface in wich I simply list the CRUD implmented method (at this stage of works exist only the getCountryListByName() )

Ok then in my code (into an action class) I perform this operation:

List<KMCountry> kmCountryListAssociated = kmCountryAreaService.getCountryListByName("BRAZIL", folderName);

So I expect to retrieve the list of KMCountry object that into the KM_COUNTRY table have the NOMEFOLDER field value equals to BRAZIL .

But it don't works, infact, using the debugger, I see that the execution go into the getCountryListByName() method but when it try to perform this statment:

List<KMCountry> list = getHibernateTemplate().findByNamedQuery("kmCountryListByName");

an exception is thrown (but its message it is not printed into the stacktrace).

What am I missing? How can I try to solve this issue? I am going crazy trying to understand what is wrong.

Tnx

EDIT-1 :

I have modified the getCountryListByName() method to track the excpetion, now my method is:

public List<KMCountry> getCountryListByName(String nomeFolder) {

    List<KMCountry> list = null;

    try{
        list = getHibernateTemplate().findByNamedQuery("kmCountryListByName", nomeFolder);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return list;
}

and the obtained excpetion is:

java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
    at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:55)
    at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:61)
    at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:397)
    at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:369)
    at org.springframework.orm.hibernate3.HibernateTemplate$33.doInHibernate(HibernateTemplate.java:985)
    at org.springframework.orm.hibernate3.HibernateTemplate$33.doInHibernate(HibernateTemplate.java:1)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:979)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:975)
    at egp.prc.km.services.KMCountryAreaServiceImpl.getCountryListByName(KMCountryAreaServiceImpl.java:34)
    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:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy377.getCountryListByName(Unknown Source)
    at egp.prc.km.actions.countryArea.CountryAreaAction.showMultiSelectCountry(CountryAreaAction.java:205)
    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 com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
    at egp.prc.km.utils.interceptors.LiferayAjaxInterceptor.intercept(LiferayAjaxInterceptor.java:28)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.servlet.StrutsServlet.service(StrutsServlet.java:76)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)

You are not passing folderName into the query. Try this

List<KMCountry> list = getHibernateTemplate().findByNamedQueryAndNamedParam("kmCountryListByName", "nomeFolder", folderName);

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