生成类型标识给出了id(主键)没有默认值的错误

时间:2016-01-12 09:57:25

标签: java mysql database hibernate orm

我尝试使用 identity Generator类并收到错误,因为我正在使用MySQL并使用其他生成器类型代码工作正常但是当涉及到生成类型& #34;身份"它给了我一个错误,即id(在电影类中)没有默认值我尝试了所有可能的解决方案,我可以找到但失败了。 请检查我的代码

POJO课程

public class Movie {

    private int id;
    private String title;
    private String synopsis;
    private String director;

    public Movie(String title, String synopsis, String direcotr) {
        this.title = title;
        this.synopsis = synopsis;
        this.director = direcotr;

    }

    public Movie() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSynopsis() {
        return synopsis;
    }

    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

}

保存数据

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.exception.GenericJDBCException;

import com.java.commons.HibernateUtil;
import com.java.commons.Movie;

public class SavingData {

    public static void saveData() {
        Session session = null;
        try {
            SessionFactory sessionFactory = HibernateUtil.buldSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            System.out.println("openSession()");
            System.out.println("session created");
            Movie movie = new Movie("test", "test", "test");
            session.persist(movie);
            transaction.commit();
            session.close();
            System.out.println("data saved with id : " + movie.getId());
        } catch (GenericJDBCException e) {
            System.out.println("error=======> " + e);
            System.out.println("stack trace======>");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SavingData.saveData();
    }
}

mapping.xml

<hibernate-mapping>
    <class name="com.java.commons.Movie" table="movie" schema="hibernate">
        <id name="id" type="int" column="id">
            <generator class="identity" />
        </id>
        <property name="title" column="tittle" />
        <property name="synopsis" column="synopsis" />
        <property name="director" column="director" />

    </class>
</hibernate-mapping>

错误日志

error=======> org.hibernate.exception.GenericJDBCException: could not execute statement
stack trace======>
org.hibernate.exception.GenericJDBCException: could not execute statement
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:84)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2792)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3362)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:597)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:232)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:213)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:256)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753)
    at com.java.saveData.SavingData.saveData(SavingData.java:24)
    at com.java.saveData.SavingData.main(SavingData.java:37)
Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
    ... 21 more

根据docs生成器类型标识可以使用默认值,并且在新数据库中默认值始终为0,当我执行它时提供错误现在实际问题是我错过了什么? 任何人都可以告诉我错误的原因以及如何解决它? 和帮助表示赞赏。 问候和感谢。

4 个答案:

答案 0 :(得分:1)

在堆栈中,hibernate生成的SQL代码不会生成任何id。一个解决方案可能是将MySQL中的id列设置为“autoincrement”,插入时会生成一个数字。

如果这不起作用,请保持自动增量并在映射中使用:

<generator class="native"/>

答案 1 :(得分:1)

在数据库中创建一个序列并按如下方式定义生成器,然后它肯定会起作用:

<generator class="sequence">
        <param name="sequence">sequence_name</param>
</generator>

答案 2 :(得分:1)

<hibernate-mapping>
    <class name="com.java.commons.Movie" table="movie" schema="hibernate">
        <id name="id" type="int" column="id">
            <generator class="identity"/>
        </id>
        <property name="title" column="tittle" />
        <property name="synopsis" column="synopsis" />
        <property name="director" column="director" />

    </class>
</hibernate-mapping>

答案 3 :(得分:1)

  1. 将ID类型从int更改为Integer
  2. 确保使用自动模式生成hibernate.hbm2ddl.auto="update"
  3. 如果您不使用自动架构生成,请确保Movie表格中包含id列,其中包含AUTO_INCREMENT