I am trying to save a byte[]
field to local filesystem instead of the database.
I have tried the JPA annotation @Transient
@Entity
@Table(name = "screenshot")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Screenshot implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@Lob
@Transient
@Column(name = "image", nullable = false)
private byte[] image;
@Column(name = "otherField", nullable = false)
private String otherField;
@Column(name = "otherField2", nullable = false)
private String otherField2;
}
But after I persist the entity, for e.g. The returnedEntity
image
property will not be returned due to @Transient
annotation.
Screenshot returnedEntity = screenshotRepository.save(entity);
But I need to persist it in the database first in-order to get an unique ID and use this ID as part of the file path to persist only the image(binary) field in my local filesystem.
I run into a situation that before I save the entity into the database, I do not have an unique ID. But after I save the entity, I lost the binary byte[] data to save the file in the local filesystem.
Can't seems to figure a good way to link the saved ID to the binary byte[].