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
This commit is contained in:
parent
c5a23bcf47
commit
9271f10941
|
@ -0,0 +1,4 @@
|
||||||
|
## Relevant articles:
|
||||||
|
|
||||||
|
- [Guide to Hibernate OGM](http://www.baeldung.com/xxxx)
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<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"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>hibernate-ogm</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>hibernate-ogm</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- MongoDB -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.ogm</groupId>
|
||||||
|
<artifactId>hibernate-ogm-mongodb</artifactId>
|
||||||
|
<version>5.4.0.Final</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Neo4j -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.ogm</groupId>
|
||||||
|
<artifactId>hibernate-ogm-neo4j</artifactId>
|
||||||
|
<version>5.4.0.Final</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Narayana JTA -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jboss.narayana.jta</groupId>
|
||||||
|
<artifactId>narayana-jta</artifactId>
|
||||||
|
<version>5.5.23.Final</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Testing -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.easytesting</groupId>
|
||||||
|
<artifactId>fest-assert</artifactId>
|
||||||
|
<version>1.4</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>hibernate-ogm</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Article> 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<Article> getAuthoredArticles() {
|
||||||
|
return authoredArticles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthoredArticles(Set<Article> authoredArticles) {
|
||||||
|
this.authoredArticles = authoredArticles;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Author> 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<Author> getAssignedAuthors() {
|
||||||
|
return assignedAuthors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAssignedAuthors(Set<Author> assignedAuthors) {
|
||||||
|
this.assignedAuthors = assignedAuthors;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
|
||||||
|
version="2.0">
|
||||||
|
<!-- MongoDB -->
|
||||||
|
<persistence-unit name="ogm-mongodb" transaction-type="JTA">
|
||||||
|
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
|
||||||
|
<properties>
|
||||||
|
<property name="hibernate.ogm.datastore.provider" value="MONGODB" />
|
||||||
|
<property name="hibernate.ogm.datastore.database" value="TestDB" />
|
||||||
|
<property name="hibernate.ogm.datastore.create_database" value="true" />
|
||||||
|
</properties>
|
||||||
|
</persistence-unit>
|
||||||
|
<!-- Neo4j -->
|
||||||
|
<persistence-unit name="ogm-neo4j" transaction-type="JTA">
|
||||||
|
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
|
||||||
|
<properties>
|
||||||
|
<property name="hibernate.ogm.datastore.provider" value="NEO4J_EMBEDDED" />
|
||||||
|
<property name="hibernate.ogm.datastore.database" value="TestDB" />
|
||||||
|
<property name="hibernate.ogm.neo4j.database_path" value="target/test_data_dir" />
|
||||||
|
</properties>
|
||||||
|
</persistence-unit>
|
||||||
|
</persistence>
|
|
@ -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<String, Author> 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;
|
||||||
|
}
|
||||||
|
}
|
2
pom.xml
2
pom.xml
|
@ -509,6 +509,7 @@
|
||||||
<module>persistence-modules/flyway</module>
|
<module>persistence-modules/flyway</module>
|
||||||
<module>persistence-modules/hbase</module>
|
<module>persistence-modules/hbase</module>
|
||||||
<module>persistence-modules/hibernate5</module>
|
<module>persistence-modules/hibernate5</module>
|
||||||
|
<module>persistence-modules/hibernate-ogm</module>
|
||||||
<module>persistence-modules/influxdb</module>
|
<module>persistence-modules/influxdb</module>
|
||||||
<module>persistence-modules/java-cassandra</module>
|
<module>persistence-modules/java-cassandra</module>
|
||||||
<module>persistence-modules/java-cockroachdb</module>
|
<module>persistence-modules/java-cockroachdb</module>
|
||||||
|
@ -1220,6 +1221,7 @@
|
||||||
<module>persistence-modules/flyway</module>
|
<module>persistence-modules/flyway</module>
|
||||||
<module>persistence-modules/hbase</module>
|
<module>persistence-modules/hbase</module>
|
||||||
<module>persistence-modules/hibernate5</module>
|
<module>persistence-modules/hibernate5</module>
|
||||||
|
<module>persistence-modules/hibernate-ogm</module>
|
||||||
<module>persistence-modules/influxdb</module>
|
<module>persistence-modules/influxdb</module>
|
||||||
<module>persistence-modules/java-cassandra</module>
|
<module>persistence-modules/java-cassandra</module>
|
||||||
<module>persistence-modules/java-cockroachdb</module>
|
<module>persistence-modules/java-cockroachdb</module>
|
||||||
|
|
Loading…
Reference in New Issue