JDBI 3: Nested SQLObject

2.5k Views Asked by At

I try to gather compounded objects with jdbi 3 which are stored in connected tables. The entities event 1 -> n author are an example for the data structure. They are connected via author_id which is saved in a column in event.

They were easily queried via join but I couldn't create an object with the given annotations from jdbi. Can you show me my mistake in the usage of jdbi and give me the opportunity to handle "complex" objects with jdbi?

Code of the DAO

    @SqlQuery("SELECT e.id as e_id, e.name as e_name, e.start_time as e_start_time, e.lastupdated as e_lastupdated, a.id as a_id, a.name as a_name, a.mail_address as a_mail_address FROM event e INNER JOIN author a ON(e.author_id = a.id) WHERE e.id = :event_id")
    @RegisterBeanMapper(value = Event.class, prefix = "e")
    @RegisterBeanMapper(value = Author.class, prefix = "a")
    List<Event> getFeedbackByEventId(@Bind("author_id") int authorId, @Bind("event_id") int eventId);

Event.java

@Data
public class Event {
    private int id;

    @NonNull
    private String name;

    private LocalDateTime startTime;

    private LocalDateTime lastUpdated;

    @NonNull
    @Nested("a")
    private Author author;

    private Feedback feedback;

    private List<ValuedIndicator> valuedIndicators;
}

Author.java

public class Author {
    private static final Logger LOGGER = LoggerFactory.getLogger(Author.class);

    private int id;

    @NonNull
    private String name;

    @NonNull
    private String mailAddress;
}
Result
[
  {
    "id": 1,
    "name": "Testveranstaltung",
    "startTime": "1970-01-01T01:16:40",
    "lastUpdated": "2021-01-14T17:15:09",
    "author": null,
    "feedback": null,
    "valuedIndicators": null
  }
]

As you can see author is null. So I guess the @Nested annotation is not used properly.

1

There are 1 best solutions below

0
On BEST ANSWER

In this case the @Nested should be used without ("a"). That works.

Event.java

@Data
public class Event {
    private int id;

    @NonNull
    private String name;

    private LocalDateTime startTime;

    private LocalDateTime lastUpdated;


    @Nested
    private Author author;

    private Feedback feedback;

    private List<ValuedIndicator> valuedIndicators;
}

DAO

    @SqlQuery("SELECT e.id as e_id, e.name as e_name, e.start_time as e_start_time, e.lastupdated as e_lastupdated, a.id as a_id, a.name as a_name, a.mail_address as a_mail_address FROM event e INNER JOIN author a ON(e.author_id = a.id) WHERE e.id = :event_id")
    @RegisterBeanMapper(value = Event.class, prefix = "e")
    @RegisterBeanMapper(value = Author.class, prefix = "a")
    List<Event> getFeedbackByEventId(@Bind("author_id") int authorId, @Bind("event_id") int eventId);