@Entity
@Polymorphism(type = PolymorphismType.EXPLICIT)
@Inheritance(strategy = InheritanceType.JOINED)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "non-lazy")
public abstract class Base extends DatabaseEntity {
private Set<Attachment> attachments = new LinkedHashSet<>();
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@Fetch(FetchMode.SUBSELECT)
public Set<Attachment> getAttachments() {
return attachments;
}
public void setAttachments(Set<Attachment> attachments) {
this.attachments = attachments;
}
}
@Entity
@Cacheable
public class Attachment extends Base {
private Base parent;
@Access(AccessType.FIELD)
@AttributeOverrides(@AttributeOverride(name = "blob", column = @Column(name = "filedata", length = 500 * 1024 * 1024)))
@Basic(fetch = FetchType.LAZY)
private DataHolder data = new DataHolder();
public DataHolder getData() {
if (data == null) {
data = new DataHolder();
}
return data;
}
@ManyToOne(fetch = FetchType.LAZY)
public Base getParent() {
return parent;
}
public void setParent(Base parent) {
this.parent = parent;
}
@Embeddable
public static class DataHolder {
private Blob blob;
@Lob
@Basic(fetch = FetchType.LAZY)
public Blob getBlob() {
return blob;
}
public void setBlob(Blob blob) {
this.blob = blob;
}
}
}