From e6360db343e3362f4ff1d9a84d7bdb6e3b301cf1 Mon Sep 17 00:00:00 2001 From: sasdroid <35628411+sasdroid@users.noreply.github.com> Date: Mon, 12 Mar 2018 18:18:55 +0100 Subject: [PATCH] Add code for article "Mapping LOB Data in Hibernate" (#3778) * Add lob example * Migrate the XML Based Configuration to Java Based Configuration --- .../hibernate/lob/HibernateSessionUtil.java | 61 ++++++++++++++++++ .../baeldung/hibernate/lob/model/User.java | 46 +++++++++++++ .../com/baeldung/hibernate/lob/LobTest.java | 61 ++++++++++++++++++ hibernate5/src/test/resources/profile.png | Bin 0 -> 1117 bytes 4 files changed, 168 insertions(+) create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/lob/HibernateSessionUtil.java create mode 100644 hibernate5/src/main/java/com/baeldung/hibernate/lob/model/User.java create mode 100644 hibernate5/src/test/java/com/baeldung/hibernate/lob/LobTest.java create mode 100644 hibernate5/src/test/resources/profile.png diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/lob/HibernateSessionUtil.java b/hibernate5/src/main/java/com/baeldung/hibernate/lob/HibernateSessionUtil.java new file mode 100644 index 0000000000..dc5242ee7c --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/lob/HibernateSessionUtil.java @@ -0,0 +1,61 @@ +package com.baeldung.hibernate.lob; + +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Properties; + +import org.apache.commons.lang3.StringUtils; +import org.hibernate.SessionFactory; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.service.ServiceRegistry; + +import com.baeldung.hibernate.lob.model.User; + +public class HibernateSessionUtil { + + private static SessionFactory sessionFactory; + private static String PROPERTY_FILE_NAME; + + public static SessionFactory getSessionFactory() throws IOException { + return getSessionFactory(null); + } + + public static SessionFactory getSessionFactory(String propertyFileName) throws IOException { + PROPERTY_FILE_NAME = propertyFileName; + if (sessionFactory == null) { + ServiceRegistry serviceRegistry = configureServiceRegistry(); + sessionFactory = makeSessionFactory(serviceRegistry); + } + return sessionFactory; + } + + private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry) { + MetadataSources metadataSources = new MetadataSources(serviceRegistry); + metadataSources.addAnnotatedClass(User.class); + + Metadata metadata = metadataSources.buildMetadata(); + return metadata.getSessionFactoryBuilder() + .build(); + + } + + private static ServiceRegistry configureServiceRegistry() throws IOException { + Properties properties = getProperties(); + return new StandardServiceRegistryBuilder().applySettings(properties) + .build(); + } + + private static Properties getProperties() throws IOException { + Properties properties = new Properties(); + URL propertiesURL = Thread.currentThread() + .getContextClassLoader() + .getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties")); + try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) { + properties.load(inputStream); + } + return properties; + } +} diff --git a/hibernate5/src/main/java/com/baeldung/hibernate/lob/model/User.java b/hibernate5/src/main/java/com/baeldung/hibernate/lob/model/User.java new file mode 100644 index 0000000000..21f725b388 --- /dev/null +++ b/hibernate5/src/main/java/com/baeldung/hibernate/lob/model/User.java @@ -0,0 +1,46 @@ +package com.baeldung.hibernate.lob.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Lob; +import javax.persistence.Table; + +@Entity +@Table(name="user") +public class User { + + @Id + private String id; + + @Column(name = "name", columnDefinition="VARCHAR(128)") + private String name; + + @Lob + @Column(name = "photo", columnDefinition="BLOB") + private byte[] photo; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public byte[] getPhoto() { + return photo; + } + + public void setPhoto(byte[] photo) { + this.photo = photo; + } +} diff --git a/hibernate5/src/test/java/com/baeldung/hibernate/lob/LobTest.java b/hibernate5/src/test/java/com/baeldung/hibernate/lob/LobTest.java new file mode 100644 index 0000000000..74a94d544b --- /dev/null +++ b/hibernate5/src/test/java/com/baeldung/hibernate/lob/LobTest.java @@ -0,0 +1,61 @@ +package com.baeldung.hibernate.lob; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; + +import org.apache.commons.io.IOUtils; +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.hibernate.lob.model.User; + +public class LobTest { + + private Session session; + + @Before + public void init(){ + try { + session = HibernateSessionUtil.getSessionFactory("hibernate.properties") + .openSession(); + } catch (HibernateException | IOException e) { + fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]"); + } + } + + @After + public void close(){ + if(session != null) session.close(); + } + + @Test + public void givenValidInsertLobObject_whenQueried_returnSameDataAsInserted(){ + User user = new User(); + try(InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("profile.png");) { + // Get Image file from the resource + if(inputStream == null) fail("Unable to get resources"); + user.setId("1"); + user.setName("User"); + user.setPhoto(IOUtils.toByteArray(inputStream)); + + session.persist(user); + } catch (IOException e) { + fail("Unable to read input stream"); + } + + User result = session.find(User.class, "1"); + + assertNotNull("Query result is null", result); + assertEquals("User's name is invalid", user.getName(), result.getName() ); + assertTrue("User's photo is corrupted", Arrays.equals(user.getPhoto(), result.getPhoto()) ); + } +} diff --git a/hibernate5/src/test/resources/profile.png b/hibernate5/src/test/resources/profile.png new file mode 100644 index 0000000000000000000000000000000000000000..1cd4e978b96d0f59a6e48692fcb68d1c6c565a20 GIT binary patch literal 1117 zcmV-j1fu(iP)%yyh@V&OYl(_AFwE`D&8)` zPnvkdvc#Z>pRh2kBHk{=qaGfCG;?<;rVe;D+%LyZS~!K}c+|op81YK@rzPHU@Th|m z4Dk`HcojTY;+}_-W%HGqKH@C!LiH8Tp>1lC@aTqoy zBU~QK<}hs@q|M8($ee`o<9#`B3-PL4Y7d9kW#<;n>t}6VNpYCswLrJTtEa_lFwgN$52-_vxvvfJs@B6-OYs%-@M_JTx*UfozG6(Bhs)>w z!pG%vU$b%f++EGa<#Tu1FZ^c#{;G|y5=kM3Y#X^mtSrFA_-o-vX7e_$5-udx(>S4U)i|4)(UJ~zR(vwE+6VTAzoo7wIS|c zh*yt`3wK%<+yX~Z9G1z2GQgf3~qx jc9u$WjpqGof00000NkvXXu0mjfJX<<= literal 0 HcmV?d00001