Skip to content

Commit

Permalink
BAEL-2088 Common Hibernate Exceptions
Browse files Browse the repository at this point in the history
Commin Hibernate Exceptions unit tests
  • Loading branch information
chandra1123 committed Jan 12, 2019
1 parent 262d2ef commit dcc1cd3
Show file tree
Hide file tree
Showing 5 changed files with 560 additions and 0 deletions.
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;
}
}
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;
}
}
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;
}
}
Loading

0 comments on commit dcc1cd3

Please sign in to comment.