【问题标题】:IllegalArgumentException in class: ..., getter method of property: id类中的 IllegalArgumentException:...,属性的 getter 方法:id
【发布时间】:2011-08-07 09:13:13
【问题描述】:

我遇到了一个奇怪的问题,我用谷歌搜索了几个小时,但没有找到解决方法。

情况如下:我有两个类RolesPrivileges,关系为ManyToMany。在向角色添加权限时,调用 saveOrUpdate(role) 时,我遇到了这个异常。

这里是角色类

@Entity
@Table(name = "ROLES")
public class Role implements GenericDomain {

private static final long serialVersionUID = -7620550658984151796L;

private Long    id;
private String  code;
private String  name;

private Set<User> users = new HashSet<User>(0);
private Set<Privilege> privileges = new HashSet<Privilege>(0);

public Role() {
}

public Role(String code, String name) {
    this.code = code;
    this.name = name;
}


@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID")
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }

@Column(name = "CODE", unique = true, nullable = false, length = 16)
@NotEmpty(message= "password.required")
@Length(min = 3, max = 16)
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }

@Column(name="NAME", nullable = false, length = 64)
@NotEmpty
@Length(min = 1, max = 32)
public String getName() { return name; }
public void setName(String name) { this.name = name; }

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "ROLES_PRIVILEGES"
    , joinColumns = { @JoinColumn(name = "ROLE_ID") }
    , inverseJoinColumns = { @JoinColumn(name = "PRIVILEGE_ID") }
)

public Set<Privilege> getPrivileges() {
    return this.privileges;
}
public void setPrivileges(Set<Privilege> privileges) {
    this.privileges = privileges;
}
    /*  overide of hascode, equals*/ 
}

这里是特权类

@Entity
@Table(name = "PRIVILEGES")
public class Privilege implements GenericDomain {

private static final long serialVersionUID = 4649689934972816194L;

private Long    id;
private String  code;

private Set<Role> roles = new HashSet<Role>(0);

public Privilege() {
}

public Privilege(String code) {
    this.code = code;
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID")
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }

@Column(name = "CODE", unique = true, nullable = false, length = 16)
@NotEmpty(message= "password.required")
@Length(min = 3, max = 16)
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }

@ManyToMany(cascade=CascadeType.REFRESH, mappedBy="privileges")
public Set<Role> getRoles() {
    return this.roles;
}
public void setRoles(Set<Role> roles) {
    this.roles = roles;
}

/*overide equals and hascode*/

}

这里是个例外:

 IllegalArgumentException in class: com.stunaz.domain.Privilege, getter method of property: id
 ....
 javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.stunaz.domain.Privilege.id
 ....
 java.lang.IllegalArgumentException: object is not an instance of declaring class

我的映射似乎有问题,我应该在某个地方传递一个对象,但我传递的是一个 ID。但我没有看到那个错误。

感谢您的建议。

【问题讨论】:

  • 我定义了代码,就像您在示例中所做的那样,它对我来说效果很好。也许您可以显示您在帖子中提到的方法 saveOrUpdate 。也许问题就在那里。
  • 是的,你是对的,休眠完全没有问题
  • 这个问题你解决了吗?我在我的项目中遇到了同样的错误。

标签: hibernate spring-mvc hibernate-mapping jta


【解决方案1】:

在告诉用户映射出了什么问题时,Hibernate 对用户并不友好。

解决办法:

  1. 调试应用程序
  2. 为抛出 IllegalArgumentException 的事件设置断点(任何地方)
  3. 执行导致此问题的操作
  4. 向上遍历调用堆栈并检查活动堆栈变量。

使用这个过程,我发现我的映射出了什么问题。

一般来说,据我所见,它通常是错误的类在某处明确说明,例如@MapKeyClass(Wrong.class),而密钥实际上是String 等。

或者,调用错误的 (Hibernate) API,例如 setParameter() 而不是 setParameterList()

Query query = session.getNamedQuery(queryName);  
// org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter
query.setParameter("children", aCollectionOfChildren);
query.list();

或者,在 Criteria API 的情况下,我已经看到了:

DetachedCriteria 暴击 = DetachedCriteria.forClass( Group.class ); crit.add(Restrictions.eq("parent", id));

Group 的 getParent() 方法返回一个 Group 但我正在尝试 将其与 Long 进行比较。

【讨论】:

    【解决方案2】:

    为了获得调用 getId() 方法的非法参数异常,Hibernate 似乎认为您的 id 类型不是 Long(可能是 Integer)。也许尝试将 @Type(type="long") 添加到您的 ID。

    每当我在使用 Hibernate 时遇到奇怪的问题时,我总是附上源代码并围绕错误发生的位置进行调试。这可以让您深入了解 Hibernate 正在尝试做什么,并帮助找出您可能遗漏了什么,或者在某个地方传递了一个错误的参数。

    【讨论】:

    • 感谢您的回答,但它没有帮助。请告诉我更多有关您如何调试的信息,您要附加哪个源?你在哪里以及如何附加?
    【解决方案3】:

    乍一看,您的代码似乎很好,但可能是:

    @ManyToMany(cascade=CascadeType.REFRESH, mappedBy="privileges")
    

    我不确定这是否不正确,但我就是这样做的:

    @ManyToMany(cascade=CascadeType.REFRESH, mappedBy="privileges", targetEntity = Roles.class)
    

    这个 mappedBy 属性甚至可以省略...

    【讨论】:

      【解决方案4】:

      你如何保存/更新这个?

      您是否正在通过调用 findById(Long roleId) 获取要为其保存“权限”的“角色”对象?一旦获得该角色对象,就创建一个新的权限对象并设置角色(角色)并设置其他属性并调用保存或更新(权限)?应该可以的。

      【讨论】:

        【解决方案5】:

        只是给其他人的注释,尽管这可能与此问题无关。我碰巧将来自 REST API 的 DTO 映射到实体。其中一个子 DTO 未正确映射到子实体(我使用的是 Dozer)。 Hibernate 失败是因为 DTO 的 id 与 Entity 的 id 不兼容。

        【讨论】:

          猜你喜欢
          • 2023-03-18
          • 1970-01-01
          • 2018-08-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-06
          相关资源
          最近更新 更多