在StackOverflow中有很多关于这方面的问题。但我还是解决不了。我正在用Hibernate作为我的持久化层来编码一个税务系统。现在,我有两个实体类: Role.java和RolePrivilege.java。我是Role.java
package com.taxsys.nsfw.role.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="role")
public class Role implements Serializable{
@Id
@GenericGenerator(name="pk_hilo", strategy="hilo")
@GeneratedValue(generator="pk_hilo")
@Column(name="role_id")
private String roleId;
@Column(name="role_name",length=32, nullable=false)
private String roleName;
@Column(name="state", length=1)
private String state;
@OneToMany(targetEntity=RolePrivilege.class,
mappedBy="role", cascade=CascadeType.ALL,
fetch=FetchType.EAGER)
private Set<RolePrivilege> role = new HashSet<>();
public static String ROLE_STATE_VALID = "1";
public static String ROLE_STATE_INVALID = "0";
public Set<RolePrivilege> getRolePrivilege() {
return role;
}
public void setRolePrivilege(Set<RolePrivilege> rolePrivilege) {
this.role = rolePrivilege;
}
public String getRoleId() {
return roleId;
}
public void setRoleId(String roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Role() {
}
public Role(String roleId, String roleName, String state, Set<RolePrivilege> rolePrivilege) {
super();
this.roleId = roleId;
this.roleName = roleName;
this.state = state;
this.role = rolePrivilege;
}
}这是RolePrivilege.java:
package com.taxsys.nsfw.role.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="role_privilege")
public class RolePrivilege implements Serializable{
@ManyToOne(targetEntity=Role.class, fetch=FetchType.EAGER)
@JoinColumn(name="role_id", referencedColumnName="role_id")
@Id
private Role role;
@Id
private String code;
public RolePrivilege(Role role, String code) {
super();
this.role = role;
this.code = code;
}
public RolePrivilege() {
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((code == null) ? 0 : code.hashCode());
result = prime * result + ((role == null) ? 0 : role.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RolePrivilege other = (RolePrivilege) obj;
if (code == null) {
if (other.code != null)
return false;
} else if (!code.equals(other.code))
return false;
if (role == null) {
if (other.role != null)
return false;
} else if (!role.equals(other.role))
return false;
return true;
}
}我使用Hibernate3.6、spring3.0、struts2.3和tomcat7。但是,当我启动Tomcat时,错误消息将显示:
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.taxsys.nsfw.role.entity.RolePrivilege.role in com.taxsys.nsfw.role.entity.Role.role看我的密码!MappedBy=“角色”。RolePrivilege.role也是如此!即使我用小写,它也不起作用!我也不知道原因。也许这是一个关于jar或兼容性的问题?谢谢!
发布于 2017-03-22 12:29:29
删除RolePrivilege.java中用于private Role role;的@Id,并报告仍然存在哪些错误
发布于 2017-03-22 13:33:59
您可以在新的类对象中使用@Embeddable注释映射复合键,然后使用@ManyToOne映射将子对象映射到父对象。请参阅Hibernate annotations to map one to one unidirectional association with composite primary key
https://stackoverflow.com/questions/42951378
复制相似问题