我在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;
}
}
正如您在前面的代码段中看到的,我定义了一个名为 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()方法获取我在模型类中定义的 kmCountryListByName ( KMCountryArea )。
问题在于,当我尝试启动我的应用程序时(它是LifeRay门户但我认为它并不重要)我在stacktrace中获得以下错误消息:
<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 服务实现中出现了一些错误。
为什么呢?我错过了什么?我该如何解决这个问题?
答案 0 :(得分:1)
尝试这样的事情:
@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子句