Example usage for org.hibernate.engine.jdbc LobCreator createBlob

List of usage examples for org.hibernate.engine.jdbc LobCreator createBlob

Introduction

In this page you can find the example usage for org.hibernate.engine.jdbc LobCreator createBlob.

Prototype

public Blob createBlob(byte[] bytes);

Source Link

Document

Create a BLOB reference encapsulating the given byte array.

Usage

From source file:com.mycompany.controllers.ClubController.java

@PostMapping("/create")
@ResponseBody//  ww  w  .jav  a2 s.  c o m
public ModelAndView createClub(@Valid ClubForm clubForm, BindingResult result, Model model) throws IOException {
    if (result.hasErrors()) {
        return new ModelAndView("redirect:/club/create");
    }

    byte[] bytes;
    bytes = clubForm.getLogo().getBytes();
    bytes = LogoConvertion(bytes);

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    Klub club = new Klub();
    club.setNazwa(clubForm.getName());

    LobCreator lcreator = Hibernate.getLobCreator(session);
    Blob blob = (Blob) lcreator.createBlob(bytes);
    club.setLogo(blob);

    session.persist(club);
    t.commit();
    session.close();
    factory.close();

    model.addAttribute("club", club);
    return new ModelAndView("redirect:/club/" + club.getIdKlub());

}

From source file:com.mycompany.controllers.ClubController.java

@PostMapping("/{id}/edit")
@ResponseBody//  w w w .jav a 2  s. c  o m
public ModelAndView editClub(@Valid ClubForm clubForm, BindingResult result, Model model,
        @PathVariable("id") String id) throws IOException {
    if (result.hasErrors()) {
        return new ModelAndView("redirect:/club/" + id + "/edit");
    }

    byte[] bytes;
    bytes = clubForm.getLogo().getBytes();
    bytes = LogoConvertion(bytes);

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    Klub club = session.find(Klub.class, Integer.parseInt(id));
    club.setNazwa(clubForm.getName());

    LobCreator lcreator = Hibernate.getLobCreator(session);
    Blob blob = (Blob) lcreator.createBlob(bytes);
    club.setLogo(blob);

    session.update(club);
    t.commit();
    session.close();
    factory.close();

    model.addAttribute("club", club);
    return new ModelAndView("redirect:/club/" + club.getIdKlub());

}