title: ON DELETE CASCADE option not in generated when using ddl schema generation on Mysql
body: <p>In a Maven-Spring-Hibernate-MySql running on Tomcat web app I'm using hibernate ddl to generate my DB schema with MySQL5InnoDBDialect.</p>
  
  <p>The schema is generated just fine except the cascade option for foreign-keys. For example I have this structure:</p>
  
  <p>A user object that holds user-details object, both sharing the same key:</p>
  
  <pre><code>@Entity
  @Table(name = "Users")
  public class User implements Serializable {
  
      private static final long serialVersionUID = -359364426541408141L;
  
      /*--- Members ---*/
  
      /**
       * The unique generated ID of the entity.
       */
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "User_Id")
      protected long id;
  
      @Getter
      @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user", optional = true)
      protected UserDetails userDetails;
  
  ...
  
  }
  </code></pre>
  
  <p>And the user-details:</p>
  
  <pre><code>@Entity
  @Table(name = "UserDetails")
  public class UserDetails implements Serializable {
  
      private static final long serialVersionUID = 957231221603878419L;
  
      /*--- Members ---*/
  
      /**
       * Shared Key
       */
      @Id
      @GeneratedValue(generator = "User-Primary-Key")
      @GenericGenerator(name = "User-Primary-Key", strategy = "foreign", parameters = { @Parameter(name = "property", value = "user") })
      @Column(name = "User_Id")
      protected long id;
  
      @Getter
      @Setter
      @OneToOne(optional = false, fetch = FetchType.LAZY)
      @PrimaryKeyJoinColumn
      private User user;
  
  ...
  
  }
  </code></pre>
  
  <p>When generating the schema, the foreign-key from users-details table to users table is missing the cascading.</p>
  
  <p>Here is the schema creation of the user-details:</p>
  
  <pre><code>CREATE TABLE `userdetails` (
    `User_Id` bigint(20) NOT NULL,
    `Creation_Time` bigint(20) NOT NULL,
    `EMail` varchar(128) DEFAULT NULL,
    `Enabled` bit(1) NOT NULL,
    `First_Name` varchar(15) DEFAULT NULL,
    `Last_Name` varchar(25) DEFAULT NULL,
    `Password` varchar(64) NOT NULL,
    `User_Name` varchar(15) NOT NULL,
    PRIMARY KEY (`User_Id`),
    UNIQUE KEY `User_Name` (`User_Name`),
    UNIQUE KEY `EMail` (`EMail`),
    KEY `FKAE447BD7BF9006F5` (`User_Id`),
    CONSTRAINT `FKAE447BD7BF9006F5` FOREIGN KEY (`User_Id`) REFERENCES `users` (`User_Id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
  </code></pre>
  
  <p>As you can see there isn't any "ON DELETE CASCADE" written there in the "FOREIGN KEY" section.</p>
  
  <p>This issue is also described <a href="https://stackoverflow.com/questions/2856460/hibernate-doesnt-generate-cascade">here</a> and <a href="https://forum.hibernate.org/viewtopic.php?t=986157" rel="nofollow noreferrer">here</a> as well.</p>
  
  <p>So I tried adding the @OnDelete annotation above the userDetails member with no luck..</p>
  
  <p>I then created my own dialect overriding the supportsCascadeDelete:</p>
  
  <pre><code>public class MySql5Dialect extends MySQL5InnoDBDialect {
  
      public MySql5Dialect() {
      super();
      }
  
      @Override
      public String getTableTypeString() {
      return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
      }
  
      @Override
      public boolean supportsCascadeDelete() {
      return true;
      }
  
  }
  </code></pre>
  
  <p>But still with no change. My foreign key cascading option still set to "RESTRICT" after generating the schema:</p>
  
  <p><img src="https://i.stack.imgur.com/DtSiK.png" alt="enter image description here"></p>
  
  <p>Is there a way to resolve this issue (non-manually of course)?</p>
  
  <h2>UPDATE</h2>
  
  <p>Following <a href="https://stackoverflow.com/users/213882/angel-villalain">Angel Villalain</a>'s suggestion I put the @OnDelete annotation above the "user" member of the UserDetails class and this did the trick for the OneToOne relation, the delete is cascaded, but the OnUpdate is set to restrict (still), which leads me to my first question - what is the meaning of that? I mean "OnDelete" is pretty straight forward - when I delete the parent delete the child as well, but what is the meaning of the "OnUpdate" option? How does it affect my app when it set to restrict/cascade?</p>
  
  <p>My second question refers to cascading with a OneToMany relation. My User class hols many UserProviders.
  The following code is from the <strong>User</strong> class:</p>
  
  <pre><code>@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
  @JoinTable(name = "Users_Providers", joinColumns = @JoinColumn(name = "User_Id"), inverseJoinColumns = @JoinColumn(name = "Provider_Id"))
  protected Set&lt;UserProvider&gt; userProviders = new HashSet&lt;&gt;(0);
  </code></pre>
  
  <p>And this is the inverse relation, from the <strong>UserProvider</strong> class:</p>
  
  <pre><code>@ManyToOne(fetch = FetchType.LAZY)
  @JoinTable(name = "Users_Providers", joinColumns = @JoinColumn(name = "Provider_Id", insertable = false, updatable = false), inverseJoinColumns = @JoinColumn(name = "User_Id"))
  @OnDelete(action = OnDeleteAction.CASCADE)
  protected User user;
  </code></pre>
  
  <p>So after using the @OnDelete annotation I expected to see the onDelete option with cascade in the join table, but it isn't :(  Have I used it correctly?</p>
  
  <p>Last question - What about unidirectional relation such as @ElementCollection?
  My <strong>UserDetails</strong> class holds an ElementCollection of roles (each user can be assigned with one or more roles):</p>
  
  <pre><code>@ElementCollection(fetch = FetchType.EAGER, targetClass = Role.class)
  @CollectionTable(name = "Users_Roles", joinColumns = @JoinColumn(name = "User_Id", referencedColumnName = "User_Id"))
  @Column(name = "Role")
  protected Set&lt;Role&gt; roles = new HashSet&lt;Enums.Role&gt;(0);
  </code></pre>
  
  <p>A Role is just an enum, not an entity hence I cannot point back from a role to the parent entity. In this case, is there a way to cascade the onDelete?</p>