类中的IllegalArgumentException:...,属性的getter方法:id

时间:2011-04-17 16:52:43

标签: hibernate spring-mvc hibernate-mapping jta

我面临一个奇怪的问题,我已经用Google搜索了几个小时,但没有找到解决方法。

以下是这种情况:我有两个类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.but我没有看到这个错误。

感谢任何建议。

5 个答案:

答案 0 :(得分:1)

当告诉用户映射有什么问题时,Hibernate对用户不太友好。

解决方案:

  1. 调试应用
  2. 为抛出IllegalArgumentException事件设置断点(任何地方)
  3. 执行导致此操作的操作
  4. 上升调用堆栈并检查活动堆栈变量。
  5. 使用这个程序,我弄清楚我的映射有什么问题。

    一般来说,从我所看到的情况来看,它通常是在某个地方明确说明的错误类,如@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 crit = DetachedCriteria.forClass(Group.class);   crit.add(Restrictions.eq(“parent”,id));

         

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

答案 1 :(得分:0)

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

每当我遇到奇怪的Hibernate问题时,我总是附上源代码并调试错误发生的地方。这可以让你深入了解Hibernate正在尝试做什么,并帮助弄清楚你可能错过了什么,或者在某处传递了一个不好的论点。

答案 2 :(得分:0)

乍一看,你的代码看起来很好,除非是:

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

我不确定这是不正确的,但这是我的方式:

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

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

答案 3 :(得分:0)

您如何保存/更新此内容?

您是否通过调用findById(Long roleId)来获取要为其保存“权限”的“角色”对象?获得该角色对象后,创建一个新的Permission对象和setRole(角色)并设置其他属性并调用saveOrUpdate(权限)?这应该有用。

答案 4 :(得分:0)

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