@OneToMany 关系休眠

大家好,我有实体:


孩子:@EDIT 感谢@MithatKonuk


@Entity

@Table(name="child")

public class Child {

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(name="ID")

    private int id;


    @Column(name="FirstName")

    private String firstName;


    @Column(name="SecondName")

    private String secondName;


    @Column(name="PESEL")

    private String PESEL;


    @Column(name="Sex")

    private String sex;


  @JsonBackReference

  @ManyToOne(cascade = CascadeType.ALL)

  @JoinColumn(name="familyid",referencedColumnName = "id")

  private Family family;

和家人:@EDIT 感谢@MithatKonuk


@Entity

@Table(name="family")

public class Family {


    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(name = "ID")

    private int id;




    @OneToOne(cascade = CascadeType.ALL)

    private Father father;


    @JsonManagedReference

    @OneToMany(mappedBy = "family",fetch=FetchType.EAGER,cascade = CascadeType.ALL)

    private List<Child> childList;

我想获得属于家庭一部分的儿童名单。我尝试进行查询:@EDIT 感谢@MithatKonuk


@Transactional

public List<Child> findAllChild(int id) {

    // TODO Auto-generated method stub

    HibernateUtil.initManager();

    HibernateUtil.getEntityManager().clear();

    Query query = HibernateUtil.getEntityManager().createQuery("SELECT child FROM Child child FETCH JOIN child.family u WHERE u.id = :userId ");

    query.setParameter("userId",id);

    List<Child> result = query.getResultList();

    HibernateUtil.shutdown();

    return result;

}

我需要你的帮助。


@Edit 好的,所以我将此代码更新为完整实体。你还需要一些吗?很抱歉,这可能很容易,但我正在学习 hibernate,但我不知道这个 cos 出了什么问题。谢谢你的回答。


哔哔one
浏览 176回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java