为什么我获得与我的 JPQL 查询相关的 HibernateException(命名查询中的错误)?

Why I obtain this HibernateException (Errors in named queries) related to my JPQL query?

我完全是 Hibernate 新手,在将 JPQL 查询定义到应用程序中时遇到以下问题。

所以我有以下情况:

1) 我有这个模型 KMCountryArea class 注释将其映射到数据库 table 并且定义了一个查询

@NamedQueries({
        @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;
    }
}

如您在前面的代码片段中所见,我定义了一个名为 kmCountryListByNameJPQL 查询,这个:

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

2) 然后我有一个 DAO 定义一个名为 KMCountryAreaService 的接口,这个:

public interface KMCountryAreaService {

    @Transactional
    public List<KMCountry> getCountryListByName(Long idCountry);

}

KMCountryAreaServiceImpl 具体实现 class:

@Repository("kmCountryAreaService")
public class KMCountryAreaServiceImpl extends AbstractService implements KMCountryAreaService {

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

}

因此,如您所见,getCountryListByName() 方法获取我在模型中定义的 kmCountryListByName class (KMCountryArea).

问题是当我尝试启动我的应用程序时(它是一个 LifeRay 门户,但我认为它并不重要)我在堆栈跟踪中收到以下错误消息:

 <Stack trace for message 149004
weblogic.application.ModuleException: :org.hibernate.HibernateException:Errors in named queries: kmCountryListByName
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:365)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
    Truncated. see log file for complete stacktrace
> 

我的 JPQL 查询(在模型 class 中定义的名为 kmCountryListByName 的查询)似乎有一些错误,或者可能在 KMCountryAreaServiceImpl 服务实现。

为什么?我错过了什么?我该如何解决这个问题?

尝试这样的事情:

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

})

你有一个国家属性,同时你正试图用这个国家的名字做别名。 或者你想加入他们吗?

如果您尝试加入他们并按国家/地区对象中的特定 属性 进行排序,您可以尝试这样的操作

@NamedQueries({
@NamedQuery(name = "kmCountryListByName", query = "SELECT c FROM KMCountryArea c JOIN c.country cc WHERE c.nomeFolder  = :nomeFolder order by cc.idCountry")

在上面的示例中,您可以看到两个对象之间的连接以及基于 Country 对象的 order by 子句 属性