Hibernate IllegalArgumentException调用setter

时间:2011-09-08 03:34:59

标签: java sql xml hibernate orm

我想我的mapping issue已经解决了,但现在我遇到了一个新的错误。当我尝试运行我的测试方法时,现在我看到“PropertyAccessException:在调用PageComment.userMadeTo的setter时发生了IllegalArgumentException”。我不确定是什么导致这种情况,我相信我的代码中的getter和setter都很好,所以也许这仍然是映射的问题?

HBM For User:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="User" table="user">
    <id name="username" column="Username" type="string">
        <generator class="assigned"></generator>
    </id>
    <property name="password" column="Password" type="string"></property>
    <property name="firstname" column="Firstname" type="string"></property>
    <property name="surname" column="Surname" type="string"></property>
    <property name="email" column="Email" type="string"></property>
    <property name="admin" column="Admin" type="integer"></property>

    <set name="commentsMadeTo">
        <key column="UserMadeTo"/>
        <one-to-many class="PageComment"/>
    </set>
</class>
</hibernate-mapping>

HBM For PageComment:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="PageComment" table="PageComment">
    <composite-id>
        <key-property name="userMadeBy" column="UserMadeBy" type="string" />
        <key-property name="time" column="Time" type="integer" />
        <generator class="assigned"></generator>
    </composite-id>
    <property name="commentText" column="CommentText" type="string"></property>
    <many-to-one name="userMadeTo" class="User" column="UserMadeTo" />
</class>
</hibernate-mapping>

userMadeTo属性的Getter / Setter:

public void setUserMadeBy(String aUsername)
{
    userMadeBy = aUsername;
}

public String getUserMadeTo()
{
    return userMadeTo;
}

1 个答案:

答案 0 :(得分:1)

  <many-to-one name="userMadeTo" class="User" column="UserMadeTo" />

从您的映射中,userMadeTo中的PageComment应该返回User类而不是String。所以,setter和getter应该是:

public class PageComment{

    private User user;

    public void setUserMadeBy(User aUser)
    {
        this.user = aUser;
    }

    public User getUserMadeTo()
    {
        return this.user;
    }
}