1

I'm trying to use identity Generator class and getting error ,as I'm working with MySQL and using other generator types code works fine but when it comes to generation type "identity" it gives me the error that id (in movie class ) doesn't have default value I tried every possible solution that i can find then but failed . Please check my code

POJO class

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;
    }

}

To save data

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 log

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

as per docs generator type identity can work with default value and in new database default value is always 0 and when i execute this provides the error now the actual question is am I missing something ? can anyone tell me the cause of error and how to fix it? and help is appreciated. Regards and thanks.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
bananas
  • 1,176
  • 2
  • 19
  • 39
  • 1
    Possible duplicate of [Hibernate: "Field 'id' doesn't have a default value"](http://stackoverflow.com/questions/804514/hibernate-field-id-doesnt-have-a-default-value) – Manu Jan 12 '16 at 10:06
  • @manu if i'm working with other strategies type like auto and increment it works fine but when it comes with generation type identity it throws error. so I think you might give it a check. – bananas Jan 12 '16 at 10:09

4 Answers4

1

In you stack, the SQL code generated by hibernate does not generate any id. A solution could be to set you id column in MySQL to "autoincrement", a number will be generated at insertion.

If this does not work, keep autoincrement and use in your mapping:

<generator class="native"/>
Orden
  • 589
  • 3
  • 13
  • native is good but i was checking for type identity ( to learn) and yes increment is working fine . is there any solution for type identity? but thanks for help :) – bananas Jan 12 '16 at 10:13
  • sorry i had no idea how native works and now native is working fine too – bananas Jan 13 '16 at 07:15
1

Create a sequence in the database and define the generator as follows, then it would definitely work:

<generator class="sequence">
        <param name="sequence">sequence_name</param>
</generator>
RKT
  • 174
  • 1
  • 4
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>
ivantxo
  • 26
  • 4
1
  1. Change the id type from int to Integer
  2. Make sure you use the automatic schema generation hibernate.hbm2ddl.auto="update"
  3. If you don't use the automatic schema generation, make sure the Movie table has an id column with AUTO_INCREMENT
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911