为什么这个Hibernate JPQL查询不起作用?

时间:2015-03-17 10:33:03

标签: java sql spring hibernate jpql

我在 Hibernate JPQL 查询中绝对是新手,我在尝试启动以下查询时遇到了一些问题。情况如下,我有:

1)名为 KMCountryArea 的实体类,用于映射我的数据库中的 KM_COUNTRY_AREA 表:

@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;
    }
}

正如您所看到的,此类和相关表只有3个字段:

  • 行ID

    @Id
    @GeneratedValue
    private Long idCountryArea;
    
  • nomeFolder 字符串与表格中的 nomeFolder 列匹配:

    @Column(name = "nomeFolder")
    private String nomeFolder;
    
  • KMCountry国家/地区代表一对一与另一个 KMCountry 实体类的关系定义到我自己定义的项目中此字段为id private Long idCountry;

    @OneToOne
    private KMCountry country;
    

事实上,这个Hibernate实体类在我的数据库中正确生成 KM_COUNTRY 表,这是我在数据库中看到的 KM_COUNTRY 表字段:

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    

我认为这是正确的,因为在我看来,定义在 KMCountryArea 实体类中的3个字段已正确映射到 KM_COUNTRY 表中。

所以我在 KM_COUNTRY 表中手动插入了一行(通过插入查询,执行这个简单的选择查询(在Oracle SQL Developer中作为DBMS IDE):

select * from KM_COUNTRY_AREA where nomefolder = 'BRAZIL';

我获得了以下正确结果:

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

现在我必须使用JPQL执行类似此查询的操作,而是获取一组行(在前一种情况下它只有一行但我可以获得更多行)我想获取 KMCountry的列表具有已定义ID的对象(在实体内)等于 COUNTRY_IDCOUNTRY 值。

因此,正如您所看到的,我实现了 JPQL命名查询

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

然后,在我的项目中,我已经定义了执行上一个查询的DAO服务,实现了这个具体的类:

@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;
    }

}

正如您所看到的那样,它被定义为Spring存储库并实现了一个 KMCountryAreaService 接口,我只是列出了CRUD实现的方法(在这个阶段的工作中只存在 getCountryListByName()

好吧然后在我的代码中(进入一个动作类)我执行这个操作:

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

所以我希望检索 KM_COUNTRY 表中的 KMCountry 对象列表 NOMEFOLDER 字段值等于 BRAZIL

但事实上,它不起作用,使用调试器,我看到执行进入 getCountryListByName()方法,但是当它尝试执行此语句时:

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

抛出异常(但是它的消息不会打印到堆栈跟踪中)。

我错过了什么?我该如何尝试解决这个问题?我疯狂地试图理解什么是错的。

TNX

修改-1

我修改了 getCountryListByName()方法来跟踪激活,现在我的方法是:

public List<KMCountry> getCountryListByName(String nomeFolder) {

    List<KMCountry> list = null;

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

    return list;
}

获得的重复是:

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)

1 个答案:

答案 0 :(得分:3)

您没有将folderName传递给查询。试试这个

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