IllegalArgumentException in class: ..., getter method of property: id

I have to deal with some strange problem, I have been looking for it for many hours, but did not know how to solve it.

Here is the situation: I have two Roles and Privileges classes with ManyToMany relation. When adding privileges to a role calling saveOrUpdate(role) , I got into this exception.

here is the role class

 @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*/ } 

Here is a privilege class

 @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*/ 

}

And with the exception of:

  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 

It seems that something is wrong with my mapping, that somewhere I have to pass the object, but I pass Id.but, I do not see this error.

Thanks for any advice.

+7
source share
5 answers

Hibernate is not very user friendly when it comes to telling the user what is wrong with the display.

Decision:

  • Application debugging
  • Set a breakpoint for an event that is raised by IllegalArgumentException (anywhere)
  • Perform the operation that causes this.
  • Go to the call stack and check the active stack variables.

Using this procedure, I found out what was wrong with my comparison.

In general, from what I saw, this is usually the wrong class, explicitly indicated somewhere, for example @MapKeyClass(Wrong.class) , when the key is actually String , etc.

Or by calling the wrong (Hibernate) API, for example setParameter() instead of setParameterList() :

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

Or, in the case of the criteria API, I saw this:

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

The getParent () method of the group returns the group, but I tried to compare it with a long one.

+1
source

To get an exception to an illegal argument that calls the getId () method, it seems that Hibernate thinks that your id type is something other than Long (possibly Integer). Perhaps try adding @Type(type="long") to your identifiers.

Whenever I have strange problems with Hibernate, I always attach the source code and debug where the error occurs. This may give you some idea of ​​what Hibernate is trying to do, and help you figure out where you might have missed something, or missed a bad argument somewhere.

0
source

At first glance, your code seems great, except perhaps for:

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

I'm not sure if this is wrong, but I do this:

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

This mappedBy property can even be omitted ...

0
source

How do you save / update this?

Do you get a Role object for which you want to keep the Permission by calling findById (Long roleId)? Once you get this role object, create a new Permission and setRole object (role) and set other properties and call saveOrUpdate (permission)? That should work.

0
source

Just a note to others, although this may not be related to this problem. It happened to me that I mapped the DTO going from the REST API to Entity. One of the child DTOs was not properly bound to the child Entity (I used Dozer). Hibernate failed because the DTO identifier was not compatible with the Entity identifier.

0
source

All Articles