From 9271f1094181278deb536dbbf6e01fabd220acfc Mon Sep 17 00:00:00 2001 From: Kumar Chandrakant Date: Sat, 15 Dec 2018 22:40:55 +0530 Subject: [PATCH] Adding files for the tutorial BAEL-1877: A Guide to Hibernate OGM (#5909) * Adding files for the tutorial BAEL-1877: A Guide to Hibernate OGM * Removing test for MongoDB as there may not be MongoDB running on integration platform. * Reapplied the standard Java formatter. * Incorporatd the review comments on the tutorial. * Commented out the integration test for MongoDB. * Removed implicit dependencies from pom.xml --- persistence-modules/hibernate-ogm/README.md | 4 + persistence-modules/hibernate-ogm/pom.xml | 60 ++++++++++++ .../com/baeldung/hibernate/ogm/Article.java | 54 +++++++++++ .../com/baeldung/hibernate/ogm/Author.java | 70 ++++++++++++++ .../com/baeldung/hibernate/ogm/Editor.java | 58 ++++++++++++ .../main/resources/META-INF/persistence.xml | 24 +++++ .../hibernate/ogm/EditorUnitTest.java | 92 +++++++++++++++++++ pom.xml | 2 + 8 files changed, 364 insertions(+) create mode 100644 persistence-modules/hibernate-ogm/README.md create mode 100644 persistence-modules/hibernate-ogm/pom.xml create mode 100644 persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Article.java create mode 100644 persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Author.java create mode 100644 persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Editor.java create mode 100644 persistence-modules/hibernate-ogm/src/main/resources/META-INF/persistence.xml create mode 100644 persistence-modules/hibernate-ogm/src/test/java/com/baeldung/hibernate/ogm/EditorUnitTest.java diff --git a/persistence-modules/hibernate-ogm/README.md b/persistence-modules/hibernate-ogm/README.md new file mode 100644 index 0000000000..f4a5bb6c3b --- /dev/null +++ b/persistence-modules/hibernate-ogm/README.md @@ -0,0 +1,4 @@ +## Relevant articles: + +- [Guide to Hibernate OGM](http://www.baeldung.com/xxxx) + diff --git a/persistence-modules/hibernate-ogm/pom.xml b/persistence-modules/hibernate-ogm/pom.xml new file mode 100644 index 0000000000..2ccac03bd3 --- /dev/null +++ b/persistence-modules/hibernate-ogm/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + com.baeldung + hibernate-ogm + 0.0.1-SNAPSHOT + hibernate-ogm + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + + org.hibernate.ogm + hibernate-ogm-mongodb + 5.4.0.Final + + + + org.hibernate.ogm + hibernate-ogm-neo4j + 5.4.0.Final + + + + org.jboss.narayana.jta + narayana-jta + 5.5.23.Final + + + + junit + junit + 4.12 + test + + + org.easytesting + fest-assert + 1.4 + test + + + + + hibernate-ogm + + + src/main/resources + true + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Article.java b/persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Article.java new file mode 100644 index 0000000000..29f01ecde7 --- /dev/null +++ b/persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Article.java @@ -0,0 +1,54 @@ +package com.baeldung.hibernate.ogm; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +import org.hibernate.annotations.GenericGenerator; + +@Entity +public class Article { + @Id + @GeneratedValue(generator = "uuid") + @GenericGenerator(name = "uuid", strategy = "uuid2") + private String articleId; + + private String articleTitle; + + @ManyToOne + private Author author; + + // constructors, getters and setters... + + Article() { + } + + public Article(String articleTitle) { + this.articleTitle = articleTitle; + } + + public String getArticleId() { + return articleId; + } + + public void setArticleId(String articleId) { + this.articleId = articleId; + } + + public String getArticleTitle() { + return articleTitle; + } + + public void setArticleTitle(String articleTitle) { + this.articleTitle = articleTitle; + } + + public Author getAuthor() { + return author; + } + + public void setAuthor(Author author) { + this.author = author; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Author.java b/persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Author.java new file mode 100644 index 0000000000..9e60c9f934 --- /dev/null +++ b/persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Author.java @@ -0,0 +1,70 @@ +package com.baeldung.hibernate.ogm; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; + +import org.hibernate.annotations.GenericGenerator; + +@Entity +public class Author { + @Id + @GeneratedValue(generator = "uuid") + @GenericGenerator(name = "uuid", strategy = "uuid2") + private String authorId; + + private String authorName; + + @ManyToOne + private Editor editor; + + @OneToMany(mappedBy = "author", cascade = CascadeType.PERSIST) + private Set
authoredArticles = new HashSet<>(); + + // constructors, getters and setters... + + Author() { + } + + public Author(String authorName) { + this.authorName = authorName; + } + + public String getAuthorId() { + return authorId; + } + + public void setAuthorId(String authorId) { + this.authorId = authorId; + } + + public String getAuthorName() { + return authorName; + } + + public void setAuthorName(String authorName) { + this.authorName = authorName; + } + + public Editor getEditor() { + return editor; + } + + public void setEditor(Editor editor) { + this.editor = editor; + } + + public Set
getAuthoredArticles() { + return authoredArticles; + } + + public void setAuthoredArticles(Set
authoredArticles) { + this.authoredArticles = authoredArticles; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Editor.java b/persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Editor.java new file mode 100644 index 0000000000..2c3f720e91 --- /dev/null +++ b/persistence-modules/hibernate-ogm/src/main/java/com/baeldung/hibernate/ogm/Editor.java @@ -0,0 +1,58 @@ +package com.baeldung.hibernate.ogm; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +import org.hibernate.annotations.GenericGenerator; + +@Entity +public class Editor { + @Id + @GeneratedValue(generator = "uuid") + @GenericGenerator(name = "uuid", strategy = "uuid2") + private String editorId; + + private String editorName; + + @OneToMany(mappedBy = "editor", cascade = CascadeType.PERSIST) + private Set assignedAuthors = new HashSet<>(); + + // constructors, getters and setters... + + Editor() { + } + + public Editor(String editorName) { + this.editorName = editorName; + } + + public String getEditorId() { + return editorId; + } + + public void setEditorId(String editorId) { + this.editorId = editorId; + } + + public String getEditorName() { + return editorName; + } + + public void setEditorName(String editorName) { + this.editorName = editorName; + } + + public Set getAssignedAuthors() { + return assignedAuthors; + } + + public void setAssignedAuthors(Set assignedAuthors) { + this.assignedAuthors = assignedAuthors; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-ogm/src/main/resources/META-INF/persistence.xml b/persistence-modules/hibernate-ogm/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..43c86fb55b --- /dev/null +++ b/persistence-modules/hibernate-ogm/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,24 @@ + + + + + org.hibernate.ogm.jpa.HibernateOgmPersistence + + + + + + + + + org.hibernate.ogm.jpa.HibernateOgmPersistence + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate-ogm/src/test/java/com/baeldung/hibernate/ogm/EditorUnitTest.java b/persistence-modules/hibernate-ogm/src/test/java/com/baeldung/hibernate/ogm/EditorUnitTest.java new file mode 100644 index 0000000000..d7fd49d917 --- /dev/null +++ b/persistence-modules/hibernate-ogm/src/test/java/com/baeldung/hibernate/ogm/EditorUnitTest.java @@ -0,0 +1,92 @@ +package com.baeldung.hibernate.ogm; + +import static org.fest.assertions.Assertions.assertThat; + +import java.util.Map; +import java.util.stream.Collectors; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.transaction.TransactionManager; + +import org.junit.Test; + +public class EditorUnitTest { + /* + @Test + public void givenMongoDB_WhenEntitiesCreated_thenCanBeRetrieved() throws Exception { + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ogm-mongodb"); + Editor editor = generateTestData(); + persistTestData(entityManagerFactory, editor); + loadAndVerifyTestData(entityManagerFactory, editor); + } + */ + @Test + public void givenNeo4j_WhenEntitiesCreated_thenCanBeRetrieved() throws Exception { + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ogm-neo4j"); + Editor editor = generateTestData(); + persistTestData(entityManagerFactory, editor); + loadAndVerifyTestData(entityManagerFactory, editor); + } + + private void persistTestData(EntityManagerFactory entityManagerFactory, Editor editor) throws Exception { + TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager(); + EntityManager entityManager; + + transactionManager.begin(); + entityManager = entityManagerFactory.createEntityManager(); + entityManager.persist(editor); + entityManager.close(); + transactionManager.commit(); + } + + private void loadAndVerifyTestData(EntityManagerFactory entityManagerFactory, Editor editor) throws Exception { + TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager(); + EntityManager entityManager; + + transactionManager.begin(); + entityManager = entityManagerFactory.createEntityManager(); + Editor loadedEditor = entityManager.find(Editor.class, editor.getEditorId()); + assertThat(loadedEditor).isNotNull(); + assertThat(loadedEditor.getEditorName()).isEqualTo("Tom"); + assertThat(loadedEditor.getAssignedAuthors()).onProperty("authorName") + .containsOnly("Maria", "Mike"); + Map loadedAuthors = loadedEditor.getAssignedAuthors() + .stream() + .collect(Collectors.toMap(Author::getAuthorName, e -> e)); + assertThat(loadedAuthors.get("Maria") + .getAuthoredArticles()).onProperty("articleTitle") + .containsOnly("Basic of Hibernate OGM"); + assertThat(loadedAuthors.get("Mike") + .getAuthoredArticles()).onProperty("articleTitle") + .containsOnly("Intermediate of Hibernate OGM", "Advanced of Hibernate OGM"); + entityManager.close(); + transactionManager.commit(); + } + + private Editor generateTestData() { + Editor tom = new Editor("Tom"); + Author maria = new Author("Maria"); + Author mike = new Author("Mike"); + Article basic = new Article("Basic of Hibernate OGM"); + Article intermediate = new Article("Intermediate of Hibernate OGM"); + Article advanced = new Article("Advanced of Hibernate OGM"); + maria.getAuthoredArticles() + .add(basic); + basic.setAuthor(maria); + mike.getAuthoredArticles() + .add(intermediate); + intermediate.setAuthor(mike); + mike.getAuthoredArticles() + .add(advanced); + advanced.setAuthor(mike); + tom.getAssignedAuthors() + .add(maria); + maria.setEditor(tom); + tom.getAssignedAuthors() + .add(mike); + mike.setEditor(tom); + return tom; + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index bab3fc2317..f1005c7053 100644 --- a/pom.xml +++ b/pom.xml @@ -509,6 +509,7 @@ persistence-modules/flyway persistence-modules/hbase persistence-modules/hibernate5 + persistence-modules/hibernate-ogm persistence-modules/influxdb persistence-modules/java-cassandra persistence-modules/java-cockroachdb @@ -1220,6 +1221,7 @@ persistence-modules/flyway persistence-modules/hbase persistence-modules/hibernate5 + persistence-modules/hibernate-ogm persistence-modules/influxdb persistence-modules/java-cassandra persistence-modules/java-cockroachdb