title: How to setup Spring/HIbernate JPA @Entiry to create a auto incrementing column (none id)
body: <p><strong>Question</strong></p>
  
  <ol>
  <li>I wanted to know if there is a way to setup an <code>@Entity</code> to create a <code>int</code> <code>column</code> which can be automatically incremented each time a new object is created which is not the <code>@Id</code>?</li>
  <li>If it is possible how can I give it an <code>initial value</code>?</li>
  </ol>
  
  <p>I have seen a way to create a separate <code>@Entity</code> and use a <code>@OneToOne</code> mapping but I wanted to know if there is any way to do this directly in the one @Entity </p>
  
  <p>I have read through various similar articles but I am still unable to find a solution.</p>
  
  <p>This particular class is not having its <code>@Id</code> auto generated.</p>
  
  <p><strong>@Entity</strong></p>
  
  <pre><code>private @Id String serial;
  
  @SequenceGenerator(name = "port_gen", sequenceName = "port_gen",  initialValue = 4700)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "port_gen")
  private @JsonIgnore int port;
  </code></pre>
  
  <p>I have tried different <code>GenerationType</code>'s.</p>
  
  <p>Stack Trace:</p>
  
  <pre><code>org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
          at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:314)
          at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
          at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
          at com.sun.proxy.$Proxy123.save(Unknown Source)
          at com.miw.mcb.adbservice.service.DeviceService.registerDevice(DeviceService.java:121)
          at com.miw.mcb.adbservice.service.DeviceService.registerAllDevices(DeviceService.java:168)
          at com.miw.mcb.adbservice.InitialRunner.registerDevices(InitialRunner.java:75)
          at com.miw.mcb.adbservice.InitialRunner.run(InitialRunner.java:37)
          at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:806)
          at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:790)
          at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:777)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
          at com.miw.mcb.adbservice.AdbService.main(AdbService.java:21)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:498)
          at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478)
          at java.lang.Thread.run(Thread.java:745)
  Caused by: org.hibernate.exception.GenericJDBCException: could not execute statement
          at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
          at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
          at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
          at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
          at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
          at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
          at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
          at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
          at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
          at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
          at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
          at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
          at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
          at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
          at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
          at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
          ... 31 more
  Caused by: java.sql.SQLException: Field 'port' 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:208)
          ... 45 more
  </code></pre>