我正在尝试使用实体和 JPA 创建树。我有一个具有以下属性的类。
public class Dir
{
@Id
@Basic(optional = false)
@NotNull
@Column(name = "dirId")
private Integer dirId;
@OneToOne(mappedBy="dirId", cascade= CascadeType.ALL)
private Dir parent;
...
一个节点知道它的父节点是谁,如果它没有父节点,它就是根节点。这就是我可以轻松构建一棵树的方法。但是......我认为映射对于这个想法是不正确的。尝试部署时出现以下错误:
An incompatible mapping has been encountered between [class com.dv.oa.model.entity.dir.Dir] and [class com.dv.oa.model.entity.dir.Dir]. This usually occurs when the cardinality of a mapping does not correspond with the cardinality of its backpointer.
它谈到了基数。但这没有意义,一个节点只能有一个父节点。这就是我选择的原因@OneToOne
任何人都可以对此有所了解吗?我想另一种问这个问题的方法是,你如何将一个实体映射到它自己的另一个实例?
编辑
这是我的表结构:
mysql> describe dir;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| dirId | int(11) | NO | PRI | NULL | |
| DTYPE | varchar(31) | YES | | NULL | |
| dirName | varchar(255) | YES | | NULL | |
| companyOwner | int(11) | YES | MUL | NULL | |
| userOwner | int(11) | YES | MUL | NULL | |
| parent | int(11) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)