BAEL-2394 JPA One-to-One Relationship (#5847)
This commit is contained in:
parent
cac62f06ae
commit
de2f02ff5d
@ -0,0 +1,66 @@
|
|||||||
|
package com.baeldung.hibernate.onetoone;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.customtypes.LocalDateStringType;
|
||||||
|
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 java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class HibernateUtil {
|
||||||
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
private HibernateUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SessionFactory getSessionFactory(Strategy strategy) {
|
||||||
|
if (sessionFactory == null) {
|
||||||
|
sessionFactory = buildSessionFactory(strategy);
|
||||||
|
}
|
||||||
|
return sessionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SessionFactory buildSessionFactory(Strategy strategy) {
|
||||||
|
try {
|
||||||
|
ServiceRegistry serviceRegistry = configureServiceRegistry();
|
||||||
|
|
||||||
|
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||||
|
|
||||||
|
for (Class<?> entityClass : strategy.getEntityClasses()) {
|
||||||
|
metadataSources.addAnnotatedClass(entityClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
Metadata metadata = metadataSources.getMetadataBuilder()
|
||||||
|
.applyBasicType(LocalDateStringType.INSTANCE)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return metadata.getSessionFactoryBuilder()
|
||||||
|
.build();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new ExceptionInInitializerError(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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("hibernate.properties");
|
||||||
|
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
|
||||||
|
properties.load(inputStream);
|
||||||
|
}
|
||||||
|
return properties;
|
||||||
|
}
|
||||||
|
}
|
@ -1,38 +0,0 @@
|
|||||||
package com.baeldung.hibernate.onetoone;
|
|
||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.service.ServiceRegistry;
|
|
||||||
|
|
||||||
public class HibernateUtil {
|
|
||||||
private static SessionFactory sessionFactory;
|
|
||||||
|
|
||||||
private static SessionFactory buildSessionFactory(Strategy strategy) {
|
|
||||||
try {
|
|
||||||
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
|
||||||
Configuration configuration = new Configuration();
|
|
||||||
|
|
||||||
for (Class<?> entityClass : strategy.getEntityClasses()) {
|
|
||||||
configuration.addAnnotatedClass(entityClass);
|
|
||||||
}
|
|
||||||
configuration.configure("one-to-one.cfg.xml");
|
|
||||||
|
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
|
||||||
|
|
||||||
return sessionFactory;
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
throw new ExceptionInInitializerError(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory(Strategy strategy) {
|
|
||||||
if (sessionFactory == null)
|
|
||||||
sessionFactory = buildSessionFactory(strategy);
|
|
||||||
return sessionFactory;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE hibernate-configuration PUBLIC
|
|
||||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
|
||||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
|
||||||
<hibernate-configuration>
|
|
||||||
<session-factory>
|
|
||||||
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
|
|
||||||
<property name="hibernate.connection.password"></property>
|
|
||||||
<property name="hibernate.connection.url">jdbc:h2:mem:spring_hibernate_one_to_one</property>
|
|
||||||
<property name="hibernate.connection.username">sa</property>
|
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
|
|
||||||
<property name="hibernate.current_session_context_class">thread</property>
|
|
||||||
<property name="hibernate.show_sql">true</property>
|
|
||||||
<property name="hibernate.hbm2ddl.auto">create-drop</property>
|
|
||||||
</session-factory>
|
|
||||||
</hibernate-configuration>
|
|
@ -1,16 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE hibernate-configuration PUBLIC
|
|
||||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
|
||||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
|
||||||
<hibernate-configuration>
|
|
||||||
<session-factory>
|
|
||||||
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
|
|
||||||
<property name="hibernate.connection.password"></property>
|
|
||||||
<property name="hibernate.connection.url">jdbc:h2:mem:spring_hibernate_one_to_one</property>
|
|
||||||
<property name="hibernate.connection.username">sa</property>
|
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
|
|
||||||
<property name="hibernate.current_session_context_class">thread</property>
|
|
||||||
<property name="hibernate.show_sql">true</property>
|
|
||||||
<property name="hibernate.hbm2ddl.auto">create-drop</property>
|
|
||||||
</session-factory>
|
|
||||||
</hibernate-configuration>
|
|
Loading…
x
Reference in New Issue
Block a user