title: Which JPA annotation should I use on an AutoPopulatingList?
body: <p>I have an entity called ReferenceForm which contains an AutoPopulatingList of ReferenceItems. It looks like this:</p>
  
  <pre><code>@Entity
  public class ReferenceForm implements Serializable{
  
      private static final long serialVersionUID = -5633788166190438576L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      long id;
  
  
      @lob
      private AutoPopulatingList&lt;ReferenceItem&gt; referenceItems;
  
  }
  </code></pre>
  
  <p>If I add no annotation at all to the AutoPopulatingList, the field type which hibernate creates is varbinary(255). This causes string truncation errors. To work around this, I used the @lob annotation. This felt questionable at the time, but it worked fine. At this point I was just using HSQLDB.</p>
  
  <p>Now the application needs to run against MSSQL. I have generated the schema using Hibernate, and referenceItems ia an image column on the ReferenceForm table. The items themselves are stored in the ReferenceItem table.</p>
  
  <p>Is @lob an appropriate annotation here?.</p>
  
  <p>EDIT: ReferenceItem looks like this:</p>
  
  <pre><code>@Entity
  public class ReferenceItem implements Serializable {
  
  private static final long serialVersionUID = -9077063073733429102L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  long id;
  
  private Title title;
  private String firstName;
  private String surname;
  private String positionHeld;
  private String institutionCompany;
  
  @Embedded
  private Address address;
  @Embedded
  private Telephone telephone;
  
  private String email;
  private boolean existingReference;
  
  private String fileName;
  
  public ReferenceItem() {
  }
  
  ...getters and setters
  }
  </code></pre>
  
  <p>SECOND EDIT: </p>
  
  <p>Thanks to Willome for suggesting using @OneToMany. In the end, this is what worked.</p>
  
  <pre><code>//from
  
  @lob
  private AutoPopulatingList&lt;ReferenceItem&gt; referenceItems;
  //to
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  private List&lt;ReferenceItem&gt; referenceItems = new AutoPopulatingList&lt;ReferenceItem&gt;(ReferenceItem.class);
  </code></pre>
  
  <ul>
  <li>@OneToMany accurately describes the nature of the relationship</li>
  <li>Use the interface (List) instead of the implementation when defining the field. See <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html" rel="nofollow">http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html</a></li>
  <li>Define the CascadeType, otherwise this error appears on saving the entity: <em>org.hibernate.TransientObjectException: object references an unsaved transient instance</em></li>
  <li>Make the FetchType EAGER otherwise you cannot load the form in a different transaction: this error appears: <em>failed to lazily initialize a collection of role: ReferenceForm.referenceItems, could not initialize proxy - no Session</em></li>
  </ul>