我沒有看到這個問題:
@Entity
class Node {
@Id @GeneratedValue
private int id;
private String name;
@ManyToOne(cascade = CascadeType.ALL)
Node parent;
@OneToOne(cascade = CascadeType.ALL)
Node leftChild;
@OneToOne(cascade = CascadeType.ALL)
Node rightChild;
Node() {}
public Node(String name) {
this.name = name;
}
// omitted getters and setters for brevity
}
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration()
.addAnnotatedClass(Node.class)
.setProperty("hibernate.connection.url",
"jdbc:h2:mem:foo;DB_CLOSE_DELAY=-1")
.setProperty("hibernate.hbm2ddl.auto", "create")
.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Node a = new Node("A");
Node b = new Node("B");
Node c = new Node("C");
Node d = new Node("D");
Node e = new Node("E");
a.setLeftChild(b);
b.setParent(a);
a.setRightChild(c);
c.setParent(a);
b.setLeftChild(d);
d.setParent(b);
b.setRightChild(e);
e.setParent(b);
System.out.println("Before saving:");
print(a, 1);
Serializable rootNodeId = session.save(a);
transaction.commit();
session.close();
session = sessionFactory.openSession();
Node root = (Node) session.load(Node.class, rootNodeId);
System.out.println("Freshly loaded:");
print(root, 1);
session.close();
}
private static void print(Node node, int depth) {
if (node == null) { return; }
System.out.format("%" + depth + "s\n", node);
print(node.getLeftChild(), depth + 1);
print(node.getRightChild(), depth + 1);
}
工程與Hibernate 4.3.11。謝謝! – Jochen