G
G
grv12017-11-03 17:15:56
Hibernate
grv1, 2017-11-03 17:15:56

How to get join of two Entity in Hibernate JPA?

From the joined objects (Group and Category), it is necessary to select not all fields and return the hierarchy (CategoryPlain)
Reason an object is needed for Rest Api

public class CategoryPlain {
    private Integer id;
    private String title;
    private Collection<Group> groups;
}


@Entity
@Table(name = "category")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
public class Category extends BaseModelEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true)
    @Nullable
    private Integer id;

    @Column(name = "title")
    @NonNull
    private String title;

}

@Entity
@Table(name = "`group`")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
public class Group extends BaseModelEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true)
    @Nullable
    private Integer id;

    @Column(name = "category_id")
    @NonNull
    private Integer categoryId;

    @Column(name = "title")
    @NonNull
    private String title;

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question