JDBI can not auto map for my class when i use mapToBean

275 Views Asked by At

I have Category and PaCategory

public class Category implements Serializable {
    private int categoryId;
    private int paCategoryId;
    private String name;

    private PaCategory paCategory;

    public Category() {
    }
    
    //getter and setter
}
public class PaCategory implements Serializable {
    private int paCategoryId;
    private String pname;

    public PaCategory() {
    }

    //getter and setter
}

Main

List<Category> categories = JDBIConnector.get().withHandle(handle -> {
    return handle.createQuery("SELECT c.category_id,c.name,pa.pa_category_id,pa.name pname FROM category c join pa_category pa on pa.pa_category_id =c.pa_category_id WHERE category_id=2").mapToBean(Category.class).list();
});
System.out.println(categories);

paCategory is null

[Category{categoryId=2, paCategoryId=1, name='Gia dụng', paCategory=null}]

But I would like a PaCategory and its value.

1

There are 1 best solutions below

0
On

You will need @Nested annotation for PaCategory

@Nested
private PaCategory paCategory;