1

我尝试使用存储在连接表中的 jdbi 3 收集复合对象。实体事件 1 -> n 作者是数据结构的示例。它们通过 author_id 连接,该 author_id 保存在 event 的列中。

它们很容易通过 join 查询,但我无法使用 jdbi 中的给定注释创建对象。你能告诉我我在使用 jdbi 时的错误,并让我有机会用 jdbi 处理“复杂”对象吗?

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);

事件.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;
}

作者.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
  }
]

如您所见,作者为空。所以我猜@Nested 注释没有正确使用。

4

1 回答 1

0

在这种情况下,@Nested 应该在没有 ("a") 的情况下使用。这样可行。

事件.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;
}

    @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);
于 2021-01-25T17:17:25.180 回答