title: org.hibernate.exception.ConstraintViolationException: could not execute statement
body: <p>I am getting the below error, when I try to add a app and app settings.
  Here is the detailed error message:</p>
  <pre><code>org.hibernate.exception.ConstraintViolationException: could not
  execute statement   at
  org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)
      at
  org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
      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.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
      at
  org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
      at
  org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
      at
  org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
      at
  org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
      at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492)
      at
  org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)
      at
  org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)
      at
  org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)
      at
  org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:334)
      at
  org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289)
      at
  org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
      at
  org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
      at
  org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
      at
  org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
      at
  org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
      at
  org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
      at
  org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
      at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
      at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)    at
  org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)   at
  com.ips.tvadmindao.services.TVAppService.addTVappsWithSettings(TVAppService.java:237)
      at TVAppserviseTest.testsSaveApps(TVAppserviseTest.java:46)     at
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)     at
  org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at
  org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at
  org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at
  org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at
  org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at
  org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)    at
  org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at
  org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)  at
  org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)     at
  org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)   at
  org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)     at
  org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)    at
  org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at
  org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)    at
  org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at
  org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at
  org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at
  org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
      at
  org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at
  org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  Caused by:
  com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
  Column 'company_id' cannot be null  at
  sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at
  sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at
  sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)     at
  com.mysql.jdbc.Util.getInstance(Util.java:383)  at
  com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)   at
  com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)  at
  com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)  at
  com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)   at
  com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)    at
  com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)     at
  com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
      at
  com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
      at
  com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)
      at
  com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)
      at
  com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147)
      at
  org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
      ... 50 more
  
  
  
  
  
  
  2015-12-21 21:23:15 DEBUG SqlExceptionHelper:139 - could not execute
  statement [n/a]
      com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
  Column 'company_id' cannot be null
          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
          at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
          at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)
          at com.mysql.jdbc.Util.getInstance(Util.java:383)
          at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)
          at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
          at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
          at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
          at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
          at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
          at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
          at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
          at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)
          at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)
          at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147)
          at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
          at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
          at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
          at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
          at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
          at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
          at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492)
          at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)
          at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)
          at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)
          at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:334)
          at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289)
          at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
          at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
          at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
          at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
          at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
          at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
          at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
          at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
          at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
          at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
          at com.ips.tvadmindao.services.TVAppService.addTVappsWithSettings(TVAppService.java:237)
          at TVAppserviseTest.testsSaveApps(TVAppserviseTest.java:46)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:606)
          at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
          at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
          at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
          at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
          at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
          at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
          at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
          at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
          at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
          at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
          at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
          at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
          at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
          at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
          at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
          at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
          at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
          at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
          at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
      2015-12-21 21:23:15 WARN  SqlExceptionHelper:144 - SQL Error: 1048, SQLState: 23000
      2015-12-21 21:23:15 ERROR SqlExceptionHelper:146 - Column 'company_id' cannot be null
  </code></pre>
  <p>Here is the junit test</p>
  <pre><code>@Test
      public void testsSaveApps() {
          List&lt;AppSetting&gt; settings = new ArrayList&lt;AppSetting&gt;();
          //int settingid = types.getMaxSettingId()+1;
          settings.add(createSettingsOne());
          settings.add(createSettingsTwo());
          assertEquals(true,types.addTVappsWithSettings( 1,13, &quot;App1&quot;, settings, &quot;Danka&quot; ) );
          
      }
      
      private static AppSetting createSettingsOne(){
          
          AppSetting setting = new AppSetting();
          setting.setCreated_by(&quot;Kasun&quot;);
          LocalDateTime time = new LocalDateTime();
          setting.setCreated_time(time);
          setting.setDisplayOrder(1);
          setting.setEditable(true);
          setting.setSettings_key(&quot;url&quot;);
          setting.setSettings_value(&quot;www.google.com&quot;);
          setting.setUpdated_time(time);
          setting.setUpdated_by(&quot;Kasun&quot;);
          //setting.setSettings_id(settingid);
          return setting;
      }
      private static AppSetting createSettingsTwo(){
          
          AppSetting setting = new AppSetting();
          setting.setCreated_by(&quot;Kasun&quot;);
          LocalDateTime time = new LocalDateTime();
          setting.setCreated_time(time);
          setting.setDisplayOrder(2);
          setting.setEditable(true);
          setting.setSettings_key(&quot;url2&quot;);
          setting.setSettings_value(&quot;www.CBC.com&quot;);
          setting.setUpdated_time(time);
          setting.setUpdated_by(&quot;Kasun&quot;);
          //setting.setSettings_id(settingid);
          
          return setting;
      }
  </code></pre>
  <p>This is the app and settings saving method</p>
  <pre><code>public boolean addTVappsWithSettings(int appTypeId,int companyid, String appName, List&lt;AppSetting&gt; settings, String createdBy) {
      //set properties of the app to persist
      App app = new App();
  
      //app.setSettings_id(settingid);
  //  app.setSettings_id(settingid);
      app.setApp_id(appTypeId);
      app.setCompany_id(companyid);
      app.setApp_name(appName);
      app.setCreated_by(createdBy);
      LocalDateTime created_time = new LocalDateTime();
      app.setCreated_time(created_time);
      app.setUpdated_by(createdBy);
      app.setUpdated_time(created_time);
      app.setEnable(true);
      app.setapps(settings);
      session.save(app);
      if (!HibernateUtil.INSTANCE.commitTransaction())
          return false;
      return true;
  }
  </code></pre>
  <p>These are DAO classes.</p>
  <pre><code>@Entity
  @Table(name=&quot;company&quot;)
  public class Company {
      @Id  //primary key
      @GeneratedValue(strategy = GenerationType.IDENTITY)//strategy for generating auto generated number
      @Column(name= &quot;company_id&quot;, nullable = false)
      private int company_id; 
      
      @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
      @Fetch(value = FetchMode.SUBSELECT)
      @JoinColumn(name = &quot;company_id&quot;, nullable = false)
      private List&lt;App&gt; threatviewapps = new ArrayList&lt;App&gt;();
      
      @NotEmpty
      @Column(name= &quot;short_name&quot;, nullable = false)
      private String short_name;
      
      @NotEmpty
      @Column(name= &quot;company_description&quot;, nullable = false)
      private String company_description;
      
      @NotEmpty
      @Column(name= &quot;company_name&quot;, nullable = false)
      private String company_name;
      
      @Type(type=&quot;org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime&quot;)
      @Column(name= &quot;created_time&quot;, nullable = false)
      private LocalDateTime created_time;
      
      @Column(name= &quot;created_by&quot;, nullable = false)
      private String created_by;
      
      @Type(type=&quot;org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime&quot;)
      @Column(name= &quot;update_time&quot;, nullable = false)
      private LocalDateTime update_time;
  
      @Column(name= &quot;updated_by&quot;, nullable = false)
      private String updated_by; 
      
      @Column(name= &quot;enable&quot;, nullable = false)
      private boolean enable;
  </code></pre>
  <p>This is the App DAO</p>
  <pre><code>@Entity
  @Table(name = &quot;apps&quot;)
  public class App {
  
      @Id // primary key
      @GeneratedValue(strategy = GenerationType.IDENTITY) 
      @Column(name = &quot;settings_id&quot;, insertable = false, updatable = false)
      private int settings_id;
      
  
      @Column(name = &quot;app_id&quot;, nullable = false)
      private int app_id;
  
  /*  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      @JoinColumn(name = &quot;settings_id&quot;, nullable = false)
      private List&lt;AppSetting&gt; tvsettings = new ArrayList&lt;AppSetting&gt;();*/
  
      @ElementCollection
      @CollectionTable(name=&quot;threatview_app_settings&quot;,joinColumns = @JoinColumn(name = &quot;settings_id&quot;))
      private List&lt;AppSetting&gt; tvsettings = new ArrayList&lt;AppSetting&gt;();
      
      
      @Column(name = &quot;company_id&quot;, insertable = false, updatable = false)
      private int company_id;
  
      @Column(name = &quot;app_name&quot;, nullable = false)
      private String app_name;
  
      @Type(type = &quot;org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime&quot;)
      @Column(name = &quot;created_time&quot;, nullable = false)
      private LocalDateTime created_time;
  
      @Column(name = &quot;created_by&quot;, nullable = false)
      private String created_by;
  
      @Type(type = &quot;org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime&quot;)
      @Column(name = &quot;updated_time&quot;, nullable = false)
      private LocalDateTime updated_time;
  
      @Column(name = &quot;updated_by&quot;, nullable = false)
      private String updated_by;
  
      @Column(name = &quot;enable&quot;, nullable = false)
      private boolean enable;
  </code></pre>
  <p>Why do I get the above error? If you guys need any more information, please let me know.</p>
  <p><strong>Update</strong></p>
  <pre><code>@Entity
  @IdClass(SettingIDKey.class)
  @Table(name = &quot;app_settings&quot;)
  public class AppSetting {
  
  
      @Id
      private int settings_id;
      
      @Id
      private String settings_key;
    
  
      @Column(name = &quot;settings_value&quot;, nullable = false)
      private String settings_value;
  
      @Type(type = &quot;org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime&quot;)
      @Column(name = &quot;created_time&quot;, nullable = false)
      private LocalDateTime created_time;
  
      @Column(name = &quot;created_by&quot;, nullable = false)
      private String created_by;
  
      @Type(type = &quot;org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime&quot;)
      @Column(name = &quot;updated_time&quot;, nullable = false)
      private LocalDateTime updated_time;
  
      @Column(name = &quot;updated_by&quot;, nullable = false)
      private String updated_by;
  
      @Column(name = &quot;editable&quot;, nullable = false)
      private boolean editable;
  
      @Column(name = &quot;enable&quot;, nullable = false)
      private boolean enable;
      
      @Column(name = &quot;display_order&quot;, nullable = false)
      private int displayOrder;
  </code></pre>