我有这样的一对一关系
父母
@JsonAutoDetect
@Entity
@Table(name = "Parent")
public class Parent{
private Integer id;
private Child child;
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column (name="idparent")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
@JoinColumn(name="idchild", nullable=true)
public Child getChild() {
return child;
}
public void setChild(Child child) {
child.setParent(this);
this.child = child;
}
}
和孩子
@JsonAutoDetect
@Entity
@Table(name="Child")
public class Child{
private Integer id;
private Parent parent;
@Id
@GeneratedValue(generator = "foreign")
@GenericGenerator(name = "foreign", strategy = "foreign", parameters = { @Parameter(name = "property", value = "parent") })
@Column (name = "idchild")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY, optional = true)
@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
@PrimaryKeyJoinColumn
@JsonIgnore
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent= parent;
}
}
我想创建返回有孩子 isNotNull 的父母的标准,我尝试了一些想法
Criteria criteria = session.createCriteria(Parent.class);
criteria.add(Restrictions.isNotNull("child"));
但不起作用,你能帮我举个例子吗?谢谢