我尝试使用jdbi 3收集存储在连接表中的复合对象。实体event 1 -> n author是数据结构的一个示例。它们通过事件中保存在列中的author_id连接。
它们很容易通过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);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
}
]如您所见,author为空。所以我猜@嵌套注释没有正确使用。
发布于 2021-01-26 01:17:25
在这种情况下,@Nested应该不带("a")。这是可行的。
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);https://stackoverflow.com/questions/65787198
复制相似问题