Java HQL语句中的length()不起作用
所以我对HQL语句有一些问题。语句Java HQL语句中的length()不起作用,java,spring,hibernate,hql,hibernate-mapping,Java,Spring,Hibernate,Hql,Hibernate Mapping,所以我对HQL语句有一些问题。语句node,其中node.dflag=?和长度(node.path)=?和node.rootEntityId=?应该是基于的有效HQL语句 这些是我的gradle依赖项: compile group: 'org.springframework', name: 'spring-core', version: '3.2.16.RELEASE' compile group: 'org.springframework', name: 'spring-web', versi
node,其中node.dflag=?和长度(node.path)=?和node.rootEntityId=?
应该是基于的有效HQL语句
这些是我的gradle依赖项:
compile group: 'org.springframework', name: 'spring-core', version: '3.2.16.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '3.2.16.RELEASE'
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.10.Final'
compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.4.1.Final'
compile group: 'org.hibernate.hql', name: 'hibernate-hql-parser', version: '1.1.0.Final'
当我删除length()
部分时,它工作正常
hql.append("from ").append(SystemComponentRef.class.getName()).append(" d where d.dflag=? and d.rootEntityId=?");
List<?> list = db.find(SystemComponentRef.class, hql.toString(), 0, SystemEntity.SYS_ROOT_ENTITY_ID);
//this code works fine
这与将
length()
与HQL
一起使用无关
这是一个封装问题,因为在SystemComponentRef
bean中,您将path
声明为protected
,因此无法从length(node.path)
中的HQL查询访问它,因为在Hibernate中,它是一种面向对象的查询语言,类似于SQL,但是HQL不是对表和列进行操作,而是处理持久的对象及其属性
这就是为什么你会有例外:
could not resolve property: path of: backend.SystemComponentRef [from backend.SystemComponentRef node where node.dflag=? and length(node.path)=?
也就是说,无法解析属性path
,换句话说,找不到(无法访问)
解决方案:
您需要将修饰符更改为private
并使用accessors
,以便它遵守封装规则,并且可以在类包之外访问,您需要记住这一点
:
我建议您查看更多详细信息。PostSystemComponentRef。class@KhalilM updated@KhalilM你要求提供bean代码是对的。是的,这是异常的来源有趣,因为我尝试将其公开,但仍然得到相同的错误。此外,我的
dflag
和rootEntityId
也在基类中受到保护。然而,当我删除path
时,它会起作用。您需要将它们声明为private
字段并使用accessors
。
hql.append("from ").append(SystemComponentRef.class.getName()).append(" d where d.dflag=? and d.rootEntityId=?");
List<?> list = db.find(SystemComponentRef.class, hql.toString(), 0, SystemEntity.SYS_ROOT_ENTITY_ID);
//this code works fine
@Entity
@Table(name = "sysreference")
public class SystemComponentRef extends TRoot{
private String name;
protected String path;
protected String parentId;
protected Integer treeRecycleFlag;
protected Integer treeDeleteFlag;
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "parent_id")
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
@Column(name = "path")
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Column(name = "recycle_flag")
public Integer getTreeRecycleFlag() {
return treeRecycleFlag == null ? 0 : treeRecycleFlag;
}
public void setTreeRecycleFlag(Integer treeRecycleFlag) {
this.treeRecycleFlag = treeRecycleFlag;
}
@Column(name = "delete_flag")
public Integer getTreeDeleteFlag() {
return treeDeleteFlag == null ? 0 : treeDeleteFlag;
}
public void setTreeDeleteFlag(Integer treeDeleteFlag) {
this.treeDeleteFlag = treeDeleteFlag;
}
}
could not resolve property: path of: backend.SystemComponentRef [from backend.SystemComponentRef node where node.dflag=? and length(node.path)=?