Java Hibernate 抛出 HibernateQueryException:无法解析属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4441837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate throws HibernateQueryException: could not resolve property
提问by Grant Cermak
So I have a table that I've defined as an entity in hibernate like this:
所以我有一个表,我在休眠中定义为一个实体,如下所示:
@Entity
@Table(name = "sec_Preference")
public class Preference {
private long id;
@Column(name = "PreferenceId", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
private long systemuserid;
@Column(name = "SystemUserId", nullable = true, insertable = true, updatable = true, length = 19, precision = 0)
@Basic
public long getSystemUserId() {
return systemuserid;
}
public void setSystemUserId(long systemuserid) {
this.systemuserid = systemuserid;
}
private long dbgroupid;
@Column(name = "DBGroupId", nullable = true, insertable = true, updatable = true, length = 19, precision = 0)
@Basic
public long getDBGroupId() {
return dbgroupid;
}
public void setDBGroupId(long dbgroupid) {
this.dbgroupid = dbgroupid;
}
private long externalgroupid;
@Column(name = "ExternalGroupId", nullable = true, insertable = true, updatable = true, length = 19, precision = 0)
@Basic
public long getExternalGroupId() {
return externalgroupid;
}
public void setExternalGroupId(long externalgroupid) {
this.externalgroupid = externalgroupid;
}
private long securityroleid;
@Column(name = "SecurityRoleId", nullable = true, insertable = true, updatable = true, length = 19, precision = 0)
@Basic
public long getSecurityRoleId() {
return securityroleid;
}
public void setSecurityRoleId(long securityroleid) {
this.securityroleid = securityroleid;
}
public void setEnum(com.vitalimages.common.server.security.Preference pref) {
this.preferencekey = pref.name();
}
private String preferencekey;
@Column(name = "PreferenceKey", nullable = false, insertable = true, updatable = true, length = 255, precision = 0)
@Basic
public String getKey() {
return preferencekey;
}
public void setKey(String key) {
this.preferencekey = key;
}
private String preferencevalue;
@Column(name = "PreferenceValue", nullable = true, insertable = true, updatable = true, length = 255, precision = 0)
@Basic
public String getValue() {
return preferencevalue;
}
public void setValue(String value) {
this.preferencevalue = value;
}
}
When I tried to write a simple query against this table:
当我尝试针对该表编写一个简单的查询时:
public Collection<Preference> getPreferencesForDBGroup(long dbgroupId) {
final DetachedCriteria criteria = DetachedCriteria.forClass(Preference.class)
.add(Restrictions.eq("dbgroupid", dbgroupId))
.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
return getHibernateTemplate().findByCriteria(criteria);
}
I got the following error:
我收到以下错误:
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: dbgroupid of: com.common.server.domain.sec.Preference; nested exception is org.hibernate.QueryException: could not resolve property: dbgroupid of: com.common.server.domain.sec.Preference
Why can't hibernate figure out what dbgroupid is on my class?
为什么休眠无法弄清楚我班上的 dbgroupid 是什么?
采纳答案by Bozho
It's probably because your getter (and setter) is not following the javabeans convention. It should be:
这可能是因为您的 getter(和 setter)没有遵循 javabeans 约定。它应该是:
public long getDbgroupId() {
return dbgroupid;
}
What I'd suggest is - name your fields, and then use your IDE to generate setters and getters. It will follow the convention. (Another thing, that is a matter of preference, but in my opinion makes a class easier to read - annotate your fields, not getters)
我的建议是 - 命名您的字段,然后使用您的 IDE 生成 setter 和 getter。它将遵循公约。(另一件事,这是一个偏好问题,但在我看来,使类更易于阅读 - 注释您的字段,而不是 getter)
回答by Pimgd
Maybe because You've labelled it "DBGroupId", and not "dbgroupid"?
也许是因为您将其标记为“DBGroupId”而不是“dbgroupid”?
回答by Grant Cermak
Well I made some progress on this but I still don't understand where hibernate gets its names. I debugged into the guts of hibernate and found the following class:
好吧,我在这方面取得了一些进展,但我仍然不明白 hibernate 的名称是从哪里来的。我调试了 hibernate 的内部结构,发现了以下类:
org.hibernate.persister.entity.AbstractPropertyMapping
In this class there is a method:
在这个类中有一个方法:
public Type toType(String propertyName) throws QueryException {
Type type = (Type) typesByPropertyPath.get(propertyName);
if (type == null) {
throw propertyException(propertyName);
}
return type;
}
Which tries to resolve the name given in the criteria against the object. So in the typesByPropertyPath map I found the following values:
它尝试根据对象解析条件中给出的名称。所以在 typesByPropertyPath 映射中,我找到了以下值:
id -> DBGroupId=org.hibernate.type.LongType@1e96ffd
key -> value=org.hibernate.type.StringType@aa2ee4
value -> value=org.hibernate.type.StringType@aa2ee4
systemUserId -> DBGroupId=org.hibernate.type.LongType@1e96ffd
securityRoleId -> DBGroupId=org.hibernate.type.LongType@1e96ffd
externalGroupId -> DBGroupId=org.hibernate.type.LongType@1e96ffd
DBGroupId -> DBGroupId=org.hibernate.type.LongType@1e96ffd
Here you can see that the CAPITALIZATION of DBGroupId did not match what I had in my criteria. So I changed that from dbgroupid to DBGroupId like this:
在这里您可以看到 DBGroupId 的 CAPITALIZATION 与我的标准不匹配。因此,我将其从 dbgroupid 更改为 DBGroupId,如下所示:
public Collection<Preference> getPreferencesForDBGroup(long dbgroupId) {
final DetachedCriteria criteria = DetachedCriteria.forClass(Preference.class)
.add(Restrictions.eq("DBGroupId", dbgroupId))
.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
return getHibernateTemplate().findByCriteria(criteria);
}
Now it works.
现在它起作用了。