Skip to content

Commit e6360db

Browse files
sasdroidpivovarit
authored andcommitted
Add code for article "Mapping LOB Data in Hibernate" (eugenp#3778)
* Add lob example * Migrate the XML Based Configuration to Java Based Configuration
1 parent 6a9ad8c commit e6360db

4 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.hibernate.lob;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
import java.util.Properties;
7+
8+
import org.apache.commons.lang3.StringUtils;
9+
import org.hibernate.SessionFactory;
10+
import org.hibernate.boot.Metadata;
11+
import org.hibernate.boot.MetadataSources;
12+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
13+
import org.hibernate.service.ServiceRegistry;
14+
15+
import com.baeldung.hibernate.lob.model.User;
16+
17+
public class HibernateSessionUtil {
18+
19+
private static SessionFactory sessionFactory;
20+
private static String PROPERTY_FILE_NAME;
21+
22+
public static SessionFactory getSessionFactory() throws IOException {
23+
return getSessionFactory(null);
24+
}
25+
26+
public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
27+
PROPERTY_FILE_NAME = propertyFileName;
28+
if (sessionFactory == null) {
29+
ServiceRegistry serviceRegistry = configureServiceRegistry();
30+
sessionFactory = makeSessionFactory(serviceRegistry);
31+
}
32+
return sessionFactory;
33+
}
34+
35+
private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry) {
36+
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
37+
metadataSources.addAnnotatedClass(User.class);
38+
39+
Metadata metadata = metadataSources.buildMetadata();
40+
return metadata.getSessionFactoryBuilder()
41+
.build();
42+
43+
}
44+
45+
private static ServiceRegistry configureServiceRegistry() throws IOException {
46+
Properties properties = getProperties();
47+
return new StandardServiceRegistryBuilder().applySettings(properties)
48+
.build();
49+
}
50+
51+
private static Properties getProperties() throws IOException {
52+
Properties properties = new Properties();
53+
URL propertiesURL = Thread.currentThread()
54+
.getContextClassLoader()
55+
.getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties"));
56+
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
57+
properties.load(inputStream);
58+
}
59+
return properties;
60+
}
61+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.hibernate.lob.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.Id;
6+
import javax.persistence.Lob;
7+
import javax.persistence.Table;
8+
9+
@Entity
10+
@Table(name="user")
11+
public class User {
12+
13+
@Id
14+
private String id;
15+
16+
@Column(name = "name", columnDefinition="VARCHAR(128)")
17+
private String name;
18+
19+
@Lob
20+
@Column(name = "photo", columnDefinition="BLOB")
21+
private byte[] photo;
22+
23+
public String getId() {
24+
return id;
25+
}
26+
27+
public void setId(String id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public byte[] getPhoto() {
40+
return photo;
41+
}
42+
43+
public void setPhoto(byte[] photo) {
44+
this.photo = photo;
45+
}
46+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.hibernate.lob;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.fail;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.Arrays;
11+
12+
import org.apache.commons.io.IOUtils;
13+
import org.hibernate.HibernateException;
14+
import org.hibernate.Session;
15+
import org.junit.After;
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
19+
import com.baeldung.hibernate.lob.model.User;
20+
21+
public class LobTest {
22+
23+
private Session session;
24+
25+
@Before
26+
public void init(){
27+
try {
28+
session = HibernateSessionUtil.getSessionFactory("hibernate.properties")
29+
.openSession();
30+
} catch (HibernateException | IOException e) {
31+
fail("Failed to initiate Hibernate Session [Exception:" + e.toString() + "]");
32+
}
33+
}
34+
35+
@After
36+
public void close(){
37+
if(session != null) session.close();
38+
}
39+
40+
@Test
41+
public void givenValidInsertLobObject_whenQueried_returnSameDataAsInserted(){
42+
User user = new User();
43+
try(InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("profile.png");) {
44+
// Get Image file from the resource
45+
if(inputStream == null) fail("Unable to get resources");
46+
user.setId("1");
47+
user.setName("User");
48+
user.setPhoto(IOUtils.toByteArray(inputStream));
49+
50+
session.persist(user);
51+
} catch (IOException e) {
52+
fail("Unable to read input stream");
53+
}
54+
55+
User result = session.find(User.class, "1");
56+
57+
assertNotNull("Query result is null", result);
58+
assertEquals("User's name is invalid", user.getName(), result.getName() );
59+
assertTrue("User's photo is corrupted", Arrays.equals(user.getPhoto(), result.getPhoto()) );
60+
}
61+
}
1.09 KB
Loading

0 commit comments

Comments
 (0)