插入记录但是没有使用hibernate生成id

时间:2014-02-04 10:33:24

标签: hibernate hibernate-mapping

当我使用hibernate在一个表中插入记录时,它会给出异常,例如 - :

org.hibernate.HibernateException: The database returned no natively generated identity value
at org.hibernate.id.IdentifierGeneratorHelper.getGeneratedIdentity(IdentifierGeneratorHelper.java:90)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:100)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)

我的表结构是,这里我使用了生成类型“auto” - :

 @Entity
@Table(name="OBJECTIVE")
public class Objective implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long objectiveID;
    private String objectiveName;
    private String objectiveDescription;
    private int orderValue;
    private int yearValue;
    private Goal goal;
    private Frequency frequency;
    private Objective  objective;


    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="OBJECTIVE_ID")
    public Long getObjectiveID() {
        return objectiveID;
    }
    public void setObjectiveID(Long objectiveID) {
        this.objectiveID = objectiveID;
    }

    @Column(name="OBJECTIVE_NAME")
    public String getObjectiveName() {
        return objectiveName;
    }

    public void setObjectiveName(String objectiveName) {
        this.objectiveName = objectiveName;
    }

    @Column(name="OBJECTIVE_DESCRIPTION")
    public String getObjectiveDescription() {
        return objectiveDescription;
    }
    public void setObjectiveDescription(String objectiveDescription) {
        this.objectiveDescription = objectiveDescription;
    }

    @Column(name="ORDER_VALUE")
    public int getOrderValue() {
        return orderValue;
    }
    public void setOrderValue(int orderValue) {
        this.orderValue = orderValue;
    }

    @Column(name="YEAR_VALUE")
    public int getYearValue() {
        return yearValue;
    }
    public void setYearValue(int yearValue) {
        this.yearValue = yearValue;
    }


    @ManyToOne
    @JoinColumn(name="GOAL_ID")
    public Goal getGoal() {
        return goal;
    }
    public void setGoal(Goal goal) {
        this.goal = goal;
    }

    @ManyToOne
    @JoinColumn(name="FREQUENCY_ID")
    public Frequency getFrequency() {
        return frequency;
    }
    public void setFrequency(Frequency frequency) {
        this.frequency = frequency;
    }

    @ManyToOne
    @JoinColumn(name="OBJECTIVE_PARENT_ID")
    public Objective getObjective() {
        return objective;
    }
    public void setObjective(Objective objective) {
        this.objective = objective;
    }
}

任何建议都会有所帮助,与其他表一样有效。

1 个答案:

答案 0 :(得分:0)

检查MySQL中的表格语法。

创建语句时必须提供自动增量。例如,看一下我使用自动增量的表:

create table customers(cid int(10) auto_increment, cname varchar(15), email varchar(30), phone int (10), city varchar(10), bal double(10,5), primary key(cid));