10

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?

3 Answers 3

16

It's probably because your getter (and setter) is not following the javabeans convention. It should be:

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)

Sign up to request clarification or add additional context in comments.

4 Comments

@Ralph - yes, it is not prohibited. But I took his field name as a starting point, hence I suggested a getter change. It could've been the other way around. But you are right that multiple capital letters in the beginning is tricky.
You are right - is miss understud your anser - so i remove my comment already
I had a similar problem where I was using all uppercase and was not finding the right getter: .add(Restrictions.eq("DBGROUPID", dbgroupId))
Instead of letting your IDE generate the getters and setters, better use lombok
6

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:

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:

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:

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.

Comments

2

Maybe because You've labelled it "DBGroupId", and not "dbgroupid"?

1 Comment

Is that because I have it declared on the getXXX method? And that is where it gets the capitalization?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.