Add code for article "Mapping LOB Data in Hibernate" (#3778)

* Add lob example

* Migrate the XML Based Configuration to Java Based Configuration
This commit is contained in:
sasdroid 2018-03-12 18:18:55 +01:00 committed by Grzegorz Piwowarek
parent 6a9ad8c1ad
commit e6360db343
4 changed files with 168 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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()) );
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB