我在MongoDB中使用Hibernate OGM(5.0.3.Final)。 我已将此复合Structure与策略= InheritanceType.SINGLE_TABLE一起保留。当我尝试查询TextNode时,我找到了属性" text" null,但如果我用mongo shell检查数据库,我可以看到属性文本不为null。 Here the DB。我报告下面使用的代码。
节点类:
@Entity
@Inheritance( strategy=InheritanceType.SINGLE_TABLE )
public abstract 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 void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@OneToMany( mappedBy="source" )
public Set<NodeLink> getChildren() {
return children;
}
void setChildren(Set<NodeLink> children) {
this.children = children;
}
}
NodeLink类:
@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);
}
}
SimpleNode类:
@Entity
public class SimpleNode extends Node {
public SimpleNode() {
super();
}
}
TextNode类:
@Entity
public class TextNode extends Node {
private String text;
public TextNode() {
super();
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
主要课程:
public class MainNode {
private static EntityManagerFactory ENTITY_MANAGER_FACTORY;
private static EntityManager manager;
public static void main(String[] args) {
ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory("JavaHelps");
manager = ENTITY_MANAGER_FACTORY.createEntityManager();
createDB();
queryDB();
}
//
// CREATE DB
//
private static void createDB() {
EntityTransaction transaction1 = manager.getTransaction();
transaction1.begin();
Node root1 = new SimpleNode();
root1.setName("root");
Node child1 = new SimpleNode();
child1.setName("children1");
TextNode child2 = new TextNode();
child2.setName("children2");
child2.setText("a text");
NodeLink nl4=new NodeLink();
nl4.assignSource(root1);
nl4.assignTarget(child1);
NodeLink nl3=new NodeLink();
nl3.assignSource(root1);
nl3.assignTarget(child2);
manager.persist(root1);
manager.persist(child1);
manager.persist(child2);
manager.persist(nl4);
manager.persist(nl3);
transaction1.commit();
}
//
// QUERY DB
//
@SuppressWarnings("unchecked")
private static void queryDB() {
"db.Node.find({'type_id':"+"ObjectId('"+typeId+"')})";
Node node = manager.find(SimpleNode.class, "5832c8762fc11706a29125b5");
print(node);
}
private static void print(Node n) {
System.out.println(n.getClass().getSimpleName() + " : " + n.getName() + " : " + (n.getClass().getSimpleName().equals("TextNode")? ((TextNode)n).getText() : "") );
for(NodeLink c : n.getChildren()) {
print(c.getTarget());
}
}
}
我的问题是:我在Hibernate OGM中发现错误或我的代码中有错误? 感谢。