Hibernate映射异常

时间:2012-11-30 16:18:07

标签: java hibernate struts2 struts

我有2个Entity类ParameterGroupBean和GroupLevelBean

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;


@Entity
@Table(name="tbl_ParameterGroups")
public class ParameterGroupBean {


@Id
@GeneratedValue
private int ParameterGroupId;
private String ParameterGroupName;  
private Boolean Status;

@ManyToOne
@JoinColumn(name="LevelId")
private GroupLevelBean level = new GroupLevelBean();

public GroupLevelBean getLevel() {
    return level;
}

public void setLevel(GroupLevelBean level) {
    this.level = level;
}

@Id
@GeneratedValue
public int getParameterGroupId() {
    return ParameterGroupId;
}

public void setParameterGroupId(int parameterGroupId) {
    ParameterGroupId = parameterGroupId;
}

@Column(length=120) 
public String getParameterGroupName() {
    return ParameterGroupName;
}

public void setParameterGroupName(String parameterGroupName) {
    ParameterGroupName = parameterGroupName;
}

public Boolean getStatus() {
    return Status;
}

public void setStatus(Boolean Status) {
    this.Status = Status;
}
}

GroupLevelBean:

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;


@Entity
@Table(name="tbl_GroupLevel")
public class GroupLevelBean {

private int LevelId;
private String LevelName;
@OneToMany(mappedBy = "level")
private Collection<ParameterGroupBean> parameterGroups = new ArrayList<ParameterGroupBean>();


public Collection<ParameterGroupBean> getParameterGroups() {
    return parameterGroups;
}

public void setParameterGroups(Collection<ParameterGroupBean> parameterGroups) {
    this.parameterGroups = parameterGroups;
}


@Id
@GeneratedValue
public int getLevelId() {
    return LevelId;
}
public void setLevelId(int levelId) {
    LevelId = levelId;
}
@Column(length = 30)
public String getLevelName() {
    return LevelName;
}
public void setLevelName(String levelName) {
    LevelName = levelName;
}   
}

GroupLevelBean和ParameterGroupBean之间的关系是一对多。 我尝试创建会话对象时遇到异常。

org.hibernate.MappingException:无法确定:com.vrde.daems.bean.GroupLevelBean的类型,在表:tbl_ParameterGroups,对于列:[org.hibernate.mapping.Column(level)]

谁能告诉我这是什么问题?

1 个答案:

答案 0 :(得分:5)

因为您在上面添加了@Id@GeneratedValue java持久性注释:

@Id
@GeneratedValue
private int ParameterGroupId;

同时在上面:

@Id
@GeneratedValue
public int getParameterGroupId() {
    return ParameterGroupId;
}

足以将注释放在private int ParameterGroupId;

之上