My goal is to persist a tree on a store MongoDB using Hibernate OGM (4.1.1 Final) with JPA annotations. I wrote two classes called Node and NodeLink: the first defines the nodes while the second the links between them.
the Node class:
@Entity
public class Node {
private Set<NodeLink> children;
private String id;
private String name;
public Node() {
children = new HashSet<>();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany( mappedBy="source" )
public Set<NodeLink> getChildren() {
return children;
}
void setChildren(Set<NodeLink> children) {
this.children = children;
}
}
the NodeLink class:
@Entity
public class NodeLink {
private String id;
private Node source;
private Node target;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@ManyToOne
public Node getSource() {
return source;
}
void setSource(Node source) {
this.source = source;
}
public void assignSource(Node source) {
setSource(source);
source.getChildren().add(this);
}
@ManyToOne
public Node getTarget() {
return target;
}
void setTarget(Node target) {
this.target = target;
}
public void assignTarget(Node target) {
setTarget(target);
}
}
The code that creates the classes and persist them is as it follows:
EntityTransaction transaction1 = manager.getTransaction();
transaction1.begin();
Node root = new Node();
root.setName("root");
Node children1 = new Node();
children1.setName("children1");
Node children2 = new Node();
children2.setName("children2");
NodeLink nl1 = new NodeLink();
nl1.assignSource(root);
nl1.assignTarget(children1);
NodeLink nl2 = new NodeLink();
nl2.assignSource(root);
nl2.assignTarget(children2);
manager.persist(root);
manager.persist(children1);
manager.persist(children2);
manager.persist(nl1);
manager.persist(nl2);
transaction1.commit();
What is persisted in MongoDB is this.
As can be seen on the image, the 'root' node is persisted correctly, with the correct references to children. Instead, the 'children1' and 'children2' nodes each have one reference to a NodeLink object, even if the original object does not have them. So my question is: this behavior is due to a bug in Hibernate OGM or MongoDB? Otherwise there is an error in my code?
I think you have found a bug. I've created an issue and sent a pull request: https://hibernate.atlassian.net/browse/OGM-1198
You could try to test the pull request or wait until the next release. Thanks