title: Hibernate: How to insert OnetoMany children by cascade
body: <p>I am trying persist a new 'UserTopics' object and map the newly UserTopic in the 'Topic' table corresponded to multiple userId's.</p>
  
  <p>I've no idea what I am doing wrong here. Below is the code I have and the exception.</p>
  
  <p>My UserTopics entity:</p>
  
  <pre><code>@Entity
  @Table(name="USERS_TOPICS")
  public class UserTopics {
  
      @Id
      @GeneratedValue(strategy= GenerationType.IDENTITY)
      @Column(name="TOPICUSER_ID")
      private Integer id;
  
      @Column(name="USER_ID")
      private Integer userId;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "TOPICS_TOPICS_ID")
      private Topics topics;
  
      // Getters and setters
  </code></pre>
  
  <p>and  Topics entity:</p>
  
  <pre><code>    @Entity
      @Table(name="TOPICS")
      public class Topics {
  
          @Id
          @GeneratedValue(strategy= GenerationType.IDENTITY)
          @Column(name="TOPICS_ID")
          private Integer id;
  
          @Column(name="TOPICNAME")
          private String topicName;
  
          @OneToMany(mappedBy = "topics", cascade= {CascadeType.ALL,CascadeType.PERSIST})
          private Set&lt;UserTopics&gt; userTopics;
  
         //Getter and setters
  </code></pre>
  
  <p>In my service Class, I am trying to save the UserTopic like so:</p>
  
  <pre><code> @Service("userTopicsService")
      @Transactional
      public class UserTopicsServiceImpl implements UserTopicsService {
  
  
          @Autowired
          TopicsDao topicsDao;
  
  
          @Override
          public void createTopicc(int UserIdOne, int UserIdTwo) {
          Set&lt;UserTopics&gt; userTopics = new HashSet&lt;&gt;();
  
          Topics topic = new Topics();
          topic.setTopicName(String.valueOf(UserIdOne+UserIdTwo));
  
          UserTopics userTopicOne = new UserTopics();
          userTopicOne.setUserId(UserIdOne);
          userTopics.add(userTopicOne);
  
          UserTopics userTopicTwo = new UserTopics();
          userTopicTwo.setUserId(UserIdTwo);
          userTopics.add(userTopicTwo);
  
          topic.setUserTopics(userTopics);
  
          topicsDao.saveTopic(topic);
  
      }
  
     //Other methods...
  </code></pre>
  
  <p>The exception below</p>
  
  <pre><code>    18:58:54.434 [http-apr-8080-exec-9] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1048, SQLState: 23000
  18:58:54.434 [http-apr-8080-exec-9] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column 'TOPICS_TOPICS_ID' cannot be null
  18:58:54.442 [http-apr-8080-exec-9] DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Warning
  java.sql.SQLWarning: Column 'TOPICS_TOPICS_ID' cannot be null
      at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:779)
      at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:707)
  </code></pre>