title: hibernate: deleting entity in a one to many relationship
body: <p>I have a following error:</p>
  
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`spindledb`.`section`, CONSTRAINT `FK_ftoru9cp83n512p9is8x3vo53` FOREIGN KEY (`scenario_id`) REFERENCES `scenario` (`scenario_id`))
  </code></pre>
  
  <p>Here are my classes:</p>
  
  <p>Scenario:</p>
  
  <pre><code>@Entity
  @Table(name = "scenario")
  public class Scenario {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "scenario_id")
  private int id;
  
  @Column(name = "title", nullable = false)
  private String title;
  
  @NotNull
  @DateTimeFormat(pattern = "dd/MM/yyyy")
  @Column(name = "creation_date", nullable = false)
  @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
  private LocalDate creationDate;
  
  @ManyToOne
  @LazyCollection(LazyCollectionOption.FALSE)
  @JoinColumn(name = "id", nullable = false)
  private User user;
  
  @OneToMany(mappedBy = "scenario", orphanRemoval = true)
  @LazyCollection(LazyCollectionOption.FALSE)
  private Set&lt;Plot&gt; plotList = new HashSet&lt;Plot&gt;();
  
  @OneToMany(mappedBy = "scenario", orphanRemoval = true)
  @LazyCollection(LazyCollectionOption.FALSE)
  private Set&lt;Character&gt; characterList = new HashSet&lt;Character&gt;();
  
  @OneToMany(mappedBy = "scenario", cascade=CascadeType.ALL, orphanRemoval = true)
  @LazyCollection(LazyCollectionOption.FALSE)
  @OrderBy("sequence ASC")
  private Set&lt;Section&gt; sectionList = new HashSet&lt;Section&gt;();
  </code></pre>
  
  <p>Section:</p>
  
  <pre><code>@Entity
  @Table(name = "section")
  public class Section {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "section_id")
  private int id;
  
  @Size(min = 4, max = 50)
  @NotNull
  @Column(name = "name")
  private String name;
  
  @NotNull
  @Column(name = "type")
  private String type = SectionType.TEXT.getSectionType();
  
  @Column(name = "visibility")
  private boolean visibility;
  
  @NotNull
  @Column(name = "sequence")
  private int sequence;
  
  @ManyToOne (cascade=CascadeType.ALL)
  @LazyCollection(LazyCollectionOption.FALSE)
  @JoinColumn(name = "scenario_id", nullable = false)
  private Scenario scenario;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>    @RequestMapping(value = { "/delete-{id}-scenario" }, method = RequestMethod.GET)
  public String deleteScenario(@PathVariable int id) {
  
      scenarioService.deleteScenarioById(id);
      return "redirect:/home";
  }
  </code></pre>
  
  <p>Scenario service:</p>
  
  <pre><code>@Service("scenarioService")
  @Transactional
  public class ScenarioServiceImpl implements ScenarioService {
  
  @Autowired
  private ScenarioDao dao;
  
  @Override
  public Scenario findById(int id) {
      return dao.findById(id);
  }
  
  @Override
  public void saveScenario(Scenario scenario) {
      dao.saveScenario(scenario);
  
  }
  
  public void updateScenario(Scenario scenario) {
      Scenario entity = dao.findById(scenario.getId());
      if(entity!=null){
          entity.setTitle(scenario.getTitle());
          entity.setCreationDate(scenario.getCreationDate());            
      }
  }
  
  @Override
  public void deleteScenarioById(int id) {
      dao.deleteScenarioById(id);
  
  }
  </code></pre>
  
  <p>Dao</p>
  
  <pre><code>@Repository("scenarioDao")
  public class ScenarioDaoImpl extends AbstractDao&lt;Integer, Scenario&gt; implements ScenarioDao {
  
  @Override
  public Scenario findById(int id) {
      return getByKey(id);
  }
  
  @Override
  public void saveScenario(Scenario scenario) {
      persist(scenario);
  
  }
  
  @Override
  public void deleteScenarioById(int id) {
  
      Query query = getSession().createSQLQuery("delete from scenario where id = :id");
      query.setString("id", ""+id);
      query.executeUpdate();
  }
  </code></pre>
  
  <p>I understand that the problem is that there may be a Section that can not exist without scenario. Right now however section table in database is empty and I still can't remove Scenario. Thanks for advice</p>