I have an entity called ReferenceForm which contains an AutoPopulatingList of ReferenceItems. It looks like this:
@Entity
public class ReferenceForm implements Serializable{
private static final long serialVersionUID = -5633788166190438576L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@lob
private AutoPopulatingList<ReferenceItem> referenceItems;
}
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.
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.
Is @lob an appropriate annotation here?.
EDIT: ReferenceItem looks like this:
@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
}
SECOND EDIT:
Thanks to Willome for suggesting using @OneToMany. In the end, this is what worked.
//from
@lob
private AutoPopulatingList<ReferenceItem> referenceItems;
//to
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<ReferenceItem> referenceItems = new AutoPopulatingList<ReferenceItem>(ReferenceItem.class);
- @OneToMany accurately describes the nature of the relationship
- Use the interface (List) instead of the implementation when defining the field. See http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html
- Define the CascadeType, otherwise this error appears on saving the entity: org.hibernate.TransientObjectException: object references an unsaved transient instance
- Make the FetchType EAGER otherwise you cannot load the form in a different transaction: this error appears: failed to lazily initialize a collection of role: ReferenceForm.referenceItems, could not initialize proxy - no Session