- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 Hibernate 新手,很难理解 HQL。下面是我的 DAO,它返回查询异常。我真的很感谢对此的任何帮助,我已经浏览过这个网站和官方文档,但仍然无法使其工作。谢谢!!
仅供引用:映射是.xml,无法切换到JPA
堆栈跟踪:
type Exception report
message Request processing failed; nested exception is org.hibernate.QueryException: illegal attempt to dereference collection [feature0_.id.actors] with element property reference [name] [SELECT f from ar.edu.uces.progweb2.springmvc.model.Feature as f where f.name like :term or f.actors.name like :term or f.genre.name like :term order by f.name ASC ]
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.QueryException: illegal attempt to dereference collection [feature0_.id.actors] with element property reference [name] [SELECT f from ar.edu.uces.progweb2.springmvc.model.Feature as f where f.name like :term or f.actors.name like :term or f.genre.name like :term order by f.name ASC ]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.hibernate.QueryException: illegal attempt to dereference collection [feature0_.id.actors] with element property reference [name] [SELECT f from ar.edu.uces.progweb2.springmvc.model.Feature as f where f.name like :term or f.actors.name like :term or f.genre.name like :term order by f.name ASC ]
org.hibernate.hql.internal.ast.tree.DotNode$1.buildIllegalCollectionDereferenceException(DotNode.java:68)
org.hibernate.hql.internal.ast.tree.DotNode.checkLhsIsNotCollection(DotNode.java:550)
org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:246)
org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:118)
org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:114)
org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:881)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1264)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4297)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3913)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1947)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1900)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1897)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:794)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:595)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:219)
org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:197)
org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1736)
ar.edu.uces.progweb2.springmvc.dao.FeatureDAO.search(FeatureDAO.java:98)
ar.edu.uces.progweb2.springmvc.dao.FeatureDAO$$FastClassByCGLIB$$3247faf2.invoke(<generated>)
net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
ar.edu.uces.progweb2.springmvc.dao.FeatureDAO$$EnhancerByCGLIB$$f55db758.search(<generated>)
ar.edu.uces.progweb2.springmvc.controller.AppController.buscar(AppController.java:229)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.37 logs.
HBM 功能:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Qualified Class Names -->
<hibernate-mapping default-lazy="false">
<class name="ar.edu.uces.progweb2.springmvc.model.Feature" table="feature">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<discriminator column="feature_type" type="string"/>
<property name="name" column="name"/>
<property name="runtime" column="runtime"/>
<property name="coverImage" column="cover_image"/>
<property name="storyline" column="storyline"/>
<set name="showtimes" inverse="true" cascade="all" fetch="join">
<key column="feature_id"/>
<one-to-many class="ar.edu.uces.progweb2.springmvc.model.Showtime" entity-name="ar.edu.uces.progweb2.springmvc.model.Showtime"/>
</set>
<subclass name="ar.edu.uces.progweb2.springmvc.model.Movie" discriminator-value="M">
<set name="actors" inverse="true" cascade="all" fetch="join">
<key column="feature_id"/>
<one-to-many class="ar.edu.uces.progweb2.springmvc.model.Actor" entity-name="ar.edu.uces.progweb2.springmvc.model.Actor"/>
</set>
</subclass>
<subclass name="ar.edu.uces.progweb2.springmvc.model.Documentary" discriminator-value="D">
<many-to-one name="genre"
column="documentary_genre_id"
not-null="true" fetch="join"/>
</subclass>
</class>
</hibernate-mapping>
模型类(故事片、电影、纪录片)
特点:
public abstract class Feature {
private int id;
private String name;
private int runtime;
private byte[] coverImage;
private String storyline;
private String featureType;
private Set<Showtime> showtimes;
public abstract boolean isMovie();
public abstract boolean isDocumentary();
public Feature(){
this.showtimes = new HashSet<Showtime>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRuntime() {
return runtime;
}
public void setRuntime(int runtime) {
this.runtime = runtime;
}
public byte[] getCoverImage() {
return coverImage;
}
public void setCoverImage(byte[] coverImage) {
this.coverImage = coverImage;
}
public String getStoryline() {
return storyline;
}
public void setStoryline(String storyline) {
this.storyline = storyline;
}
public String getFeatureType() {
return featureType;
}
public void setFeatureType(String featureType) {
this.featureType = featureType;
}
public Set<Showtime> getShowtimes() {
return showtimes;
}
public void setShowtimes(Set<Showtime> showtimes) {
this.showtimes = showtimes;
}
}
电影:
public class Movie extends Feature {
private Set<Actor> actors;
@Override
public boolean isMovie() {
return true;
}
public Movie (){
actors = new HashSet<Actor>();
}
}
纪录片:
public class Documentary extends Feature{
private Genre genre;
@Override
public boolean isDocumentary(){
return true;
}
@Override
public boolean isMovie() {
return false;
}
public Documentary(){
this.genre = new Genre();
}
public Genre getGenre() {
return genre;
}
public void setGenre(Genre genre) {
this.genre = genre;
}
}
DAO:
public List<Feature> search(SearchForm sf){
StringBuffer query = new StringBuffer("SELECT f ");
Map<String, Object> params = new HashMap<>();
Session session = sessionFactory.getCurrentSession();
switch (sf.getFeatureType()){ //0: Both, 1: Commercial Film, 2: Documentary
case 0:
query.append("from Feature as f ");
if (sf.getTerm() != null && !sf.getTerm().isEmpty()){
query.append("where f.name like :term or f.actors.name like :term or f.genre.name like :term ");
params.put("term","%"+ sf.getTerm() + "%");
}
break;
case 1:
if (sf.getTerm() != null && !sf.getTerm().isEmpty()){
query.append("from Movie m left join m.actors a where (a.name like :term or m.name like :term) ");
params.put("term","%"+ sf.getTerm() + "%");
} else {
query.append("from Movie m ");
}
break;
case 2:
if (sf.getTerm() != null && !sf.getTerm().isEmpty()){
query.append("from Documentary d left join d.genre g where (g.name like :term or d.name like :term) ");
params.put("term", "%"+ sf.getTerm()+"%");
} else {
query.append("from Documentary d left join d.genre g ");
}
break;
}
switch (sf.getOrder()){
case "nameDesc":
query.append("order by f.name DESC ");
break;
default:
query.append("order by f.name ASC ");
break;
}
System.out.println("Query: " + query.toString());
Query q = session.createQuery(query.toString());
Iterator<String> iter = params.keySet().iterator();
while(iter.hasNext()){
String k = iter.next();
Object o = params.get(k);
q.setParameter(k, o);
}
q.setFirstResult(this.resultsPerPage * sf.getPage());
q.setMaxResults(this.resultsPerPage);
//System.out.println("Search OK!");
return q.list();
}
最佳答案
解决了,我的hql比较马虎
case 0:
query.append("from Feature f ");
if (sf.getTerm() != null && !sf.getTerm().isEmpty()){
query.append("left join fetch f.actors a left join f.genre g where f.name like :term or a.name like :term or g.name like :term ");
params.put("term","%"+ sf.getTerm() + "%");
}
break;
为您的实体考虑良好的别名非常重要,并且还了解到此类错误可能与缺少连接有关。谢谢!
关于java - 查询异常 : illegal attempt to dereference collection Hibernate Select w/subclasses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44710516/
是否有带有索引的.collect?我想做这样的事情: def myList = [ [position: 0, name: 'Bob'], [position: 0, name: 'J
我创建了一个 Collection 类,它扩展了 ArrayList 以添加一些有用的方法。它看起来像这样: public class Collection extends ArrayList {
我知道如果我有元素,我想得到 List/Set/Map 我可以调用这个元素: Collections.singleton()/Collections.singletonList()/Collectio
我刚刚在我的 pom 文件中看到 Apache commons-collections 有两个不同的组 ID: commons-collections commons-collect
我们可以对所有 Collections 类型的对象(如 Set 和 List)使用 Collections.synchronizedCollection(Collection c),这就是为什么我们有
我有List>我想让它把上一个集合中的所有人复制到List收藏。 我是这样做的: var People = new List>{ new List{...},... };
我想做的是使用良好的旧循环非常简单。 假设我有一个包含 B 列表的对象 A。 public class A { public List myListOfB; } 在其他一些方法中,我有一个 As
在 Capgemini 的采访中,我被问到一个我无法回答的问题。所有集合类和接口(interface)共有的那些方法是什么? 最佳答案 所有 java 对象类(包括所有集合)都派生自名为 Object
我有一系列存储估计信息的数据库表。当设置某些边界时,我试图从所有数据库表中返回所有数据。 收藏 $estimateItems = new Collection(); $esti
为什么 Haskell 实现如此专注于链表? 例如,我知道 Data.Sequence 效率更高 大多数列表操作(cons 操作除外),并且被大量使用; 但是,从语法上讲,它“几乎不受支持”。 Has
我试图简单地将我在 PHP 中请求的内容返回到 JSON。我的问题是每个库存尚未完成。事实上,它是“渲染”,但“this.collection.models”尚未完成,因为请求尚未完成。 我应该如何解
本质上,作为Powershell脚本的一部分,我需要实现广度优先搜索。因此,我需要队列,并且认为System.Collections.Queue与其他任何队列一样好。但是,当我从队列中取出一个对象时,
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是 on-topic用于堆栈溢出。 已关闭10 年前。 Improve t
嗨,我不明白为什么这不起作用? Notifications.update({'userId':Meteor.userId(), 'notifyUserId':notifyFriendId}, {$se
假设我有一个闭包: def increment = {value, step -> value + step } 现在我想遍历我的整数集合的每个项目,用 5 递增,并将新元素保存到一个新集合中:
使用逐页 View 时,我的 plone 集合文件夹未显示所有项目。基本上我有 9 页包含元素,但第 6 - 8 页显示的内容完全相同。因此,并非所有项目都会显示,即使项目总数对应于应该在集合中的元素
private Map> map ,其中 ProgramCourse 是我的项目中的域类,上面的 map 是我运行项目时域类 Program 的字段以下异常即将到来。 Use of @OneToMan
三者的主要区别是什么?现在,我想分别使用字符串/字符串创建一个键/值对。这三个似乎都有我可以使用的选项。 编辑:我只想创建一个简单的哈希表 - 没什么特别复杂的。 最佳答案 通用集合几乎完全取代了基础
我正在为 NodeJs 使用 mongodb 驱动程序,其中有 3 个方法: 1) db.collection.insert 2) 数据库.collection.insertOne 3) db.col
我有一个集合,我正在尝试使用 Distinct 方法删除重复项。 public static Collection imagePlaylist imagePlaylist = imagePlaylis
我是一名优秀的程序员,十分优秀!