当我试图获取递归表的根节点时,hibernate没有找到它。
表架构:
+----------+------------------+------+-----+---------+----------------+
| 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查询中添加一个标题时(我通过idbox = 1和name =&#39; root&#39;进行搜索)结果为null
仅当idparent设置为NULL
时才会出现问题2)我是否需要为自己辩护?
知道为什么吗? 感谢
答案 0 :(得分:0)
问题的原因是您已将idparent
连接列定义为不可为空,即使您的数据库明确允许此列的NULL
值。
基于此Hibernate将认为父ID不能包含空值,这将影响它生成的SQL代码。在您的情况下,它会导致您在评论中提到INNER JOIN
。
要解决此问题,请将连接列设置为可为空:@JoinColumn(name="idparent")
(如果查看其源代码,默认情况下它是可为空的,因此无需明确指定它。)