JDBI 3:嵌套 SQLObject
JDBI 3: Nested SQLObject
我尝试使用 jdbi 3 收集存储在连接表中的复合对象。实体事件 1 -> n 作者是数据结构的示例。它们通过 author_id 连接,保存在事件的列中。
它们很容易通过连接查询,但我无法使用 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 为空。所以我猜@Nested注解没有用好。
在这种情况下,@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);
我尝试使用 jdbi 3 收集存储在连接表中的复合对象。实体事件 1 -> n 作者是数据结构的示例。它们通过 author_id 连接,保存在事件的列中。
它们很容易通过连接查询,但我无法使用 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 为空。所以我猜@Nested注解没有用好。
在这种情况下,@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);