gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:20:36 30 4
gpt4 key购买 nike

我有两个类RolePrivilege,其关系为ManyToMany。将 Privilege 添加到 Role,然后调用 saveOrUpdate(role) 时,出现以下异常。

这是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*/
}

这是Privilege类:

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

这是一个异常(exception):

 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。

最佳答案

在告诉用户映射出了什么问题时,Hibernate 对用户不太友好。

解决方案:

  1. 调试应用
  2. 为抛出 IllegalArgumentException 事件设置断点(任何地方)
  3. 执行导致此情况的操作
  4. 向上遍历调用堆栈并检查 Activity 堆栈变量。

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

一般来说,从我所见,通常是在某个地方明确声明了错误的类,例如当键实际上是String等时@MapKeyClass(Wrong.class)

或者,调用错误的 (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 ) );

The getParent() method of Group returns a Group but I was attempting to compare it against a Long.

关于java - 类 : . .. 中的 IllegalArgumentException 属性的 getter 方法:id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694899/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com