Hibernate joining two table and fetch all records?

后端 未结 4 2059
离开以前
离开以前 2021-01-02 20:51

I have two entity class Category and Events.I need to join both the tables and fetch all records which matching the given condition

4条回答
  •  时光说笑
    2021-01-02 21:34

    Hibernate is about mapping objects and the relations, however you are mapping simple id fields.

    In your Events class you have the followingL

    @Entity
    @Table(name = "events")
    public class Events implements Serializable { 
    
        @Column(name = "category_i")
        private Integer categoryI;
    
    }
    

    However it should be a Category instead of an Integer.

    @Entity
    @Table(name = "events")
    public class Events implements Serializable { 
    
        @ManyToOne
        @Column(name = "category_i")
        private Category category;
    
    }
    

    Then in your Category you should add the mappedBy field to the @ManyToOne on the events field and remove the @JoinColumn.

    @Entity
    @Table(name = "category")
    public class Category implements Serializable {
    
        @OneToMany(mappedBy="category")
        private Events events;
    }
    

    The same applies to the parentCategoryId of the Category class.

    Now that you have your mapping corrected you should be able to write the query as you wanted to.

提交回复
热议问题