title: ManyToMany NOT NULL check constraint when using Named query
body: <p>I have an Entity (<code>Layer</code>) that maps a list of other Entities (<code>Member</code>). This List may have no entries / be <code>null</code>. Yet, when I query for the Entity I get a <code>NOT NULL check constraint</code> error from the database.
  It seems to be connected to the <code>NamedQueries</code> as I can read the Entity from DB if I query by <code>id</code>.</p>
  
  <pre><code>@Entity
  @NamedQueries({
      @NamedQuery(name="getChildLayers",-
                  query = "SELECT la
                           FROM Layer la
                           WHERE la.parent = :parent AND la.deletedDate IS NULL")})
  public class Layer extends CommonModel {
      /*... other field */
      @ManyToOne(fetch = FetchType.LAZY, targetEntity = Layer.class, optional = true)
      private Layer parent;
  
      @ManyToMany(fetch = FetchType.LAZY, targetEntity = MyUser.class)
      private List&lt;MyUser&gt; members;
      public List&lt;MyUser&gt; getMembers() {
          return members;
      }
      public void setMembers(List&lt;MyUser&gt; members) {
          this.members = members;
      }
      /*... other getters and setters */
  }
  </code></pre>
  
  <p>I get this error: <code>integrity constraint violation: NOT NULL check constraint; SYS_CT_10298 table: LAYER_MYUSER column: MEMBERS_ID</code></p>
  
  <p>I am able to create the entry, though.</p>
  
  <p>When I run my tests then all tests fail that read the Entity (but creation works). If I add the following line in the creation method:</p>
  
  <pre><code>  layer.setMembers(new ArrayList&lt;MyUser&gt;());
  </code></pre>
  
  <p>then the methods that test the alternation of the members work (meaning, I can create a <code>Layer</code> and alter its <code>members</code> by adding and removing elements from the list).</p>
  
  <p>It seems to me that reading the Entity from Database fails whenever there are no <code>Member</code> to the <code>Layer</code>.</p>
  
  <p>I did try adding <code>@JoinColumn(nullable=true)</code> to the field, but it changed nothing.</p>
  
  <p>I import <code>javax.persistence</code> classes.</p>
  
  <p>Example as to how I access the variable (in <code>LayerService</code>)</p>
  
  <pre><code>// this method works as expected
  public Layer getById(Long id) {
      Session s = sessionFactory.getCurrentSession();
      return (Layer)s.get(Layer.class, id);
  }
  
  // this does not.
  public List&lt;Layer&gt; getChildren(Layer layer) {
      Query childrenQuery = sessionFactory.getCurrentSession().getNamedQuery("getChildLayers");
      childrenQuery.setParameter("parent", layer);
  
      return (List&lt;Layer&gt;) childrenQuery.list();
  }
  </code></pre>
  
  <hr>
  
  <p>Code changed after Jason Cs answer:</p>
  
  <p><code>Layer</code></p>
  
  <pre><code>...
  private final List&lt;OCWUser&gt; members = new ArrayList&lt;&gt;();
  ...
  public void setMembers(List&lt;OCWUser&gt; members) {
      this.members.clear();
      this.members.addAll(members);
  }
  </code></pre>
  
  <p>Problem still exists.</p>