0

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • It's hard to guess, where you lose all that time. Make sure that you a) only load the rows you need (assuming you are showing a grid; so use a backend data provider) and b) only load the relevant data and the file content once the user wants a download. Turn on the logging for queries in your code or the db and measure, what is so costly. – cfrick May 07 '22 at 16:55
  • idk what it's type of magic, but i've deployed app to heroku, and it works fine. that query takes time only on my pc – k0dama1337 May 07 '22 at 17:27
  • Then you have to ramp up your profiling game. We can not guess, what problems your PC is facing... – cfrick May 07 '22 at 17:37
  • I have MacBook Pro M1 16GB RAM Heroku provides me 25MB... Sory tho.. – k0dama1337 May 07 '22 at 18:50
  • Off topic, `Date` and `SimpleDateFormat` for an amount of elapsed time is a horrible misuse. In addition those classes are cumbersome to work with and long outdated, so I definitely recommend you don’t. Use the `Duration` class for an amount of time. It will also calculate the difference for you. To format it see for example [How to convert Milliseconds to "X mins, x seconds" in Java?](https://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java) and/or [this](https://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss). – Ole V.V. May 08 '22 at 08:21

0 Answers0