title: Hibernate exception on Joining two tables
body: <p>DB Tables:</p>
  <pre class="lang-sql prettyprint-override"><code>student (
     stud_id int primary key,
     stud_name varchar(10)
  );
      
  product (
     product_id int primary key,
     product_name varchar(10),
     product_price int
  );
      
  user (
     stud_id int,
     product_id int,
     foreign key(product_id) references product(product_id),
     foreign key(stud_id) references student(stud_id)
  );
  </code></pre>
  <p>SQL Query I am running:</p>
  <pre class="lang-sql prettyprint-override"><code>select s.stud_name, result.tot_exp
  from student s
  inner join (
     select u.stud_id sid, sum(product_price) tot_exp
     from product p
     inner join user u on u.product_id = p.product_id
     group by u.stud_id order by tot_exp desc
  ) result on s.stud_id = result.sid
  order by result.tot_exp desc;
  </code></pre>
  <p>Entity Class:</p>
  <pre class="lang-java prettyprint-override"><code>class Product {
          
     @Id
     int product_id;
     String product_name;
     int product_price;
             
     @ManyToOne() //not accepting cascade
     @JoinTable(name=&quot;user&quot;,
        joinColumns= { @JoinColumn(name=&quot;stud_id&quot;) },
        inverseJoinColumns= { @JoinColumn(name = &quot;product_id&quot;) }
     )
     Student s;
     
     // ...
  }
  
  class Student {
      
     @Id
     int stud_id;
     String stud_name;
              
     @OneToMany//(cascade = CascadeType.ALL)
      //      @JoinTable(name=&quot;user&quot;,
      //                 joinColumns= { @JoinColumn(name=&quot;stud_id&quot;) },
      //                 inverseJoinColumns= { @JoinColumn(name = &quot;product_id&quot;) }
      //                )
      //      //Initially join-table was here
      List&lt;Product&gt; products;
  
      // ...
  }
  </code></pre>
  <p>Now my question is if I mention <code>@JoinTable</code> in the <code>Product</code> class then I am getting  exceptions but surprisingly getting correct result while executing select query but getting wrong while inserting new values to Student class.</p>
  <p>And if I mention <code>@JoinTable</code> is the <code>Student</code> class I am not getting any error and getting result as expected. Also no issues if I insert new Student in the table.</p>
  <p>Can anyone elucidate what's actually going on? Is it wrong to declare <code>@JoinTable</code> in the <code>Product</code> class?  Please help.</p>
  <p>Exception:</p>
  <pre><code>  Hibernate: alter table Student_Product drop index UK_f28mdtx7chxxfked98qbllv2j
      Hibernate: alter table Student_Product add constraint UK_f28mdtx7chxxfked98qbllv2j unique (products_product_id)
      Hibernate: alter table Student_Product add constraint FKad7avlyg2mjh9nhnh3u64yacd foreign key (products_product_id) references Product (product_id)
      Hibernate: alter table Student_Product add constraint FKkkdpc75v3wtdvoq2087lt5rjn foreign key (Student_stud_id) references Student (stud_id)
      Apr 28, 2021 11:07:45 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
      WARN: GenerationTarget encountered exception accepting command : Error executing DDL &quot;Apr 28, 2021 11:07:43 PM org.hibernate.Version logVersion
      INFO: HHH000412: Hibernate ORM core version 5.4.27.Final
      Apr 28, 2021 11:07:44 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager &lt;clinit&gt;
      INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
      Apr 28, 2021 11:07:44 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
      WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
      Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
      Apr 28, 2021 11:07:44 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
      INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/fst]
      Apr 28, 2021 11:07:44 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
      INFO: HHH10001001: Connection properties: {password=****, user=root}
      Apr 28, 2021 11:07:44 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
      INFO: HHH10001003: Autocommit mode: false
      Apr 28, 2021 11:07:44 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections &lt;init&gt;
      INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
      Apr 28, 2021 11:07:44 PM org.hibernate.dialect.Dialect &lt;init&gt;
      INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
      Apr 28, 2021 11:07:45 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
      INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@daf22f0] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
      Hibernate: create table Student_Product (Student_stud_id integer not null, products_product_id integer not null) type=MyISAM
      Apr 28, 2021 11:07:45 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
      WARN: GenerationTarget encountered exception accepting command : Error executing DDL &quot;create table Student_Product (Student_stud_id integer not null, products_product_id integer not null) type=MyISAM&quot; via JDBC Statement
      org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL &quot;create table Student_Product (Student_stud_id integer not null, products_product_id integer not null) type=MyISAM&quot; via JDBC Statement
          at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277)
          at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
          at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
          at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
          at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:316)
          at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
          at hibernate.retry.hibernate.App.main(App.java:30)
      Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=MyISAM' at line 1
          at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
          at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
          at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
          at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:764)
          at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648)
          at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
          ... 13 more
      
      Hibernate: alter table Student_Product drop index UK_f28mdtx7chxxfked98qbllv2j
      Hibernate: alter table Student_Product add constraint UK_f28mdtx7chxxfked98qbllv2j unique (products_product_id)
      Hibernate: alter table Student_Product add constraint FKad7avlyg2mjh9nhnh3u64yacd foreign key (products_product_id) references Product (product_id)
      Hibernate: alter table Student_Product add constraint FKkkdpc75v3wtdvoq2087lt5rjn foreign key (Student_stud_id) references Student (stud_id)
      Apr 28, 2021 11:07:45 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
      WARN: GenerationTarget encountered exception accepting command : Error executing DDL &quot;alter table Student_Product add constraint FKad7avlyg2mjh9nhnh3u64yacd foreign key (products_product_id) references Product (product_id)&quot; via JDBC Statement
      org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL &quot;alter table Student_Product add constraint FKad7avlyg2mjh9nhnh3u64yacd foreign key (products_product_id) references Product (product_id)&quot; via JDBC Statement
          at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applyForeignKeys(AbstractSchemaMigrator.java:433)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:249)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
          at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
          at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
          at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:316)
          at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
          at hibernate.retry.hibernate.App.main(App.java:30)
      Caused by: java.sql.SQLSyntaxErrorException: Table 'fst.student_product' doesn't exist
          at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
          at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
          at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
          at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:764)
          at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648)
          at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
          ... 12 more
      
      Apr 28, 2021 11:07:45 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
      WARN: GenerationTarget encountered exception accepting command : Error executing DDL &quot;alter table Student_Product add constraint FKkkdpc75v3wtdvoq2087lt5rjn foreign key (Student_stud_id) references Student (stud_id)&quot; via JDBC Statement
      org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL &quot;alter table Student_Product add constraint FKkkdpc75v3wtdvoq2087lt5rjn foreign key (Student_stud_id) references Student (stud_id)&quot; via JDBC Statement
          at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applyForeignKeys(AbstractSchemaMigrator.java:433)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:249)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
          at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
          at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
          at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:316)
          at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
          at hibernate.retry.hibernate.App.main(App.java:30)
      Caused by: java.sql.SQLSyntaxErrorException: Table 'fst.student_product' doesn't exist
          at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
          at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
          at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
          at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:764)
          at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648)
          at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
          ... 12 more
      
      Hibernate: alter table user add constraint FKhfswe0v9qoihn3j6e8lxgdkj4 foreign key (stud_id) references Product (product_id)
      Apr 28, 2021 11:07:45 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
      WARN: GenerationTarget encountered exception accepting command : Error executing DDL &quot;alter table user add constraint FKhfswe0v9qoihn3j6e8lxgdkj4 foreign key (stud_id) references Product (product_id)&quot; via JDBC Statement
      org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL &quot;alter table user add constraint FKhfswe0v9qoihn3j6e8lxgdkj4 foreign key (stud_id) references Product (product_id)&quot; via JDBC Statement
          at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applyForeignKeys(AbstractSchemaMigrator.java:433)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:249)
          at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
          at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
          at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
          at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:316)
          at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
          at hibernate.retry.hibernate.App.main(App.java:30)
      Caused by: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`fst`.`#sql-1aa8_b7`, CONSTRAINT `FKhfswe0v9qoihn3j6e8lxgdkj4` FOREIGN KEY (`stud_id`) REFERENCES `product` (`product_id`))
          at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)
          at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
          at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
          at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:764)
          at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648)
          at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
          ... 12 more
      
      Hibernate: select s.stud_name,result.tot_exp from student s inner join
      (select u.stud_id sid, SUM(product_price) tot_exp from product p inner join user u on u.product_id=p.product_id group by u.stud_id order by tot_exp desc) result
      on s.stud_id=result.sid order by result.tot_exp desc
      Name :Austin Spending:14000
      Name :Jason Spending:9000
      Name :Kendall Spending:3500
      Name :Ramsey Spending:3000
      Name :Jeremy Spending:1500
  </code></pre>
  <p>Hibernate Config File:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;
          &quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
          &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name=&quot;hibernate.connection.password&quot;&gt;123&lt;/property&gt;
          &lt;property name=&quot;hibernate.connection.url&quot;&gt;jdbc:mysql://localhost:3306/fst&lt;/property&gt;
          &lt;property name=&quot;hibernate.connection.username&quot;&gt;root&lt;/property&gt;
          &lt;property name=&quot;hibernate.default_schema&quot;&gt;fst&lt;/property&gt;
          &lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
          &lt;property name=&quot;hbm2ddl.auto&quot;&gt;update&lt;/property&gt;
          &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;
          
          &lt;property name=&quot;hibernate.current_session_context_class&quot;&gt;thread&lt;/property&gt;
          &lt;mapping class=&quot;hibernate.retry.hibernate.Student&quot;/&gt;
          &lt;mapping class=&quot;hibernate.retry.hibernate.Product&quot;/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>