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

sto*_*ter 7 hibernate jta spring-mvc hibernate-mapping

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

情况就是这样:我有两个班级RolesPrivilegesManyToMany的关系.在向角色添加权限时,调用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*/ 
}
Run Code Online (Sandbox Code Playgroud)

这是特权阶层

@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*/
Run Code Online (Sandbox Code Playgroud)

}

这是例外:

 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
Run Code Online (Sandbox Code Playgroud)

我的映射似乎有些问题,我应该传递一个对象,但我传递了一个Id.but我没有看到那个错误.

感谢任何建议.

Sea*_*son 0

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

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