【发布时间】:2015-05-10 10:21:38
【问题描述】:
我是 Hibernate 的新手,在将 JPQL 查询定义到应用程序时遇到以下问题。
所以我有以下情况:
1) 我有这个模型 KMCountryArea 类,该类被注释为将其映射到数据库表并定义查询
@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;
}
}
正如你在之前的代码 sn-p 中看到的,我定义了一个名为 kmCountryListByName 的 JPQL 查询,这个:
@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 具体类实现:
@Repository("kmCountryAreaService")
public class KMCountryAreaServiceImpl extends AbstractService implements KMCountryAreaService {
public List<KMCountry> getCountryListByName(Long idCountry){
List<KMCountry> list = getHibernateTemplate().findByNamedQuery("kmCountryListByName");
return list;
}
}
如您所见,getCountryListByName() 方法获取了我在模型类(KMCountryArea)中定义的kmCountryListByName。
问题是当我尝试启动我的应用程序(它是一个 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 查询(模型类中定义的名为 kmCountryListByName 的查询)或 KMCountryAreaServiceImpl 服务实现中似乎有一些错误。
为什么?我错过了什么?我该如何解决这个问题?
【问题讨论】:
-
您有
order by country.idCountry,但该字段实际上名为idCountryArea。
标签: java sql hibernate jakarta-ee jpql