【问题标题】:JDBI 3: Nested SQLObjectJDBI 3:嵌套 SQLObject
【发布时间】:2021-01-19 07:40:44
【问题描述】:

我尝试使用存储在连接表中的 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);

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

作者.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注解没有正确使用。

【问题讨论】:

    标签: java mysql jdbi3


    【解决方案1】:

    在这种情况下,@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;
    }
    

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

    【讨论】:

      猜你喜欢
      • 2022-10-03
      • 2015-12-04
      • 2021-08-09
      • 2018-11-29
      • 2020-11-07
      • 2013-08-04
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多