Skip to content

Commit 4d90ca6

Browse files
authored
Merge pull request eugenp#6129 from chandra1123/BAEL-2088-HibernateExceptions
Bael 2088 hibernate exceptions
2 parents 599a706 + dcc1cd3 commit 4d90ca6

File tree

5 files changed

+560
-0
lines changed

5 files changed

+560
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.hibernate.exception;
2+
3+
import javax.persistence.Entity;
4+
5+
@Entity
6+
public class EntityWithNoId {
7+
private int id;
8+
9+
public int getId() {
10+
return id;
11+
}
12+
13+
public void setId(int id) {
14+
this.id = id;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.hibernate.exception;
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+
public class HibernateUtil {
16+
private static SessionFactory sessionFactory;
17+
private static String PROPERTY_FILE_NAME;
18+
19+
public static SessionFactory getSessionFactory() throws IOException {
20+
return getSessionFactory(null);
21+
}
22+
23+
public static SessionFactory getSessionFactory(String propertyFileName)
24+
throws IOException {
25+
PROPERTY_FILE_NAME = propertyFileName;
26+
if (sessionFactory == null) {
27+
ServiceRegistry serviceRegistry = configureServiceRegistry();
28+
sessionFactory = makeSessionFactory(serviceRegistry);
29+
}
30+
return sessionFactory;
31+
}
32+
33+
private static SessionFactory makeSessionFactory(
34+
ServiceRegistry serviceRegistry) {
35+
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
36+
metadataSources.addAnnotatedClass(Product.class);
37+
Metadata metadata = metadataSources.getMetadataBuilder()
38+
.build();
39+
return metadata.getSessionFactoryBuilder()
40+
.build();
41+
42+
}
43+
44+
private static ServiceRegistry configureServiceRegistry()
45+
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,
56+
"hibernate-exception.properties"));
57+
try (FileInputStream inputStream = new FileInputStream(
58+
propertiesURL.getFile())) {
59+
properties.load(inputStream);
60+
}
61+
return properties;
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.hibernate.exception;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class Product {
9+
10+
private int id;
11+
12+
private String name;
13+
private String description;
14+
15+
@Id
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
24+
@Column(nullable=false)
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getDescription() {
34+
return description;
35+
}
36+
37+
public void setDescription(String description) {
38+
this.description = description;
39+
}
40+
}

0 commit comments

Comments
 (0)