附件型号:
@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," 。我希望不需要这样的东西。你能帮忙的话,我会很高兴。非常感谢。
答案 0 :(得分:1)
其中一个
懒惰加载字段
在您的字段中添加@Basic(fetch = FetchType.LAZY)
注释。你需要字节码检测。请参阅http://java.dzone.com/articles/hibernate-bytecode-enhancement获取帮助
将该字段设为自己的类,并使其成为one2one关系。然后做那个懒惰的
您可以与同一个班级建立一个单独的关系吗?然后做那个懒惰的
对于第二个问题,您应该将上传文件的contentType
保存在数据库中,并在使用response.setHeader('Content-Type', ...);
下载时将其写回来