title: I get "Cannot add or update a child row: a foreign key constraint fails" for some reason
body: <p>I am trying to implement a rezervation of a table.</p>
  <p>A page has a combo-box of tables and input fields. I choose a table, pick a date, enter a number of customers and press <em>Proceed</em>. Then it sends me to a page with input fields. I input information about a customer and add him to the list. I can also add more customers. If it's enough, I press <em>Reserve</em>.</p>
  <p>It's supposed to show a page telling me that it was successful, but instead I get:</p>
  <pre><code>java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`mydb`.`Upis`, CONSTRAINT `fk_Upis_Musterija1` FOREIGN KEY (`idMusterija`) REFERENCES `Musterija` (`idMusterija`))
      at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.29.jar:8.0.29]
      at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.29.jar:8.0.29]
      at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916) ~[mysql-connector-java-8.0.29.jar:8.0.29]
      at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1061) ~[mysql-connector-java-8.0.29.jar:8.0.29]
      at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1009) ~[mysql-connector-java-8.0.29.jar:8.0.29]
      at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1320) ~[mysql-connector-java-8.0.29.jar:8.0.29]
      at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:994) ~[mysql-connector-java-8.0.29.jar:8.0.29]
      at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-4.0.3.jar:na]
      at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-4.0.3.jar:na]
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:46) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3375) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3908) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:107) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:721) ~[na:na]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:344) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1407) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:489) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3290) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2425) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:449) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:40) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
      at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:562) ~[spring-orm-5.3.21.jar:5.3.21]
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:743) ~[spring-tx-5.3.21.jar:5.3.21]
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711) ~[spring-tx-5.3.21.jar:5.3.21]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:654) ~[spring-tx-5.3.21.jar:5.3.21]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:407) ~[spring-tx-5.3.21.jar:5.3.21]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.21.jar:5.3.21]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.21.jar:5.3.21]
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.21.jar:5.3.21]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.21.jar:5.3.21]
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174) ~[spring-data-jpa-2.7.1.jar:2.7.1]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.21.jar:5.3.21]
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.21.jar:5.3.21]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.21.jar:5.3.21]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.21.jar:5.3.21]
      at jdk.proxy8/jdk.proxy8.$Proxy170.save(Unknown Source) ~[na:na]
      at com.example.demo.controller.RezervacijaController.rezervisi(RezervacijaController.java:92) ~[classes/:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.21.jar:5.3.21]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.21.jar:5.3.21]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.21.jar:5.3.21]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.21.jar:5.3.21]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.21.jar:5.3.21]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.21.jar:5.3.21]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.21.jar:5.3.21]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.21.jar:5.3.21]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.21.jar:5.3.21]
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.21.jar:5.3.21]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.64.jar:4.0.FR]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.21.jar:5.3.21]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.64.jar:4.0.FR]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.21.jar:5.3.21]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.21.jar:5.3.21]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.21.jar:5.3.21]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.21.jar:5.3.21]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.21.jar:5.3.21]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.21.jar:5.3.21]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1787) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.64.jar:9.0.64]
      at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
  </code></pre>
  <p>SQL scripts and tables:</p>
  <pre><code>CREATE TABLE `Musterija` (
    `idMusterija` int NOT NULL AUTO_INCREMENT,
    `ime` varchar(20) NOT NULL,
    `prezime` varchar(50) NOT NULL,
    `adresa` varchar(100) NOT NULL,
    `email` varchar(200) NOT NULL,
    `brTelefona` varchar(20) NOT NULL,
    PRIMARY KEY (`idMusterija`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
  
  CREATE TABLE `Upis` (
    `idRezervacija` int NOT NULL,
    `idMusterija` int NOT NULL,
    KEY `fk_Upis_Musterija1_idx` (`idMusterija`),
    KEY `fk_Upis_Rezervacija1_idx` (`idRezervacija`),
    CONSTRAINT `fk_Upis_Musterija1` FOREIGN KEY (`idMusterija`) REFERENCES `Musterija` (`idMusterija`),
    CONSTRAINT `fk_Upis_Rezervacija1` FOREIGN KEY (`idRezervacija`) REFERENCES `Rezervacija` (`idRezervacija`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
  
  CREATE TABLE `Rezervacija` (
    `idRezervacija` int NOT NULL AUTO_INCREMENT,
    `brMusterija` int NOT NULL,
    `datumRezervacije` datetime NOT NULL,
    `cenaRezervacije` double NOT NULL,
    `idSto` int NOT NULL,
    `idUser` int NOT NULL,
    PRIMARY KEY (`idRezervacija`),
    KEY `fk_Rezervacija_Sto1_idx` (`idSto`),
    KEY `fk_Rezervacija_User1_idx` (`idUser`),
    CONSTRAINT `fk_Rezervacija_Sto1` FOREIGN KEY (`idSto`) REFERENCES `Sto` (`idSto`),
    CONSTRAINT `fk_Rezervacija_User1` FOREIGN KEY (`idUser`) REFERENCES `User` (`idUser`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/AnVrT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AnVrT.png" alt="tables" /></a></p>
  <p>Rezervacija.java:</p>
  <pre><code>@Entity
  @NamedQuery(name=&quot;Rezervacija.findAll&quot;, query=&quot;SELECT r FROM Rezervacija r&quot;)
  public class Rezervacija implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int idRezervacija;
  
      private int brMusterija;
  
      private double cenaRezervacije;
  
      @Temporal(TemporalType.TIMESTAMP)
      private Date datumRezervacije;
  
      //bi-directional many-to-many association to Musterija
      @ManyToMany
      @JoinTable(
          name=&quot;Upis&quot;
          , joinColumns={
              @JoinColumn(name=&quot;idRezervacija&quot;)
              }
          , inverseJoinColumns={
              @JoinColumn(name=&quot;idMusterija&quot;)
              }
          )
      private List&lt;Musterija&gt; musterijas;
  
      //bi-directional many-to-one association to Sto
      @ManyToOne
      @JoinColumn(name=&quot;idSto&quot;)
      private Sto sto;
  
      //bi-directional many-to-one association to User
      @ManyToOne
      @JoinColumn(name=&quot;idUser&quot;)
      private User user;
  
      //bi-directional many-to-one association to Upis
      @OneToMany(mappedBy=&quot;rezervacija&quot;)
      private List&lt;Upis&gt; upis;
  
      public Rezervacija() {
      }
  
      public int getIdRezervacija() {
          return this.idRezervacija;
      }
  
      public void setIdRezervacija(int idRezervacija) {
          this.idRezervacija = idRezervacija;
      }
  
      public int getBrMusterija() {
          return this.brMusterija;
      }
  
      public void setBrMusterija(int brMusterija) {
          this.brMusterija = brMusterija;
      }
  
      public double getCenaRezervacije() {
          return this.cenaRezervacije;
      }
  
      public void setCenaRezervacije(double cenaRezervacije) {
          this.cenaRezervacije = cenaRezervacije;
      }
  
      public Date getDatumRezervacije() {
          return this.datumRezervacije;
      }
  
      public void setDatumRezervacije(Date datumRezervacije) {
          this.datumRezervacije = datumRezervacije;
      }
  
      public List&lt;Musterija&gt; getMusterijas() {
          return this.musterijas;
      }
  
      public void setMusterijas(List&lt;Musterija&gt; musterijas) {
          this.musterijas = musterijas;
      }
  
      public Sto getSto() {
          return this.sto;
      }
  
      public void setSto(Sto sto) {
          this.sto = sto;
      }
  
      public User getUser() {
          return this.user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      public List&lt;Upis&gt; getUpis() {
          return this.upis;
      }
  
      public void setUpis(List&lt;Upis&gt; upis) {
          this.upis = upis;
      }
  
      public Upis addUpi(Upis upi) {
          getUpis().add(upi);
          upi.setRezervacija(this);
  
          return upi;
      }
  
      public Upis removeUpi(Upis upi) {
          getUpis().remove(upi);
          upi.setRezervacija(null);
  
          return upi;
      }
  
  }
  </code></pre>
  <p>Sto.java:</p>
  <pre><code>@Entity
  @NamedQuery(name=&quot;Sto.findAll&quot;, query=&quot;SELECT s FROM Sto s&quot;)
  public class Sto implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int idSto;
  
      private int brMesta;
  
      //bi-directional many-to-one association to Rezervacija
      @OneToMany(mappedBy=&quot;sto&quot;)
      private List&lt;Rezervacija&gt; rezervacijas;
  
      public Sto() {
      }
  
      public int getIdSto() {
          return this.idSto;
      }
  
      public void setIdSto(int idSto) {
          this.idSto = idSto;
      }
  
      public int getBrMesta() {
          return this.brMesta;
      }
  
      public void setBrMesta(int brMesta) {
          this.brMesta = brMesta;
      }
  
      public List&lt;Rezervacija&gt; getRezervacijas() {
          return this.rezervacijas;
      }
  
      public void setRezervacijas(List&lt;Rezervacija&gt; rezervacijas) {
          this.rezervacijas = rezervacijas;
      }
  
      public Rezervacija addRezervacija(Rezervacija rezervacija) {
          getRezervacijas().add(rezervacija);
          rezervacija.setSto(this);
  
          return rezervacija;
      }
  
      public Rezervacija removeRezervacija(Rezervacija rezervacija) {
          getRezervacijas().remove(rezervacija);
          rezervacija.setSto(null);
  
          return rezervacija;
      }
  
  }
  </code></pre>
  <p>Upis.java:</p>
  <pre><code>@Entity
  @NamedQuery(name=&quot;Upis.findAll&quot;, query=&quot;SELECT u FROM Upis u&quot;)
  public class Upis implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @EmbeddedId
      private UpisPK id;
  
      //bi-directional many-to-one association to Musterija
      @ManyToOne
      @JoinColumn(name=&quot;idMusterija&quot;, insertable = false, updatable = false)
      private Musterija musterija;
  
      //bi-directional many-to-one association to Rezervacija
      @ManyToOne
      @JoinColumn(name=&quot;idRezervacija&quot;, insertable = false, updatable = false)
      private Rezervacija rezervacija;
  
      public Upis() {
      }
  
      public UpisPK getId() {
          return this.id;
      }
  
      public void setId(UpisPK id) {
          this.id = id;
      }
  
      public Musterija getMusterija() {
          return this.musterija;
      }
  
      public void setMusterija(Musterija musterija) {
          this.musterija = musterija;
      }
  
      public Rezervacija getRezervacija() {
          return this.rezervacija;
      }
  
      public void setRezervacija(Rezervacija rezervacija) {
          this.rezervacija = rezervacija;
      }
  
  }
  </code></pre>
  <p>UpisPK.java:</p>
  <pre><code>@Embeddable
  public class UpisPK implements Serializable {
      //default serial version id, required for serializable classes.
      private static final long serialVersionUID = 1L;
  
      @Column(insertable=false, updatable=false)
      private int idRezervacija;
  
      @Column(insertable=false, updatable=false)
      private int idMusterija;
  
      public UpisPK() {
      }
      public int getIdRezervacija() {
          return this.idRezervacija;
      }
      public void setIdRezervacija(int idRezervacija) {
          this.idRezervacija = idRezervacija;
      }
      public int getIdMusterija() {
          return this.idMusterija;
      }
      public void setIdMusterija(int idMusterija) {
          this.idMusterija = idMusterija;
      }
  
      public boolean equals(Object other) {
          if (this == other) {
              return true;
          }
          if (!(other instanceof UpisPK)) {
              return false;
          }
          UpisPK castOther = (UpisPK)other;
          return 
              (this.idRezervacija == castOther.idRezervacija)
              &amp;&amp; (this.idMusterija == castOther.idMusterija);
      }
  
      public int hashCode() {
          final int prime = 31;
          int hash = 17;
          hash = hash * prime + this.idRezervacija;
          hash = hash * prime + this.idMusterija;
          
          return hash;
      }
  }
  </code></pre>
  <p>Musterija.java:</p>
  <pre><code>@Entity
  @NamedQuery(name=&quot;Musterija.findAll&quot;, query=&quot;SELECT m FROM Musterija m&quot;)
  public class Musterija implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int idMusterija;
  
      private String adresa;
  
      private String brTelefona;
  
      private String email;
  
      private String ime;
  
      private String prezime;
  
      //bi-directional many-to-one association to Narudzbina
      @OneToMany(mappedBy=&quot;musterija&quot;)
      private List&lt;Narudzbina&gt; narudzbinas;
  
      //bi-directional many-to-many association to Rezervacija
      @ManyToMany(mappedBy=&quot;musterijas&quot;)
      private List&lt;Rezervacija&gt; rezervacijas;
  
      //bi-directional many-to-one association to Upis
      @OneToMany(mappedBy=&quot;musterija&quot;)
      private List&lt;Upis&gt; upis;
  
      public Musterija() {
      }
  
      public int getIdMusterija() {
          return this.idMusterija;
      }
  
      public void setIdMusterija(int idMusterija) {
          this.idMusterija = idMusterija;
      }
  
      public String getAdresa() {
          return this.adresa;
      }
  
      public void setAdresa(String adresa) {
          this.adresa = adresa;
      }
  
      public String getBrTelefona() {
          return this.brTelefona;
      }
  
      public void setBrTelefona(String brTelefona) {
          this.brTelefona = brTelefona;
      }
  
      public String getEmail() {
          return this.email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getIme() {
          return this.ime;
      }
  
      public void setIme(String ime) {
          this.ime = ime;
      }
  
      public String getPrezime() {
          return this.prezime;
      }
  
      public void setPrezime(String prezime) {
          this.prezime = prezime;
      }
  
      public List&lt;Narudzbina&gt; getNarudzbinas() {
          return this.narudzbinas;
      }
  
      public void setNarudzbinas(List&lt;Narudzbina&gt; narudzbinas) {
          this.narudzbinas = narudzbinas;
      }
  
      public Narudzbina addNarudzbina(Narudzbina narudzbina) {
          getNarudzbinas().add(narudzbina);
          narudzbina.setMusterija(this);
  
          return narudzbina;
      }
  
      public Narudzbina removeNarudzbina(Narudzbina narudzbina) {
          getNarudzbinas().remove(narudzbina);
          narudzbina.setMusterija(null);
  
          return narudzbina;
      }
  
      public List&lt;Rezervacija&gt; getRezervacijas() {
          return this.rezervacijas;
      }
  
      public void setRezervacijas(List&lt;Rezervacija&gt; rezervacijas) {
          this.rezervacijas = rezervacijas;
      }
  
      public List&lt;Upis&gt; getUpis() {
          return this.upis;
      }
  
      public void setUpis(List&lt;Upis&gt; upis) {
          this.upis = upis;
      }
  
      public Upis addUpi(Upis upi) {
          getUpis().add(upi);
          upi.setMusterija(this);
  
          return upi;
      }
  
      public Upis removeUpi(Upis upi) {
          getUpis().remove(upi);
          upi.setMusterija(null);
  
          return upi;
      }
  
  }
  </code></pre>
  <p>RezervacijaController.java:</p>
  <pre><code>@Controller
  @RequestMapping(&quot;/rezervacije&quot;)
  public class RezervacijaController {
  
      @Autowired
      RezervacijaRepository rr;
      @Autowired
      MusterijaRepository mr;
      @Autowired
      StoRepository sr;
      @Autowired
      UpisRepository ur;
  
      @GetMapping(value = &quot;popuniStolove&quot;)
      public String popuniStolove(HttpServletRequest request) {
          List&lt;Sto&gt; stolovi = sr.findAll();
          request.setAttribute(&quot;stolovi&quot;, stolovi);
          return &quot;rezervacijaStola&quot;;
      }
      
      @PostMapping(value = &quot;unesiRezervaciju&quot;)
      public String unesiRezervaciju(Integer brMusterija, Date datum, Sto sto, HttpServletRequest request) {
          Rezervacija r = new Rezervacija();
          r.setBrMusterija(brMusterija);
          r.setCenaRezervacije(800.0 * brMusterija);
          r.setDatumRezervacije(datum);
          sto.addRezervacija(r);
          sr.updateSto(sto);
          
          List&lt;Musterija&gt; musterije = new ArrayList&lt;Musterija&gt;();
          request.getSession().setAttribute(&quot;musterije&quot;, musterije);
          request.getSession().setAttribute(&quot;rezervacija&quot;, r);
          return &quot;unosMusterija&quot;;
      }
      
      @SuppressWarnings(&quot;unchecked&quot;)
      @PostMapping(value = &quot;/dodajMusteriju&quot;)
      public String dodajMusteriju(String ime, String prezime, String adresa, String email, String brTelefona, HttpServletRequest request) {
          List&lt;Musterija&gt; musterije = (ArrayList&lt;Musterija&gt;) request.getSession().getAttribute(&quot;musterije&quot;);
          Musterija m = new Musterija();
          m.setIme(ime);
          m.setPrezime(prezime);
          m.setAdresa(adresa);
          m.setEmail(email);
          m.setBrTelefona(brTelefona);
          musterije.add(m);
          request.setAttribute(&quot;musterije&quot;, musterije);
          return &quot;unosMusterija&quot;;
      }
      
      @SuppressWarnings(&quot;unchecked&quot;)
      @PostMapping(value = &quot;rezervisi&quot;)
      public String rezervisi(HttpServletRequest request) {
          Rezervacija r = (Rezervacija) request.getSession().getAttribute(&quot;rezervacija&quot;);
          List&lt;Musterija&gt; musterije = (ArrayList&lt;Musterija&gt;) request.getSession().getAttribute(&quot;musterije&quot;);
          
          for (Musterija m : musterije) {
              Upis upis = new Upis();
              UpisPK uPK = new UpisPK();
              uPK.setIdMusterija(m.getIdMusterija());
              uPK.setIdRezervacija(r.getIdRezervacija());
              upis.setId(uPK);
              ur.save(upis);
              mr.save(m);
          }
          
          rr.save(r);
          return &quot;uspehRezervacija&quot;;
      }
      
      @InitBinder
      public void initBinder(WebDataBinder binder) {
          SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-MM-dd&quot;);
          sdf.setLenient(true);
          binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
      }
  
  }
  </code></pre>
  <p>Repositories:</p>
  <pre><code>public interface UpisRepository extends JpaRepository&lt;Upis, UpisPK&gt; {
  
  }
  </code></pre>
  <pre><code>@Repository
  @Transactional
  public class StoRepoSpecificImpl implements StoRepoSpecific {
  
      @PersistenceContext
      EntityManager em;
  
      @Override
      public void updateSto(Sto sto) {
          try {
              em.merge(sto);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  <pre><code>public interface StoRepoSpecific {
  
      public void updateSto(Sto sto);
      
  }
  </code></pre>
  <pre><code>public interface StoRepository extends JpaRepository&lt;Sto, Integer&gt;, StoRepoSpecific {
  
  }
  </code></pre>
  <p>You can see my public <a href="https://github.com/IgorArnaut/MIS-Picerija" rel="nofollow noreferrer">project</a> on Github.</p>