Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Last active May 31, 2020 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gakuzzzz/67ce2c77fa4b864dedeb82300ec8fac6 to your computer and use it in GitHub Desktop.
Save gakuzzzz/67ce2c77fa4b864dedeb82300ec8fac6 to your computer and use it in GitHub Desktop.
Doma2 Criteria API with Immutable entity.

associateの例 を Immutable Entity でどうするか

1:1:N ぐらいなら collect で何とでもなる

    Employee_ e = new Employee_();
    Task_ t = new Task_();
    Address_ a = new Address_();

    Map<Employee, Tuple2<List<Task>, Optional<Address>>> result = nativeql
        .from(e)
        .innerJoin(t, on -> on.eq(e.id, t.asiginee))
        .innerJoin(a, on -> on.eq(e.addressId, a.addressId))
        .orderBy(c -> c.asc(e.employeeName))
        .select(e, t, a)
        .collect(
           groupingBy(
               Tuple3::getItem1, 
               teeing(
                 mapping(Tuple3::getItem2, toList()),
                 mapping(Tuple3::getItem3, first()),
                 Tuple2::new
               )
           )
        );

inner join なのに Address がOptionalになってしまうのと、Tuple3::getItem1 とかが何を表してるか分かりづらい……。 joined entity type から entity type を取り出すのに Metamodel のメソッドが使えると解決するかも?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment