Hibernate throws org.hibernate.HibernateException: Unable to resolve property: id
During a recent refactor to bring my hibernate objects more in line with the actual database, I've begun to throw org.hibernate.HibernateException: Unable to resolve property: id.
Previously the User.hbm.xml defined username to be the index, while in actuality id is the database key. This came into play when I had to refactor to start implementing functionality to create a new user.
I've poked around a bit on here and on google, but I'm at a loss. I'm fairly new to hibernate and it seems to me to be well formed (though obviously it's not!). Snippets below, please let me know if I've missed any critical snippet and I'll provide it. Thanks in advance!
Stack Trace:
org.hibernate.HibernateException: Unable to resolve property: id
at org.hibernate.tuple.entity.EntityMetamodel.getPropertyIndex(EntityMetamodel.java:486)
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyIndex(AbstractEntityPersister.java:1812)
at org.hibernate.engine.EntityEntry.getLoadedValue(EntityEntry.java:254)
at org.hibernate.type.CollectionType.getKeyOfOwner(CollectionType.java:364)
at org.hibernate.type.CollectionType.resolve(CollectionType.java:425)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:139)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:982)
at org.hibernate.loader.Loader.doQuery(Loader.java:857)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1842)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)
at space.data.hibernate.HibernateDatabase.loadUser(HibernateDatabase.java:178)
at space.data.hibernate.HibernateDatabase.loadUser(HibernateDatabase.java:22)
at space.network.authenticator.AuthenticationRequestProcessor.isValidUser(AuthenticationRequestProcessor.java:424)
at space.network.authenticator.AuthenticationRequestProcessor.processMessage(AuthenticationRequestProcessor.java:196)
at space.network.authenticator.AuthenticationRequestProcessor.processMessage(AuthenticationRequestProcessor.java:182)
at space.network.authenticator.AuthenticationRequestProcessor.process(AuthenticationRequestProcessor.java:119)
at space.network.authenticator.AuthenticationServer.execute(AuthenticationServer.java:153)
at space.network.authenticator.AuthenticationServer.main(AuthenticationServer.java:46)
Calling Method:
Line 178 being Transaction tx = global_session.beginTransaction
Line 22 is the Class Declaration for User
public User loadUser(String username)
{
User user = null;
String sql = "SELECT * FROM User WHERE name='" + username + "';";
// Logger.logData(sql);
List<User> users = null;
try
{
Transaction tx = global_session.beginTransaction();
users = (List<User>) global_session.createSQLQuery(sql)
.addEntity(User.class).list();
tx.commit();
}
catch (Exception e)
{
e.printStackTrace();
}
if (users != null && users.size() > 0)
{
user = users.get(0);
}
return user;
}
User.hbm.xml
<hibernate-mapping>
<class name="space.data.hibernate.User" table="User">
<id access="field" column="id" name="id" type="int">
<generator class="increment"/>
</id>
<!--<id access="field" column="name" name="username" type="java.lang.String">
<generator class="increment"/>
</id> -->
<property generated="never" lazy="false" name="username" type="java.lang.String">
<column name="name"/>
</property>
<property generated="never" lazy="false" name="password" type="java.lang.String">
<column name="password"/>
</property>
<!-- <property generated="never" lazy="false" name="id" type="int">
<column name="id"/>
</property>-->
<property generated="never" lazy="false" name="walletAmount" type="double">
<column name="wallet"/>
</property>
<set name="assets" table="InstanceObject">
<key column="pOwnerId" property-ref="id"/>
<one-to-many class="space.data.hibernate.InstanceObject"/>
</set>
<set name="knowledge" table="Knowledge">
<key column="pOwnerId" property-ref="id"/>
<one-to-many class="space.data.hibernate.Knowledge"/>
</set>
<set name="missions" table="MiningMission">
<key column="pOwnerId" property-ref="id"/>
<one-to-many class="space.data.hibernate.MiningMission"/>
</set>
</class>
</hibernate-mapping>
User class:
public class User implements space.data.generic.User
{
private int id;
private String username;
private String password;
private String error;
private Set&开发者_JS百科lt;InstanceObject> assets;
private Set<Knowledge> knowledge;
private Set<MiningMission> missions;
private double walletAmount;
protected boolean loggedIn;
public User()
{
super();
loggedIn = false;
}
public User(String username, String password)
{
super();
this.username = username;
this.password = password;
loggedIn = false;
error = "";
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return password;
}
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return id;
}
public void setErrorMessage(String error)
{
this.error = error;
}
public String getErrorMessage()
{
return error;
}
}
It is being caused by this line:
<key column="pOwnerId" property-ref="id"/>
No property has been defined named id, just an id named id...so when hibernate does a property lookup for 'id' it finds nothing.
Also I wrote this code and fixed it this way.
It's telling you that your User class doesn't have a property named "id". From your question, it seems that the "username" property used to be mapped as the hibernate id field. Is that right? Just changing
<id name="username">
to
<id name="id">
is no good. You have to actually have a property called "id" in the User class.
精彩评论