Can someone help me please to figure out my problem? I have following entities:
@Entity
@Getter
@Setter
@Table(name = "file_metadata")
@NoArgsConstructor
public class FileMetadata {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotEmpty
private String fileName;
private String contentType;
private boolean isZipped;
private String originalSize;
private String zippedSize;
@JsonIgnore
@ToString.Exclude
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "file_content_id")
private FileContent fileContent;
And the FileContent entity:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FileContent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "file_content_id")
private Integer id;
@Lob
@Type(type = "org.hibernate.type.BinaryType")
private byte[] content;
So, the fact that i have nested entity with byte array - i use FileMetadata to display it on frontend, and when user want to download file - i'm returning him byte array from FileContent Entity. To get nested entity - i use following method in my repository:
@Repository
public interface FileRepository extends JpaRepository<FileMetadata, Integer> {
@Transactional
default FileMetadata get(Integer id) {
FileMetadata fm = this.getById(id);
long start = System.currentTimeMillis();
Hibernate.initialize(fm.getFileContent());
long elapsed = System.currentTimeMillis() - start;
DateFormat df = new SimpleDateFormat("HH 'hours', mm 'mins,' ss 'seconds'");
df.setTimeZone(TimeZone.getTimeZone("GMT+0"));
System.out.println(df.format(new Date(elapsed)));
return fm;
}
So, as you see, i'm Using Hibernate.initialize here, because i'm using Vaadin and it's let me to work with entities only like this, otherwise i have error "Hibernate can't initialize proxy". So that query will be completed in 18-20 seconds. Can i increase speed for fetching FileContent?