Merge pull request #12037 from etrandafir93/BAEL-4942-hibernate_nullability_check
BAEL-4942: added tests for hibernate nullability check
This commit is contained in:
commit
2d672af774
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Article> articles;
|
||||
|
||||
public Author(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Author() {
|
||||
}
|
||||
|
||||
public List<Article> getArticles() {
|
||||
return articles;
|
||||
}
|
||||
|
||||
public void setArticles(List<Article> articles) {
|
||||
this.articles = articles;
|
||||
}
|
||||
|
||||
public void addArticles(List<Article> articles) {
|
||||
this.articles = articles;
|
||||
articles.forEach(article -> article.setAuthor(this));
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue