home
search
help
profile
liking the experience? our app is even better
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now  
showing results for how to save hibernate database to a file
1package com.boraji.tutorial.hibernate;
2
3import java.util.HashMap;
4import java.util.Map;
5
6import org.hibernate.SessionFactory;
7import org.hibernate.boot.Metadata;
8import org.hibernate.boot.MetadataSources;
9import org.hibernate.boot.registry.StandardServiceRegistry;
10import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
11
12import com.boraji.tutorial.hibernate.entity.Product;
13
14/**
15 * @author imssbora
16 */
17public class HibernateUtil {
18  private static StandardServiceRegistry registry;
19  private static SessionFactory sessionFactory;
20
21  public static SessionFactory getSessionFactory() {
22    if (sessionFactory == null) {
23      try {
24        StandardServiceRegistryBuilder registryBuilder = 
25            new StandardServiceRegistryBuilder();
26
27        Map<String, String> settings = new HashMap<>();
28        settings.put("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver");
29        settings.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/BORAJI");
30        settings.put("hibernate.connection.username", "root");
31        settings.put("hibernate.connection.password", "admin");
32        settings.put("hibernate.show_sql", "true");
33        settings.put("hibernate.hbm2ddl.auto", "update");
34
35        registryBuilder.applySettings(settings);
36
37        registry = registryBuilder.build();
38
39        MetadataSources sources = new MetadataSources(registry)
40            .addAnnotatedClass(Product.class);
41
42        Metadata metadata = sources.getMetadataBuilder().build();
43
44        sessionFactory = metadata.getSessionFactoryBuilder().build();
45      } catch (Exception e) {
46        System.out.println("SessionFactory creation failed");
47        if (registry != null) {
48          StandardServiceRegistryBuilder.destroy(registry);
49        }
50      }
51    }
52    return sessionFactory;
53  }
54
55  public static void shutdown() {
56    if (registry != null) {
57      StandardServiceRegistryBuilder.destroy(registry);
58    }
59  }
60}
61
upvote
downvote
source
1package com.boraji.tutorial.hibernate;
2
3import java.awt.image.BufferedImage;
4import java.io.ByteArrayOutputStream;
5import java.io.File;
6import java.io.IOException;
7
8import javax.imageio.ImageIO;
9
10import org.hibernate.Session;
11import org.hibernate.Transaction;
12import org.hibernate.engine.jdbc.BlobProxy;
13
14import com.boraji.tutorial.hibernate.entity.Product;
15
16/**
17 * @author imssbora
18 */
19public class ImageSaveExample {
20   public static void main(String[] args) {
21      Session session = null;
22      Transaction transaction = null;
23      try {
24         session = HibernateUtil.getSessionFactory().openSession();
25         transaction = session.beginTransaction();
26         transaction.begin();
27         
28         Product product=new Product();
29         product.setName("Java - The Complete Reference");
30         session.doWork(conn->{
31            product.setImage(BlobProxy.generateProxy(getImage()));
32         });
33         session.save(product);
34         transaction.commit();
35         
36         System.out.println("Product is saved successfully.");
37         
38      } catch (Exception e) {
39         if (transaction != null) {
40            System.out.println("Transaction is being rolled back.");
41            transaction.rollback();
42         }
43         e.printStackTrace();
44      } finally {
45         if (session != null) {
46            session.close();
47         }
48      }
49
50      HibernateUtil.shutdown();
51   }
52
53   public static byte[] getImage() {
54      File file =new File("Java.png");
55      if(file.exists()){
56         try {
57            BufferedImage bufferedImage=ImageIO.read(file);
58            ByteArrayOutputStream byteOutStream=new ByteArrayOutputStream();
59            ImageIO.write(bufferedImage, "png", byteOutStream);
60            return byteOutStream.toByteArray();
61         } catch (IOException e) {
62            e.printStackTrace();
63         }
64      }
65      return null;
66   }
67}
68
upvote
downvote
source
1package com.boraji.tutorial.hibernate;
2
3import java.awt.image.BufferedImage;
4import java.io.File;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8
9import javax.imageio.ImageIO;
10
11import org.hibernate.Session;
12import org.hibernate.Transaction;
13
14import com.boraji.tutorial.hibernate.entity.Product;
15
16/**
17 * @author imssbora
18 */
19public class ImageLoadExample {
20   public static void main(String[] args) {
21      Session session = null;
22      Transaction transaction = null;
23      try {
24         session = HibernateUtil.getSessionFactory().openSession();
25         transaction = session.getTransaction();
26         transaction.begin();
27
28         Product product = session.get(Product.class, 4L);
29         System.out.println("Product Name: "+product.getName());
30         
31         InputStream imgStream = product.getImage().getBinaryStream();
32         saveImage(imgStream);
33
34         transaction.commit();
35      } catch (Exception e) {
36         if (transaction != null) {
37            transaction.rollback();
38         }
39         e.printStackTrace();
40      } finally {
41         if (session != null) {
42            session.close();
43         }
44      }
45
46      HibernateUtil.shutdown();
47   }
48
49   public static void saveImage(InputStream stream) {
50      File file = new File("output.png");
51      try(FileOutputStream outputStream = new FileOutputStream(file)) {
52         BufferedImage bufferedImage = ImageIO.read(stream);
53         ImageIO.write(bufferedImage, "png", outputStream);
54         System.out.println("Image file location: "+file.getCanonicalPath());
55      } catch (IOException e) {
56         e.printStackTrace();
57      }
58   }
59}
60
upvote
downvote
source