提高获取和删除字节阵列的速度
有人可以帮我找出我的问题吗? 我有以下实体:
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论