Hibernate抛出HibernateQueryException:无法解析属性

时间:2010-12-14 16:57:37

标签: java hibernate spring

所以我有一个表,我在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是什么?

3 个答案:

答案 0 :(得分:14)

这可能是因为你的getter(和setter)没有遵循javabeans约定。它应该是:

public long getDbgroupId() {
    return dbgroupid;
}

我建议的是 - 命名您的字段,然后使用您的IDE生成setter和getter。它将遵循惯例。 (另一件事,这是一个偏好的问题,但在我看来,使一个班级更容易阅读 - 注释你的领域,而不是吸气剂)

答案 1 :(得分:5)

我在这方面取得了一些进展,但我仍然不明白hibernate在哪里获得它的名字。我调试了hibernate的内容并找到了以下类:

org.hibernate.persister.entity.AbstractPropertyMapping

在这个课程中有一个方法:

public Type toType(String propertyName) throws QueryException {
    Type type = (Type) typesByPropertyPath.get(propertyName);
    if (type == null) {
        throw propertyException(propertyName);
    }
    return type;
}

尝试解析针对该对象的条件中给出的名称。所以在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

在这里你可以看到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);
}

现在它有效。

答案 2 :(得分:2)

也许是因为你把它标记为“DBGroupId”,而不是“dbgroupid”?