Hibernate throws a HibernateQueryException: failed to resolve property

So, I have a table that I defined as an entity in sleep mode, for example:

@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 on 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 determine what dbgroupid is in my class?

+8
java spring hibernate
source share
3 answers

Probably because your getter (and setter) does not follow the javabeans convention. It should be:

 public long getDbgroupId() { return dbgroupid; } 

What I suggest is name your fields, and then use your IDE to create setters and getters. He will follow the convention. (Another thing is a matter of preference, but, in my opinion, it makes it easier to read the class - annotate your fields, not getters)

+14
source share

Well, I have made some progress on this, but I still don't understand where sleep mode gets its names. I was debugging the sleep gut and found the following class:

 org.hibernate.persister.entity.AbstractPropertyMapping 

There is a method in this class:

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

Who is trying to resolve the name specified in the criteria for the object. Therefore, on the type map of ByPropertyPath, 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 CAPITALIZATION DBGroupId does not match what I had in my criteria. So I changed this from dbgroupid to DBGroupId as follows:

 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.

+5
source share

Perhaps because you named it "DBGroupId" rather than "dbgroupid"?

+2
source share

Source: https://habr.com/ru/post/650134/


All Articles