如何在编译Spring3 Hibernate 4时解决Hibernate.createBlob()方法错误

时间:2013-07-24 10:12:19

标签: spring hibernate

在eclipse中编译我的DocumentManager应用程序时遇到问题。它没有得到Hibernate.createBlob(file.getInputStream())方法并且给出了“Hibernate类型的方法createBlob(InputStream)未定义”。我正在使用Spring 3和Hibernate 4与Maven。请给我一些解决方案。代码如下......谢谢

package com.ecom.data.access.controller;

    import java.io.IOException;
    import java.io.OutputStream;
    import java.sql.Blob;
    import java.sql.SQLException;
    import java.util.Map;

    import javax.servlet.http.HttpServletResponse;

    import com.ecom.data.access.dao.DocumentDAO;
    import com.ecom.data.access.model.Document;

    import org.apache.commons.io.IOUtils;
    import org.hibernate.Hibernate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;

    @Controller
    public class DocumentController {

        @Autowired
        private DocumentDAO documentDao;

        @RequestMapping("/index")
        public String index(Map<String, Object> map) {
            try {
                map.put("document", new Document());
                map.put("documentList", documentDao.list());
            }catch(Exception e) {
                e.printStackTrace();
            }

            return "documents";
        }

        @RequestMapping(value = "/save", method = RequestMethod.POST)
        public String save(
                @ModelAttribute("document") Document document,
                @RequestParam("file") MultipartFile file) {


            System.out.println("Name:" + document.getName());
            System.out.println("Desc:" + document.getDescription());
            System.out.println("File:" + file.getName());
            System.out.println("ContentType:" + file.getContentType());

            try {
                Blob blob = Hibernate.createBlob(file.getInputStream());

                document.setFilename(file.getOriginalFilename());
                document.setContent(blob);
                document.setContentType(file.getContentType());
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                documentDao.save(document);
            } catch(Exception e) {
                e.printStackTrace();
            }

            return "redirect:/index.html";
        }

        @RequestMapping("/download/{documentId}")
        public String download(@PathVariable("documentId")
                Integer documentId, HttpServletResponse response) {

            Document doc = documentDao.get(documentId);
            try {
                response.setHeader("Content-Disposition", "inline;filename=\"" +doc.getFilename()+ "\"");
                OutputStream out = response.getOutputStream();
                response.setContentType(doc.getContentType());
                IOUtils.copy(doc.getContent().getBinaryStream(), out);
                out.flush();
                out.close();

            } catch (IOException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }


            return null;
        }

        @RequestMapping("/remove/{documentId}")
        public String remove(@PathVariable("documentId")
                Integer documentId) {

            documentDao.remove(documentId);

            return "redirect:/index.html";
        }
    }

2 个答案:

答案 0 :(得分:6)

方法Hibernate.createBlob()已弃用,您可以使用Hibernate.getLobCreator(sessionfactory.getCurrentSession())。createBlob()而不是

答案 1 :(得分:2)

我认为您无法解决该方法,因为它不存在。

在Hibernate 3中,有一个方法几乎就像你正在调用的那样,但它有一个额外的整数参数来指定大小。

该方法仍然存在于Hibernate 4中,但在不同的地方。您可能想要调用Hibernate.getLobCreator方法,然后在其上使用createBlob方法。