long time user, first time poster here.
We have an jpa embeddable class we're using to keep track of files. It looks like this plus irrelevant stuff I've stripped out.
@Embeddable
public class Blob {
@Column(name="FILE_ID", length=64)
private String blobFile;
@Column(name="PATH", length=256)
private String path;
@Column(name="SIZE")
private long size;
@Transient
private byte[] data;
... stuff ...
public byte[] getData() { return data; }
}
Initially class Blob contained code for actually reading and writing to the filesystem which I've moved to a Spring component something like this (again simplified) now that other areas of the code base need filesystem access.
interface FileSystem {
public void writeFile(String filepath, byte[] bytes);
public byte[] readFile(String filepath);
}
When byte[] data is set I want to save it to disk when an Entity embedding the Blob class is saved. The Entities which embed this class will be loaded potentially pretty frequently but I rarely need to retrieve the file from disk so I want to:
- Load the file only when requested by code
- Use my spring component to load the file
Callbacks don't work on embedded objects so I can't use them for persisting on save and cleaning up on delete, but I don't want each entity that embed's Blob to need to call a save and cleanup method on it manually in their own callbacks, I'd much rather that the Embeddable was as self contained as possible.
The other issue is actually getting access to the spring bean for the filesystem I've looked around at getting access to a spring bean within a jpa entity or entitylistener class which looks like it's a bit ugly, see http://www.simonellistonball.com/technology/injecting-dependencies-into-a-jpa-entitylistener/ and Injecting spring bean in a JPA Entity Listener.
So after all that rambling, the question is:
- Is there an established pattern to follow to achieve what I'm after?
- Or am I going about this the wrong way and I shouldn't be accessing the filesystem within the entity but instead at a higher level asking the entity for data i.e. getFilePath() and then going after the file there?