Hibernate:挑出一个包含不应加载的二进制数据的列
Hibernate : Single out a column containing binary data which shouldn't be loaded
- 我正在开发一个 Spring-MVC 应用程序并使用 Hibernate 作为
带有 PostgreSQL 的 ORM 工具。目前我想保存一些文件
在数据库中。为此,我有一个 class 附件,其中有一个
带有 class 注释的多对一映射。
- 现在在 class 附件中,我还有文件名、上传者姓名等,
我想展示,但不以 EAGER 加载为代价
或者从数据库中获取对象,这会拖拽附件
连同它。是否有任何选项可以排除二进制列
数据,所以它没有被加载。
代码如下:
附件型号:
@Entity
@Table(name = "attachments")
public class Attachment {
@Id
@Column(name="attachid")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "attach_gen")
@SequenceGenerator(name = "attach_gen",sequenceName = "attach_seq")
private int attachid;
@Column(name = "filename")
private String fileName;
@Column(name = "uploaddate")
private Timestamp fileUploadDate;
@Column(name = "attachmentdata")
private byte[] attachment;
@ManyToOne
@JoinColumn(name = "noteid")
private Notes notedata;
public void setNotedata(Notes notedata){this.notedata=notedata;}
public Notes getNotedata(){return notedata;}
//Getters and setters ommitted
}
附件DAOImpl :
@Override
public boolean addAttachment(byte[] bytes, String fileName, int noteid) {
session = this.sessionFactory.getCurrentSession();
try {
Attachment attachment = new Attachment();
attachment.setFileName(fileName);
attachment.setAttachment(bytes);
attachment.setFileUploadDate(new Timestamp(System.currentTimeMillis()));
Notes notes = (Notes)session.get(Notes.class,noteid);
notes.getAttachments().add(attachment);
attachment.setNotedata(notes);
session.save(notes);
session.flush();
return true;
} catch (HibernateException e){
e.printStackTrace();
return false;
}
}
此外,如果我在数据库中保存了一个 PDF,我是否需要对其执行一些额外的操作才能正确获取数据。我的意思是,当我以前保存图像时,我必须在图像数据前加上 "data:image/png;base64," 。我希望不需要这样的事情。你能帮忙的话,我会很高兴。非常感谢。
其中之一
延迟加载字段
为您的字段添加 @Basic(fetch = FetchType.LAZY)
注释。您将需要字节码检测。请参阅 http://java.dzone.com/articles/hibernate-bytecode-enhancement 寻求帮助
使字段成为自己的 class 并使其成为一对一的关系。然后让那个懒惰
你们也许可以与同一个 class 建立一对一的关系?然后让那个懒惰
对于你的第二个问题,你应该将上传文件的 contentType
保存在你的数据库中,并在使用 response.setHeader('Content-Type', ...);
下载时写回
- 我正在开发一个 Spring-MVC 应用程序并使用 Hibernate 作为
带有 PostgreSQL 的 ORM 工具。目前我想保存一些文件
在数据库中。为此,我有一个 class 附件,其中有一个
带有 class 注释的多对一映射。
- 现在在 class 附件中,我还有文件名、上传者姓名等, 我想展示,但不以 EAGER 加载为代价 或者从数据库中获取对象,这会拖拽附件 连同它。是否有任何选项可以排除二进制列 数据,所以它没有被加载。 代码如下:
附件型号:
@Entity
@Table(name = "attachments")
public class Attachment {
@Id
@Column(name="attachid")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "attach_gen")
@SequenceGenerator(name = "attach_gen",sequenceName = "attach_seq")
private int attachid;
@Column(name = "filename")
private String fileName;
@Column(name = "uploaddate")
private Timestamp fileUploadDate;
@Column(name = "attachmentdata")
private byte[] attachment;
@ManyToOne
@JoinColumn(name = "noteid")
private Notes notedata;
public void setNotedata(Notes notedata){this.notedata=notedata;}
public Notes getNotedata(){return notedata;}
//Getters and setters ommitted
}
附件DAOImpl :
@Override
public boolean addAttachment(byte[] bytes, String fileName, int noteid) {
session = this.sessionFactory.getCurrentSession();
try {
Attachment attachment = new Attachment();
attachment.setFileName(fileName);
attachment.setAttachment(bytes);
attachment.setFileUploadDate(new Timestamp(System.currentTimeMillis()));
Notes notes = (Notes)session.get(Notes.class,noteid);
notes.getAttachments().add(attachment);
attachment.setNotedata(notes);
session.save(notes);
session.flush();
return true;
} catch (HibernateException e){
e.printStackTrace();
return false;
}
}
此外,如果我在数据库中保存了一个 PDF,我是否需要对其执行一些额外的操作才能正确获取数据。我的意思是,当我以前保存图像时,我必须在图像数据前加上 "data:image/png;base64," 。我希望不需要这样的事情。你能帮忙的话,我会很高兴。非常感谢。
其中之一
延迟加载字段
为您的字段添加
@Basic(fetch = FetchType.LAZY)
注释。您将需要字节码检测。请参阅 http://java.dzone.com/articles/hibernate-bytecode-enhancement 寻求帮助使字段成为自己的 class 并使其成为一对一的关系。然后让那个懒惰
你们也许可以与同一个 class 建立一对一的关系?然后让那个懒惰
对于你的第二个问题,你应该将上传文件的 contentType
保存在你的数据库中,并在使用 response.setHeader('Content-Type', ...);