Hibernate 在递归表中找不到休眠根项
当我试图获取递归表的根节点时,hibernate没有找到它 表架构:Hibernate 在递归表中找不到休眠根项,hibernate,jpa,Hibernate,Jpa,当我试图获取递归表的根节点时,hibernate没有找到它 表架构: +----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+----------------+
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| idbox | int(10) unsigned | NO | MUL | NULL | |
| idparent | int(10) unsigned | YES | MUL | NULL | |
| nom | text | NO | | NULL | |
+----------+------------------+------+-----+---------+----------------+
数据表的选择:
+----+-------+----------+----------------+
| id | idbox | idparent | nom |
+----+-------+----------+----------------+
| 1 | 1 | 16 | RDC |
| 2 | 1 | 1 | salon |
| 3 | 1 | 1 | cuisine |
| 4 | 1 | 1 | room invite |
| 5 | 1 | 16 | etage 1 |
| 6 | 1 | 5 | room parent |
| 7 | 1 | 5 | room david |
| 8 | 1 | 5 | room sarah |
| 9 | 1 | 5 | room rachel |
| 10 | 1 | 5 | room leon |
| 11 | 1 | 16 | etage 2 |
| 12 | 1 | 11 | grenier |
| 13 | 1 | 5 | WC |
| 14 | 1 | 5 | SDB |
| 15 | 1 | 6 | SDB-Parent |
| 16 | 1 | NULL | root |
+----+-------+----------+----------------+
班级职务:
public class Position implements Serializable, Comparable<Position>
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private int id;
@Lob
@Column(nullable=false)
private String nom;
//bi-directional many-to-one association to Dispositif
@OneToMany(mappedBy="position")
private Set<Dispositif> dispositifs;
//bi-directional many-to-one association to Box
@ManyToOne
@JoinColumn(name="idbox", nullable=false)
private Box box;
//bi-directional many-to-one association to Position
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="idparent", nullable=false)
private Position parent;
//bi-directional many-to-one association to Position
@OneToMany(mappedBy="parent", orphanRemoval=true, cascade = CascadeType.ALL )
private Set<Position> positions = new HashSet<Position>();
...
}
因此,当我在hibernate查询中添加critieria时,我通过idbox=1和name='root'进行搜索,结果为空
仅当idparent设置为NULL时,才会出现此问题
2.我是否需要自己引用根
知道为什么吗?
谢谢问题的原因是您已将idparent join列定义为不可为NULL,即使您的数据库明确允许此列为NULL值 基于此,Hibernate将认为父ID不能包含空值,这将影响它生成的SQL代码。在您的案例中,它导致使用您在评论中提到的内部连接
要修复此问题,请将联接列设置为null,如下所示:@JoinColumnname=idparent如果查看其源代码,它默认为null,因此无需显式指定它。请使用JPA实体的代码和正在运行的Hibernate查询更新此问题,好吗?Thanks@BohuslavBurghardt我发现了问题,但不知道如何解决。问题是hibernate在这个u.idparent=position4.id上做了一个引用他自己的内部联接:内部联接位置4 u,其中这个u.idbox=?而这个uz.nom=?@JoinColumnname=idparent,nullable=false。当数据库中根的idparent中显然有空值时,为什么将其设置为不可空值。我认为这可能与Hibernate认为不能使用父id时生成的查询有关null@BohuslavBurghardt非常感谢,这是工作。我更改了数据库的模式,并使该字段为空,但我忘记了JPA。再次非常感谢!!很高兴我能帮忙。顺便说一句,我发布了完整的解决方案作为答案。如果它解决了你的问题,你能帮我吗?谢谢: