2011-04-17 135 views
7

我面临一个奇怪的问题,我已经搜索了几个小时,但没有找到如何解决它。IllegalArgumentException类:...,getter方法的属性:ID

这里是情况:我有两个类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 

它似乎somethi ng对我的映射是错误的,在某个地方我应该传递一个对象,但我传递了一个Id.but但我没有看到那个错误。

感谢您的任何建议。

+0

我定义的代码就像你在你的例子中做的一样,它对我来说工作得很好。也许你可以显示你在文章中提到的saveOrUpdate方法。也许问题在那里。 – 2011-04-18 15:00:29

+0

是的,你是对的没有休眠的问题 – 2011-04-19 22:54:48

+0

你解决了这个问题吗?我在我的项目中遇到同样的错误。 – 2012-01-13 18:01:41

回答

0

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

每当我遇到有关Hibernate的奇怪问题时,我都会附上源代码并调试错误发生的位置。这可以让你深入了解Hibernate正在尝试做什么,并帮助找出你可能错过了什么地方,或者在某个地方传递了一个糟糕的论点。

+0

谢谢你的回答,但它没有帮助。请告诉我更多关于你如何调试,你附加哪个源?你在哪里以及如何附加? – 2011-04-18 01:19:09

0

乍一看你的代码似乎罚款除非是:

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

我不知道这是不正确,但是这是我做的方式:

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

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

+0

谢谢,这并没有帮助((( – 2011-04-19 00:41:06

0

你是如何保存/更新?

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

1

当谈到告诉用户映射有什么问题时,Hibernate并没有多大的用户友好性。

解决方案:

  1. 调试应用程序
  2. 设置抛出IllegalArgumentException的事件断点被抛出(任意位置)
  3. 执行这将导致此
  4. 上去,通过调用堆栈中的操作和检查活动堆栈变量。

使用这个程序,我找出了我的映射有什么问题。

一般来说,从我所看到的,它通常是错误的类明确地规定的地方,像@MapKeyClass(Wrong.class)把钥匙是String

或者调用错误(休眠)API,像setParameter(),而不是setParameterList()

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

或者,在标准API的情况下,我已经看到了这一点:

DetachedCrite ria crit = DetachedCriteria.forClass(Group.class); crit.add(Restrictions.eq(“parent”,id));

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

0

只是为他人提供一个说明,尽管这可能与此问题无关。我碰巧遇到了将来自REST API的DTO映射到实体。其中一个孩子的DTO没有正确地映射到一个孩子实体(我正在使用推土机)。 Hibernate失败是因为DTO的id与实体的id不兼容。

相关问题