2011-04-17 45 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不兼容。

相關問題