我有两个类Role
和Privilege
,它们之间的关系是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*/
}
这里有个例外:
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
似乎我的Map出了问题,在某个地方我应该传递一个对象,但我传递的是一个ID。
5条答案
按热度按时间1mrurvl11#
Hibernate在告诉用户Map有什么问题时并不是很友好。
解决方法:
1.调试应用程序
1.为抛出IllegalArgumentException的事件设置断点(任意位置)
1.执行导致此问题的操作
1.向上遍历调用堆栈并检查活动堆栈变量。
使用此过程,我发现了Map的问题所在。
一般来说,从我所看到的,它通常是错误的类明确地陈述某处,如
@MapKeyClass(Wrong.class)
时,关键字实际上是String
等。或者,调用错误的(Hibernate)API,例如
setParameter()
而不是setParameterList()
:或者,在Criteria API的情况下,我看到了以下内容:
分离条件crit =类的分离条件(组.类);(“父”,标识符));
Group的getParent()方法返回一个Group,但我试图将其与Long进行比较。
pxiryf3j2#
为了得到一个非法参数异常调用getId()方法,Hibernate似乎认为你的id的类型不是Long(可能是Integer)。也许尝试添加
@Type(type="long")
到你的id。每当我在使用Hibernate时遇到奇怪的问题时,我总是附加源代码,并在错误发生的地方进行调试。这可以给予您了解Hibernate正在尝试做什么,并帮助找出您可能遗漏了什么,或在某处传递了一个错误的参数。
2ul0zpep3#
乍看之下,您的代码似乎很好,除了以下几点:
我不确定这是否正确,但这是我的做法:
甚至可以省略此mappedBy属性...
q3qa4bjr4#
您如何保存/更新此信息?
是否通过调用findById(Long roleId)获取要为其保存“权限”的“角色”对象?获取该角色对象后,创建一个新的权限对象并设置角色(role),设置其他属性并调用saveOrUpdate(permission)?应该可以。
vsmadaxz5#
只是给其他人的一个注解,尽管这可能与此问题无关。我碰巧将来自REST API的DTOMap到实体。其中一个子DTO未正确Map到子实体(我使用的是Dozer)。由于DTO的ID与实体的ID不兼容,因此Hibernate失败。