gpt4 book ai didi

java - Hibernate IllegalArgumentException 调用 setter

转载 作者:行者123 更新时间:2023-12-01 15:44:34 26 4
gpt4 key购买 nike

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

HBM 对于用户:

<?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 页面评论:

<?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;
}

最佳答案

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

根据您的映射,PageComment 中的 userMadeTo 应返回 User 类而不是字符串。所以,setter 和 getter 应该是:

public class PageComment{

private User user;

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

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

关于java - Hibernate IllegalArgumentException 调用 setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7342852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com