Hibernate:挑出一个包含不应加载的二进制数据的列

Hibernate : Single out a column containing binary data which shouldn't be loaded

附件型号:

@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," 。我希望不需要这样的事情。你能帮忙的话,我会很高兴。非常感谢。

其中之一

  1. 延迟加载字段

    为您的字段添加 @Basic(fetch = FetchType.LAZY) 注释。您将需要字节码检测。请参阅 http://java.dzone.com/articles/hibernate-bytecode-enhancement 寻求帮助

  2. 使字段成为自己的 class 并使其成为一对一的关系。然后让那个懒惰

  3. 你们也许可以与同一个 class 建立一对一的关系?然后让那个懒惰

对于你的第二个问题,你应该将上传文件的 contentType 保存在你的数据库中,并在使用 response.setHeader('Content-Type', ...);

下载时写回