Merge pull request #7257 from amit2103/BAEL-14774
[BAEL-14774] - Updated Hibernate @OneToMany article code
This commit is contained in:
commit
a5c30d84a6
|
@ -4,3 +4,4 @@
|
||||||
- [Persisting Maps with Hibernate](https://www.baeldung.com/hibernate-persisting-maps)
|
- [Persisting Maps with Hibernate](https://www.baeldung.com/hibernate-persisting-maps)
|
||||||
- [Difference Between @Size, @Length, and @Column(length=value)](https://www.baeldung.com/jpa-size-length-column-differences)
|
- [Difference Between @Size, @Length, and @Column(length=value)](https://www.baeldung.com/jpa-size-length-column-differences)
|
||||||
- [Hibernate Validator Specific Constraints](https://www.baeldung.com/hibernate-validator-constraints)
|
- [Hibernate Validator Specific Constraints](https://www.baeldung.com/hibernate-validator-constraints)
|
||||||
|
- [Hibernate One to Many Annotation Tutorial](http://www.baeldung.com/hibernate-one-to-many)
|
|
@ -58,7 +58,7 @@
|
||||||
<finalName>hibernate-mapping</finalName>
|
<finalName>hibernate-mapping</finalName>
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src/test/resources</directory>
|
<directory>src/main/resources</directory>
|
||||||
<filtering>true</filtering>
|
<filtering>true</filtering>
|
||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package com.baeldung.hibernate.oneToMany.config;
|
package com.baeldung.hibernate.oneToMany.config;
|
||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.boot.Metadata;
|
||||||
|
import org.hibernate.boot.MetadataSources;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
|
||||||
public class HibernateAnnotationUtil {
|
public class HibernateAnnotationUtil {
|
||||||
|
@ -12,16 +13,12 @@ public class HibernateAnnotationUtil {
|
||||||
private static SessionFactory buildSessionFactory() {
|
private static SessionFactory buildSessionFactory() {
|
||||||
try {
|
try {
|
||||||
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
||||||
Configuration configuration = new Configuration();
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure("hibernate-annotation.cfg.xml").build();
|
||||||
configuration.configure("hibernate-annotation.cfg.xml");
|
Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
|
||||||
System.out.println("Hibernate Annotation Configuration loaded");
|
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
|
||||||
|
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
|
||||||
System.out.println("Hibernate Annotation serviceRegistry created");
|
|
||||||
|
|
||||||
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
|
||||||
|
|
||||||
return sessionFactory;
|
return sessionFactory;
|
||||||
|
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
System.err.println("Initial SessionFactory creation failed." + ex);
|
System.err.println("Initial SessionFactory creation failed." + ex);
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
|
@ -4,11 +4,11 @@
|
||||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||||
<hibernate-configuration>
|
<hibernate-configuration>
|
||||||
<session-factory>
|
<session-factory>
|
||||||
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
|
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
|
||||||
<property name="hibernate.connection.password">mypassword</property>
|
<property name="hibernate.connection.password"></property>
|
||||||
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring_hibernate_one_to_many?createDatabaseIfNotExist=true</property>
|
<property name="hibernate.connection.url">jdbc:h2:mem:spring_hibernate_one_to_many</property>
|
||||||
<property name="hibernate.connection.username">myuser</property>
|
<property name="hibernate.connection.username">sa</property>
|
||||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
|
||||||
<property name="hbm2ddl.auto">create</property>
|
<property name="hbm2ddl.auto">create</property>
|
||||||
<property name="hibernate.current_session_context_class">thread</property>
|
<property name="hibernate.current_session_context_class">thread</property>
|
||||||
<property name="hibernate.show_sql">true</property>
|
<property name="hibernate.show_sql">true</property>
|
|
@ -10,7 +10,7 @@ import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.dialect.HSQLDialect;
|
import org.hibernate.dialect.H2Dialect;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
|
@ -18,6 +18,7 @@ import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.hibernate.oneToMany.model.Cart;
|
import com.baeldung.hibernate.oneToMany.model.Cart;
|
||||||
import com.baeldung.hibernate.oneToMany.model.Items;
|
import com.baeldung.hibernate.oneToMany.model.Items;
|
||||||
|
|
||||||
|
@ -33,9 +34,9 @@ public class HibernateOneToManyAnnotationMainIntegrationTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeTests() {
|
public static void beforeTests() {
|
||||||
Configuration configuration = new Configuration().addAnnotatedClass(Cart.class).addAnnotatedClass(Items.class)
|
Configuration configuration = new Configuration().addAnnotatedClass(Cart.class).addAnnotatedClass(Items.class)
|
||||||
.setProperty("hibernate.dialect", HSQLDialect.class.getName())
|
.setProperty("hibernate.dialect", H2Dialect.class.getName())
|
||||||
.setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
|
.setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
|
||||||
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
|
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test")
|
||||||
.setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "")
|
.setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "")
|
||||||
.setProperty("hibernate.hbm2ddl.auto", "update");
|
.setProperty("hibernate.hbm2ddl.auto", "update");
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
|
@ -9,7 +9,6 @@
|
||||||
- [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
- [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
||||||
- [Hibernate: save, persist, update, merge, saveOrUpdate](http://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)
|
- [Hibernate: save, persist, update, merge, saveOrUpdate](http://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)
|
||||||
- [Eager/Lazy Loading In Hibernate](http://www.baeldung.com/hibernate-lazy-eager-loading)
|
- [Eager/Lazy Loading In Hibernate](http://www.baeldung.com/hibernate-lazy-eager-loading)
|
||||||
- [Hibernate One to Many Annotation Tutorial](http://www.baeldung.com/hibernate-one-to-many)
|
|
||||||
- [The DAO with Spring and Hibernate](http://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
|
- [The DAO with Spring and Hibernate](http://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
|
||||||
|
|
||||||
### Quick Start
|
### Quick Start
|
||||||
|
|
Loading…
Reference in New Issue