forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-2088 Common Hibernate Exceptions
Commin Hibernate Exceptions unit tests
- Loading branch information
1 parent
262d2ef
commit dcc1cd3
Showing
5 changed files
with
560 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...nce-modules/hibernate5/src/main/java/com/baeldung/hibernate/exception/EntityWithNoId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.hibernate.exception; | ||
|
||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class EntityWithNoId { | ||
private int id; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...ence-modules/hibernate5/src/main/java/com/baeldung/hibernate/exception/HibernateUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.baeldung.hibernate.exception; | ||
|
||
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; | ||
|
||
public class HibernateUtil { | ||
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(Product.class); | ||
Metadata metadata = metadataSources.getMetadataBuilder() | ||
.build(); | ||
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-exception.properties")); | ||
try (FileInputStream inputStream = new FileInputStream( | ||
propertiesURL.getFile())) { | ||
properties.load(inputStream); | ||
} | ||
return properties; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/exception/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.baeldung.hibernate.exception; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
public class Product { | ||
|
||
private int id; | ||
|
||
private String name; | ||
private String description; | ||
|
||
@Id | ||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
@Column(nullable=false) | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
} |
Oops, something went wrong.