I have a domain object that contains another domain object; call them A and B. B contains a blob (an image file) which can be large. As long as I am only dealing with one A at a time having B on A isn’t a concern. However, sometimes I’ll be dealing with thousands of A in which carrying around the blob on B causes heap to run out. When I am dealing with so many A I really don’t need B anyway.
Is there some way of telling Hibernate to ignore this property for a particular call? Should I just make B transient and deal with updating/deleting manually in this case?
Right now to get around this problem I use a SQL query to pull all the ids I want then iterate over that list getting each domain object out, doing what I need, then evicting it.
Also, I can’t lazy load B because I’m in a servlet environment so my Hibernate session is closed before I access the properties in most cases.
@Entity
@Table(name="A")
public class A {
private Long id
@OneToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true)
@JoinColumn(name = "a_id", referencedColumnName = "b_id", nullable = true)
@NotFound(action = NotFoundAction.IGNORE)
private B b
...getters and setters
}
@Entity
@Table(name="B")
public class B {
private Long id;
private byte[] blob;
...getters and setters
}
Thanks
One option would be to use lazy property fetching (this requires bytecode instrumentation, refer to the documentation). So you could map B like this:
And, as documented:
Another option would be to use an alternative version of A (A’) without the B, for this special use case.
References