Hibernate抛出org.hibernate.HibernateException:无法解析property:id

时间:2011-07-17 16:05:44

标签: hibernate hibernate-mapping

在最近的重构中,我的hibernate对象更符合实际的数据库,我开始抛出org.hibernate.HibernateException:无法解析property:id。

以前User.hbm.xml将用户名定义为索引,而实际上id是数据库密钥。当我不得不重构以开始实现创建新用户的功能时,这就发挥了作用。

我在这里和谷歌上都戳了一下,但我不知所措。我对休眠很新,在我看来形成得很好(虽然显然不是!)。下面的片段,如果我错过了任何关键片段,请告诉我,我会提供。提前谢谢!

堆栈追踪:

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)

通话方式:

第178行是交易tx = global_session.beginTransaction

第22行是用户的类声明

    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>

用户类:

public class User implements space.data.generic.User
{

    private int id;
    private String username;
    private String password;
    private String error;
    private Set<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;
    }
}

2 个答案:

答案 0 :(得分:1)

这是由这条线引起的:

<key column="pOwnerId" property-ref="id"/>

没有任何属性被定义为名为id,只是一个名为id的id ...所以当hibernate对'id'进行属性查找时,它什么都没找到。

我也写了这段代码并以这种方式修复。

答案 1 :(得分:0)

它告诉您,您的User类没有名为“id”的属性。从您的问题来看,似乎“username”属性曾被映射为hibernate id字段。是对的吗?只是改变

<id name="username">

<id name="id">

不好。您必须在User类中实际拥有一个名为“id”的属性。