Skip to content

Commit 9271f10

Browse files
kcacademicmaibin
authored andcommitted
Adding files for the tutorial BAEL-1877: A Guide to Hibernate OGM (eugenp#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
1 parent c5a23bc commit 9271f10

8 files changed

Lines changed: 364 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Relevant articles:
2+
3+
- [Guide to Hibernate OGM](http://www.baeldung.com/xxxx)
4+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>hibernate-ogm</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>hibernate-ogm</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
<relativePath>../../</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<!-- MongoDB -->
19+
<dependency>
20+
<groupId>org.hibernate.ogm</groupId>
21+
<artifactId>hibernate-ogm-mongodb</artifactId>
22+
<version>5.4.0.Final</version>
23+
</dependency>
24+
<!-- Neo4j -->
25+
<dependency>
26+
<groupId>org.hibernate.ogm</groupId>
27+
<artifactId>hibernate-ogm-neo4j</artifactId>
28+
<version>5.4.0.Final</version>
29+
</dependency>
30+
<!-- Narayana JTA -->
31+
<dependency>
32+
<groupId>org.jboss.narayana.jta</groupId>
33+
<artifactId>narayana-jta</artifactId>
34+
<version>5.5.23.Final</version>
35+
</dependency>
36+
<!-- Testing -->
37+
<dependency>
38+
<groupId>junit</groupId>
39+
<artifactId>junit</artifactId>
40+
<version>4.12</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.easytesting</groupId>
45+
<artifactId>fest-assert</artifactId>
46+
<version>1.4</version>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<finalName>hibernate-ogm</finalName>
53+
<resources>
54+
<resource>
55+
<directory>src/main/resources</directory>
56+
<filtering>true</filtering>
57+
</resource>
58+
</resources>
59+
</build>
60+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.hibernate.ogm;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
import javax.persistence.ManyToOne;
7+
8+
import org.hibernate.annotations.GenericGenerator;
9+
10+
@Entity
11+
public class Article {
12+
@Id
13+
@GeneratedValue(generator = "uuid")
14+
@GenericGenerator(name = "uuid", strategy = "uuid2")
15+
private String articleId;
16+
17+
private String articleTitle;
18+
19+
@ManyToOne
20+
private Author author;
21+
22+
// constructors, getters and setters...
23+
24+
Article() {
25+
}
26+
27+
public Article(String articleTitle) {
28+
this.articleTitle = articleTitle;
29+
}
30+
31+
public String getArticleId() {
32+
return articleId;
33+
}
34+
35+
public void setArticleId(String articleId) {
36+
this.articleId = articleId;
37+
}
38+
39+
public String getArticleTitle() {
40+
return articleTitle;
41+
}
42+
43+
public void setArticleTitle(String articleTitle) {
44+
this.articleTitle = articleTitle;
45+
}
46+
47+
public Author getAuthor() {
48+
return author;
49+
}
50+
51+
public void setAuthor(Author author) {
52+
this.author = author;
53+
}
54+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.hibernate.ogm;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import javax.persistence.CascadeType;
7+
import javax.persistence.Entity;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.Id;
10+
import javax.persistence.ManyToOne;
11+
import javax.persistence.OneToMany;
12+
13+
import org.hibernate.annotations.GenericGenerator;
14+
15+
@Entity
16+
public class Author {
17+
@Id
18+
@GeneratedValue(generator = "uuid")
19+
@GenericGenerator(name = "uuid", strategy = "uuid2")
20+
private String authorId;
21+
22+
private String authorName;
23+
24+
@ManyToOne
25+
private Editor editor;
26+
27+
@OneToMany(mappedBy = "author", cascade = CascadeType.PERSIST)
28+
private Set<Article> authoredArticles = new HashSet<>();
29+
30+
// constructors, getters and setters...
31+
32+
Author() {
33+
}
34+
35+
public Author(String authorName) {
36+
this.authorName = authorName;
37+
}
38+
39+
public String getAuthorId() {
40+
return authorId;
41+
}
42+
43+
public void setAuthorId(String authorId) {
44+
this.authorId = authorId;
45+
}
46+
47+
public String getAuthorName() {
48+
return authorName;
49+
}
50+
51+
public void setAuthorName(String authorName) {
52+
this.authorName = authorName;
53+
}
54+
55+
public Editor getEditor() {
56+
return editor;
57+
}
58+
59+
public void setEditor(Editor editor) {
60+
this.editor = editor;
61+
}
62+
63+
public Set<Article> getAuthoredArticles() {
64+
return authoredArticles;
65+
}
66+
67+
public void setAuthoredArticles(Set<Article> authoredArticles) {
68+
this.authoredArticles = authoredArticles;
69+
}
70+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.hibernate.ogm;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import javax.persistence.CascadeType;
7+
import javax.persistence.Entity;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.Id;
10+
import javax.persistence.OneToMany;
11+
12+
import org.hibernate.annotations.GenericGenerator;
13+
14+
@Entity
15+
public class Editor {
16+
@Id
17+
@GeneratedValue(generator = "uuid")
18+
@GenericGenerator(name = "uuid", strategy = "uuid2")
19+
private String editorId;
20+
21+
private String editorName;
22+
23+
@OneToMany(mappedBy = "editor", cascade = CascadeType.PERSIST)
24+
private Set<Author> assignedAuthors = new HashSet<>();
25+
26+
// constructors, getters and setters...
27+
28+
Editor() {
29+
}
30+
31+
public Editor(String editorName) {
32+
this.editorName = editorName;
33+
}
34+
35+
public String getEditorId() {
36+
return editorId;
37+
}
38+
39+
public void setEditorId(String editorId) {
40+
this.editorId = editorId;
41+
}
42+
43+
public String getEditorName() {
44+
return editorName;
45+
}
46+
47+
public void setEditorName(String editorName) {
48+
this.editorName = editorName;
49+
}
50+
51+
public Set<Author> getAssignedAuthors() {
52+
return assignedAuthors;
53+
}
54+
55+
public void setAssignedAuthors(Set<Author> assignedAuthors) {
56+
this.assignedAuthors = assignedAuthors;
57+
}
58+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
5+
version="2.0">
6+
<!-- MongoDB -->
7+
<persistence-unit name="ogm-mongodb" transaction-type="JTA">
8+
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
9+
<properties>
10+
<property name="hibernate.ogm.datastore.provider" value="MONGODB" />
11+
<property name="hibernate.ogm.datastore.database" value="TestDB" />
12+
<property name="hibernate.ogm.datastore.create_database" value="true" />
13+
</properties>
14+
</persistence-unit>
15+
<!-- Neo4j -->
16+
<persistence-unit name="ogm-neo4j" transaction-type="JTA">
17+
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
18+
<properties>
19+
<property name="hibernate.ogm.datastore.provider" value="NEO4J_EMBEDDED" />
20+
<property name="hibernate.ogm.datastore.database" value="TestDB" />
21+
<property name="hibernate.ogm.neo4j.database_path" value="target/test_data_dir" />
22+
</properties>
23+
</persistence-unit>
24+
</persistence>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.baeldung.hibernate.ogm;
2+
3+
import static org.fest.assertions.Assertions.assertThat;
4+
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
8+
import javax.persistence.EntityManager;
9+
import javax.persistence.EntityManagerFactory;
10+
import javax.persistence.Persistence;
11+
import javax.transaction.TransactionManager;
12+
13+
import org.junit.Test;
14+
15+
public class EditorUnitTest {
16+
/*
17+
@Test
18+
public void givenMongoDB_WhenEntitiesCreated_thenCanBeRetrieved() throws Exception {
19+
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ogm-mongodb");
20+
Editor editor = generateTestData();
21+
persistTestData(entityManagerFactory, editor);
22+
loadAndVerifyTestData(entityManagerFactory, editor);
23+
}
24+
*/
25+
@Test
26+
public void givenNeo4j_WhenEntitiesCreated_thenCanBeRetrieved() throws Exception {
27+
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ogm-neo4j");
28+
Editor editor = generateTestData();
29+
persistTestData(entityManagerFactory, editor);
30+
loadAndVerifyTestData(entityManagerFactory, editor);
31+
}
32+
33+
private void persistTestData(EntityManagerFactory entityManagerFactory, Editor editor) throws Exception {
34+
TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
35+
EntityManager entityManager;
36+
37+
transactionManager.begin();
38+
entityManager = entityManagerFactory.createEntityManager();
39+
entityManager.persist(editor);
40+
entityManager.close();
41+
transactionManager.commit();
42+
}
43+
44+
private void loadAndVerifyTestData(EntityManagerFactory entityManagerFactory, Editor editor) throws Exception {
45+
TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
46+
EntityManager entityManager;
47+
48+
transactionManager.begin();
49+
entityManager = entityManagerFactory.createEntityManager();
50+
Editor loadedEditor = entityManager.find(Editor.class, editor.getEditorId());
51+
assertThat(loadedEditor).isNotNull();
52+
assertThat(loadedEditor.getEditorName()).isEqualTo("Tom");
53+
assertThat(loadedEditor.getAssignedAuthors()).onProperty("authorName")
54+
.containsOnly("Maria", "Mike");
55+
Map<String, Author> loadedAuthors = loadedEditor.getAssignedAuthors()
56+
.stream()
57+
.collect(Collectors.toMap(Author::getAuthorName, e -> e));
58+
assertThat(loadedAuthors.get("Maria")
59+
.getAuthoredArticles()).onProperty("articleTitle")
60+
.containsOnly("Basic of Hibernate OGM");
61+
assertThat(loadedAuthors.get("Mike")
62+
.getAuthoredArticles()).onProperty("articleTitle")
63+
.containsOnly("Intermediate of Hibernate OGM", "Advanced of Hibernate OGM");
64+
entityManager.close();
65+
transactionManager.commit();
66+
}
67+
68+
private Editor generateTestData() {
69+
Editor tom = new Editor("Tom");
70+
Author maria = new Author("Maria");
71+
Author mike = new Author("Mike");
72+
Article basic = new Article("Basic of Hibernate OGM");
73+
Article intermediate = new Article("Intermediate of Hibernate OGM");
74+
Article advanced = new Article("Advanced of Hibernate OGM");
75+
maria.getAuthoredArticles()
76+
.add(basic);
77+
basic.setAuthor(maria);
78+
mike.getAuthoredArticles()
79+
.add(intermediate);
80+
intermediate.setAuthor(mike);
81+
mike.getAuthoredArticles()
82+
.add(advanced);
83+
advanced.setAuthor(mike);
84+
tom.getAssignedAuthors()
85+
.add(maria);
86+
maria.setEditor(tom);
87+
tom.getAssignedAuthors()
88+
.add(mike);
89+
mike.setEditor(tom);
90+
return tom;
91+
}
92+
}

‎pom.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@
509509
<module>persistence-modules/flyway</module>
510510
<module>persistence-modules/hbase</module>
511511
<module>persistence-modules/hibernate5</module>
512+
<module>persistence-modules/hibernate-ogm</module>
512513
<module>persistence-modules/influxdb</module>
513514
<module>persistence-modules/java-cassandra</module>
514515
<module>persistence-modules/java-cockroachdb</module>
@@ -1220,6 +1221,7 @@
12201221
<module>persistence-modules/flyway</module>
12211222
<module>persistence-modules/hbase</module>
12221223
<module>persistence-modules/hibernate5</module>
1224+
<module>persistence-modules/hibernate-ogm</module>
12231225
<module>persistence-modules/influxdb</module>
12241226
<module>persistence-modules/java-cassandra</module>
12251227
<module>persistence-modules/java-cockroachdb</module>

0 commit comments

Comments
 (0)