简体   繁体   English

休眠一对多关系

[英]Hibernate OnetoMany relationship

I am trying to implement OnetoMany relationship for two tables student and certificates. 我正在尝试为两个表的学生和证书实施OnetoMany关系。 I have two classes, Student and Certificates which have OnetoMany relationship from Student class to Certificates class. 我有两个班级,学生班和证书班,从学生班到证书班有一对多的关系。

Student Class: 学生班:

@Entity
@Table(name = "student")
public class Student implements Serializable{

    @Id  
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;
    @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="student") 
    private List<Certificats> listCertificats;
    // Getters and Setters

Certificats class: 证书类别:

   @Entity
   @Table(name = "certificates")
   public class Certificates implements Serializable{

        @Id  
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name = "id")
        private Integer id;
        @ManyToOne
        @JoinColumn(name="student_id")
        private Student student;    
        // Getters and Setters

My DAO class has a method to fetch data from the database: 我的DAO类有一种从数据库中获取数据的方法:

    @SuppressWarnings("unchecked")
    public List<Students> listStudents() {

        List<Students> studentList;

        studentList= sessionFactory.getCurrentSession().createQuery("from Student").list();

        return studentList;
    }

Now when I run this method, the value for the listCertificats in the Student class which is a List is always null. 现在,当我运行此方法时,作为List的Student类中listCertificats的值始终为null。 However, I get value for id field in the Student class. 但是,我在Student类中获得了id字段的值。 What am I doing wrong here? 我在这里做错了什么? I checked some tutorials for Hibernate OnetoMany relationship and they described the same way I implemented. 我检查了一些有关Hibernate OnetoMany关系的教程,它们描述的方式与我实现的方式相同。 Any would be much appreciated. 任何将不胜感激。

FetchType.lazy implies that you will not be seeing data of your child entities until you access specific properties of them. FetchType.lazy表示直到访问子实体的特定属性,您才可以看到它们的数据。

Please specify when are you seeing null? 请指定您何时看到空值? Are you seeing null even when you access them? 即使访问它们,您仍然看到null吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM