title: Hibernate save or update with JoinColumn
body: <p>I would have a problem with create a new entity, if it have a join column. </p>
  
  <p>Entity:</p>
  
  <p>@</p>
  
  <pre><code>Entity
  @Table(name = "projects")
  public class Project {
  
      @Id
      @GeneratedValue(generator="increment")
      @GenericGenerator(name="increment", strategy = "increment")
      @Column(name = "id_project")
      private Long id;
  
      @Column(name = "project_name")
      private String name;
  
      @JsonInclude(value = Include.NON_NULL)
      @Column(name = "project_language")  
      private String language;
  
      @Temporal(TemporalType.DATE)
      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
      @Column(name = "start_date")
      private Date start_date;
  
      @Temporal(TemporalType.DATE)
      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
      @Column(name = "end_date")
      private Date end_date;
  
      @NotFound(action = NotFoundAction.IGNORE)
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name = "id_content_manager")
      private User contentManager;
  </code></pre>
  
  <p>if i send this json:</p>
  
  <pre><code>{
      "name": "prova3",
      "language": "1",
      "start_date": "15-04-2018",
      "end_date": "26-04-2018"
  
  }
  </code></pre>
  
  <p>the result are:</p>
  
  <pre><code>Hibernate: insert into projects (id_content_manager, end_date, project_language, project_name, start_date, id_project) values (?, ?, ?, ?, ?, ?)
  WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1452, SQLState: 23000
  ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot add or update a child row: a foreign key constraint fails (`dipp`.`projects`, CONSTRAINT `projects_user_id_user_fk` FOREIGN KEY (`id_project`) REFERENCES `user` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE)
  ERROR: org.hibernate.internal.ExceptionMapperStandardImpl - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
  </code></pre>
  
  <p>the method for save the entity:</p>
  
  <pre><code>public void newProject(Project project) {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          Transaction tx = null;
  
          tx = session.beginTransaction();
  
          if(session != null) {
              try {
                  session.save(project);
                  tx.commit();
                  session.close();
              }catch(Exception ex) {
                  session.close(); 
              }
          }
      }
  </code></pre>
  
  <p>Someone could help me to solve?
  I would need to understand how to pass the parameter "contentManager" as optional in order to pass only the id and then through a find function to retrieve everything and pass it to contentManager</p>
  
  <p>Edit:</p>
  
  <p>The controller:</p>
  
  <pre><code>@RequestMapping(value = "/", method = RequestMethod.POST, consumes = "application/json" , produces = "application/json")
      public @ResponseBody ResponseEntity&lt;BaseModel&gt; newProject( @RequestHeader(value="Authorization") String token, @RequestBody Project project, Locale locale) throws ParseException{
          if(!SecurityUtil.validateToken(token)) {
              return new ResponseEntity&lt;BaseModel&gt;(new BaseModel("error", MessageHandler.returnMessage("invalid_token", locale)), HttpStatus.UNAUTHORIZED);
          }
          User contentManager = new User();
          logger.info(project.getContentManager().getId().toString());
          if(project.getContentManager().getId() != null) {
              logger.info("have id");
              contentManager = userDao.find(project.getContentManager().getId());
          }
          project.setContentManager(contentManager);
          projectDao.newProject(project);
          return new ResponseEntity&lt;BaseModel&gt;(new BaseModel("ok", project.getId()), HttpStatus.OK);
      }
  </code></pre>