提高获取和删除字节阵列的速度

发布于 2025-01-26 18:53:53 字数 1809 浏览 1 评论 0原文

有人可以帮我找出我的问题吗? 我有以下实体:

@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;

以及filecontent实体:

@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;

因此,我已经使用字节数组嵌套实体 - 我使用filemetadata在frontend上显示它,当用户想下载文件时 - 我正在从filecontent实体返回他的字节数组。要获取嵌套实体 - 我在存储库中使用以下方法:

@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;
    }

因此,如您所见,我在这里使用Hibernate.Initialize,因为我使用Vaadin,让我只能与这样的实体一起工作,否则我有错误“ Hibernate不能初始化代理”。因此,查询将在18-20秒内完成。我可以提高获取filecontent的速度吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文