我有几个从db动态创建的实体类,但是当我用关系注释它时我有以下错误
mappedBy reference an unknown target entity property: coma.entity.impl.CompetenceCriteriaEntityImpl.competence in coma.entity.impl.CompetenceEntityImpl.competenceCriteriaEntityImpl] with root cause
org.hibernate.AnnotationException:mappedBy引用一个未知的目标实体属性:coma.entity.impl.CompetenceEntityImpl.CompetenceCriteriaEntityImpl中的coma.entity.impl.CompetenceCriteriaEntityImpl.competence
我的实体类如下
package coma.entity.impl;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import coma.entity.CompetenceEntity;
import coma.exception.InvalidIDException;
@Entity
@Table(name = "competence")
public class CompetenceEntityImpl implements CompetenceEntity, Serializable {
@OneToMany(fetch = FetchType.EAGER, mappedBy = "competence", targetEntity = JobProfileCompetenceEntityImpl.class)
public Set<JobProfileCompetenceEntityImpl> jobProfileCompetenceEntityImpl;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "competence",targetEntity = CompetenceCriteriaEntityImpl.class)
public Set<CompetenceCriteriaEntityImpl> competenceCriteriaEntityImpl;
/**
*
*/
/**
* Identifier of competence
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_competence")
private int idCompetence;
public Set<JobProfileCompetenceEntityImpl> getJobProfileCompetenceEntityImpl() {
return jobProfileCompetenceEntityImpl;
}
public void setJobProfileCompetenceEntityImpl(Set<JobProfileCompetenceEntityImpl> jobProfileCompetenceEntityImpl) {
this.jobProfileCompetenceEntityImpl = jobProfileCompetenceEntityImpl;
}
public Set<CompetenceCriteriaEntityImpl> getCompetenceCriteriaEntityImpl() {
return competenceCriteriaEntityImpl;
}
public void setCompetenceCriteriaEntityImpl(Set<CompetenceCriteriaEntityImpl> competenceCriteriaEntityImpl) {
this.competenceCriteriaEntityImpl = competenceCriteriaEntityImpl;
}
public void setIdCompetence(int idCompetence) {
this.idCompetence = idCompetence;
}
/**
* Name of competence
*/
@Column(name = "name_competence")
private String nameCompetence;
/**
* Identifier category competence
*/
@Column(name = "id_category_competence_enum")
private int idCategoryCompetenceEnum;
/**
* Constructor without parameters
*/
public CompetenceEntityImpl() {
}
/**
* Constructor with parameters
*
* @param idCompetence
* @param nameCompetence
* @param idCategoryCompetenceEnum
*/
public CompetenceEntityImpl(int idCompetence, String nameCompetence, int idCategoryCompetenceEnum) {
this.idCompetence = idCompetence;
this.nameCompetence = nameCompetence;
this.idCategoryCompetenceEnum = idCategoryCompetenceEnum;
}
/**
* Return identifier of current competence
*
* @return idCompetence
*/
@Override
public int getIdCompetence() {
return idCompetence;
}
/**
* Return name of current competence
*
* @return nameCompetence
*/
@Override
public String getNameCompetence() {
return nameCompetence;
}
/**
* Set name of current competence
*
* @param nameCompetence
*/
@Override
public void setNameCompetence(String nameCompetence) {
this.nameCompetence = nameCompetence;
}
/**
* Return identifier category competence
*
* @return idCategoryCompetence
*/
@Override
public int getIdCategoryCompetenceEnum() {
return idCategoryCompetenceEnum;
}
/**
* Set identifier category competence
*
* @param idCategoryCompetenceEnum
*/
@Override
public void setIdCategoryCompetenceEnum(int idCategoryCompetenceEnum) throws InvalidIDException {
if (idCategoryCompetenceEnum < 0) {
throw new InvalidIDException("The idCategoryCompetenceEnum must be greater than 0");
} else {
this.idCategoryCompetenceEnum = idCategoryCompetenceEnum;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + idCategoryCompetenceEnum;
result = prime * result + idCompetence;
result = prime * result + ((nameCompetence == null) ? 0 : nameCompetence.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;
CompetenceEntityImpl other = (CompetenceEntityImpl) obj;
if (idCategoryCompetenceEnum != other.idCategoryCompetenceEnum)
return false;
if (idCompetence != other.idCompetence)
return false;
if (nameCompetence == null) {
if (other.nameCompetence != null)
return false;
} else if (!nameCompetence.equals(other.nameCompetence))
return false;
return true;
}
@Override
public String toString() {
return "CompetenceEntityImpl [idCompetence=" + idCompetence + ", nameCompetence=" + nameCompetence
+ ", idCategoryCompetenceEnum=" + idCategoryCompetenceEnum + "]";
}
}
和
package coma.entity.impl;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import coma.entity.CompetenceCriteriaEntity;
import coma.exception.InvalidIDException;
@Entity
@Table(name = "competence_criteria")
public class CompetenceCriteriaEntityImpl implements CompetenceCriteriaEntity, Serializable {
/**
*
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_competence_criteria")
private int idCompetenceCriteria;
@ManyToOne(fetch = FetchType.EAGER, targetEntity = CompetenceEntityImpl.class)
@JoinColumn(name = "id_competence")
CompetenceEntityImpl competenceEntityImpl;
@ManyToOne(fetch = FetchType.EAGER, targetEntity = CriteriaEntityImpl.class)
@JoinColumn(name = "id_criteria")
CriteriaEntityImpl criteriaEntityImpl;
@Column(name = "id_criteria", insertable = false, updatable = false)
private int idCriteria;
@Column(name = "id_competence", insertable = false, updatable = false)
private int idCompetence;
public CompetenceEntityImpl getCompetenceEntityImpl() {
return competenceEntityImpl;
}
public void setCompetenceEntityImpl(CompetenceEntityImpl competenceEntityImpl) {
this.competenceEntityImpl = competenceEntityImpl;
}
public CriteriaEntityImpl getCriteriaEntityImpl() {
return criteriaEntityImpl;
}
public void setCriteriaEntityImpl(CriteriaEntityImpl criteriaEntityImpl) {
this.criteriaEntityImpl = criteriaEntityImpl;
}
public CompetenceCriteriaEntityImpl() {
}
public CompetenceCriteriaEntityImpl(int idCompetenceCriteria, int idCompetence, int idCriteria) {
this.idCompetenceCriteria = idCompetenceCriteria;
this.idCompetence = idCompetence;
this.idCriteria = idCriteria;
}
@Override
public int getIdCompetenceCriteria() {
return idCompetenceCriteria;
}
@Override
public void setIdCompetenceCriteria(int idCompetenceCriteria) {
this.idCompetenceCriteria = idCompetenceCriteria;
}
@Override
public int getIdCompetence() {
return idCompetence;
}
@Override
public void setIdCompetence(int idCompetence) throws InvalidIDException {
if (idCompetence < 0) {
throw new InvalidIDException("The idCompetence must be greater than 0");
} else {
this.idCompetence = idCompetence;
}
}
@Override
public int getIdCriteria() {
return idCriteria;
}
@Override
public void setIdCriteria(int idCriteria) throws InvalidIDException {
if (idCriteria < 0) {
throw new InvalidIDException("The idCriteria must be greater than 0");
} else {
this.idCriteria = idCriteria;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + idCompetence;
result = prime * result + idCompetenceCriteria;
result = prime * result + idCriteria;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CompetenceCriteriaEntityImpl other = (CompetenceCriteriaEntityImpl) obj;
if (idCompetence != other.idCompetence)
return false;
if (idCompetenceCriteria != other.idCompetenceCriteria)
return false;
if (idCriteria != other.idCriteria)
return false;
return true;
}
@Override
public String toString() {
return "CompetenceCriteriaEntityImpl [idCompetenceCriteria=" + idCompetenceCriteria + ", idCompetence="
+ idCompetence + ", idCriteria=" + idCriteria + "]";
}
}
我对Hibernate感到非常困惑和绝望......
答案 0 :(得分:0)
正如消息所示,您的&#34; mappedBy &#34;价值是错误的。它应该是BIDIRECTIONAL关系另一侧的FIELD / PROPERTY的名称,它链接回此类。例如CompetenceEntityImpl.competenceCriteriaEntityImpl
声称通过字段CompetenceCriteriaEntityImpl.competence
链接回来,但这并不存在。而 mappedBy 应该是&#34; competenceEntityImpl
&#34;。这可以在任何基本的JPA文档中找到