Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 23, 2016

generation type identity gives error that id(primary key ) doesn't have default value

generation type identity gives error that id(primary key ) doesn't have default value


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

                                                                                  

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.

Answer by Orden for generation type identity gives error that id(primary key ) doesn't have default value


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:

  

Answer by user5542464 for generation type identity gives error that id(primary key ) doesn't have default value


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

          sequence_name    

Answer by ivantxo for generation type identity gives error that id(primary key ) doesn't have default value


                                                                                  

Answer by Vlad Mihalcea for generation type identity gives error that id(primary key ) doesn't have default value


  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

Answer by Pankaj Pathania for generation type identity gives error that id(primary key ) doesn't have default value


I created new database and everything is working fine now the question is how is that possible thanks for the help coders. :)


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.