HQL查询使用hibernate从数据库替换现有文件

时间:2016-05-07 13:05:20

标签: hibernate hql

我在数据库中有一个现有文件,现在需要用新文件将其替换为数据库。什么是HQL查询删除现有文件并添加新文件?

1 个答案:

答案 0 :(得分:0)

public void addReplacementFile(UploadReplacementFileBean uploadReplacementFileBean) throws Exception {

    SessionFactory sessionFactory = HibernateUtility.createSessionFactory();
    Session session = null;
    session = sessionFactory.openSession();
    session.beginTransaction();


    InputStream input = uploadReplacementFileBean.getFileData().getInputStream();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[10240];
    for (int length = 0; (length = input.read(buffer)) > 0;)
        output.write(buffer, 0, length);


    try {

        output.flush();
        String qs="update UploadFileEntity set fileData=? where  fileId=?";

        Query query=session.createQuery(qs);
        query.setParameter(0, output.toByteArray());
        query.setParameter(1, uploadReplacementFileBean.getFileId());

        query.executeUpdate();
        session.getTransaction().commit();
        session.close();

    }catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();


    }