因此,我有一个这样的表,它已定义为 hibernate 状态的实体,如下所示:
@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;
}
}
当我尝试针对此表编写一个简单查询时:
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);
}
我收到以下错误:
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
为什么不能 hibernate 找出类上的dbgroupid?
最佳答案
可能是因为您的getter(和setter)没有遵循javabeans约定。它应该是:
public long getDbgroupId() {
return dbgroupid;
}
我的建议是-命名您的字段,然后使用您的IDE生成 setter 和 getter 。它将遵循惯例。 (另一件事,这是一个优先事项,但我认为这使一类课更易于阅读-为您的字段加上注释,而不是获取方法)
关于java - Hibernate抛出HibernateQueryException : could not resolve property,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4441837/