diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/HibernateUtil.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/HibernateUtil.java new file mode 100644 index 0000000000..641b80b412 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/HibernateUtil.java @@ -0,0 +1,47 @@ +package com.baeldung.hibernate.exception.persistentobject; + +import java.util.Properties; + +import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; +import org.hibernate.service.ServiceRegistry; + +import com.baeldung.hibernate.exception.persistentobject.entity.Article; +import com.baeldung.hibernate.exception.persistentobject.entity.Author; +import com.baeldung.hibernate.exception.persistentobject.entity.Book; + +public class HibernateUtil { + private static SessionFactory sessionFactory; + + public static SessionFactory getSessionFactory() { + if (sessionFactory == null) { + try { + Configuration configuration = new Configuration(); + Properties settings = new Properties(); + + settings.put(Environment.DRIVER, "org.hsqldb.jdbcDriver"); + settings.put(Environment.URL, "jdbc:hsqldb:mem:userrole"); + settings.put(Environment.USER, "sa"); + settings.put(Environment.PASS, ""); + settings.put(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect"); + settings.put(Environment.SHOW_SQL, "true"); + settings.put(Environment.HBM2DDL_AUTO, "update"); + settings.put(Environment.CHECK_NULLABILITY, "true"); + + configuration.setProperties(settings); + configuration.addAnnotatedClass(Book.class); + configuration.addAnnotatedClass(Author.class); + configuration.addAnnotatedClass(Article.class); + + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() + .applySettings(configuration.getProperties()).build(); + sessionFactory = configuration.buildSessionFactory(serviceRegistry); + } catch (Exception e) { + e.printStackTrace(); + } + } + return sessionFactory; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/entity/Article.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/entity/Article.java new file mode 100644 index 0000000000..3c9c7c5b31 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/entity/Article.java @@ -0,0 +1,29 @@ +package com.baeldung.hibernate.exception.persistentobject.entity; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +@Entity +public class Article { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private String title; + + @ManyToOne(optional = false) + private Author author; + + public Article(String title) { + this.title = title; + } + + public void setAuthor(Author author) { + this.author = author; + } + +} diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/entity/Author.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/entity/Author.java new file mode 100644 index 0000000000..fa6aaa9abe --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/entity/Author.java @@ -0,0 +1,48 @@ +package com.baeldung.hibernate.exception.persistentobject.entity; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; + +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; + +@Entity +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private String name; + + @OneToMany + @Cascade(CascadeType.ALL) + private List
articles; + + public Author(String name) { + this.name = name; + } + + public Author() { + } + + public List
getArticles() { + return articles; + } + + public void setArticles(List
articles) { + this.articles = articles; + } + + public void addArticles(List
articles) { + this.articles = articles; + articles.forEach(article -> article.setAuthor(this)); + } + +} diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/entity/Book.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/entity/Book.java new file mode 100644 index 0000000000..342da27c77 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/entity/Book.java @@ -0,0 +1,34 @@ +package com.baeldung.hibernate.exception.persistentobject.entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; + +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(nullable = false) + private String title; + + public Book(String title) { + this.title = title; + } + + public Book() { + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} diff --git a/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/exception/persistentobject/HibernatePersistentObjectUnitTest.java b/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/exception/persistentobject/HibernatePersistentObjectUnitTest.java new file mode 100644 index 0000000000..09f11b07a2 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/exception/persistentobject/HibernatePersistentObjectUnitTest.java @@ -0,0 +1,65 @@ +package com.baeldung.hibernate.exception.persistentobject; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.hibernate.PropertyValueException; +import org.hibernate.Session; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.hibernate.exception.persistentobject.entity.Article; +import com.baeldung.hibernate.exception.persistentobject.entity.Author; +import com.baeldung.hibernate.exception.persistentobject.entity.Book; + +public class HibernatePersistentObjectUnitTest { + + private static Session session; + + @Before + public void beforeAll() { + session = HibernateUtil.getSessionFactory() + .openSession(); + session.beginTransaction(); + } + + @After + public void afterAll() { + session.close(); + } + + @Test + public void whenSavingEntityWithNullMandatoryField_thenThrowPropertyValueException() { + Book book = new Book(); + + assertThatThrownBy(() -> session.save(book)).isInstanceOf(PropertyValueException.class) + .hasMessageContaining("not-null property references a null or transient value"); + } + + @Test + public void whenSavingEntityWithAllMandatoryField_thenDoNotThrowException() { + Book book = new Book(); + book.setTitle("Clean Code"); + + session.save(book); + } + + @Test + public void whenSavingBidirectionalEntitiesWithoutSettingParent_thenThrowPropertyValueException() { + Author author = new Author("John Doe"); + author.setArticles(asList(new Article("Java Tutorial"), new Article("What's New in JUnit5"))); + + assertThatThrownBy(() -> session.save(author)).isInstanceOf(PropertyValueException.class) + .hasMessageContaining("not-null property references a null or transient value"); + } + + @Test + public void whenSavingBidirectionalEntitiesWithCorrectParent_thenDoNotThrowException() { + Author author = new Author("John Doe"); + author.addArticles(asList(new Article("Java tutorial"), new Article("What's new in JUnit5"))); + + session.save(author); + } + +} \ No newline at end of file