spring-data-jdbc:与JOOQ包含1-n关系的查询实体

问题描述 投票:0回答:1

[我正在尝试借助JOOQ(基于spring-data-jdbc)加载包含对另一个实体(1-n)的引用的实体。

我开始扩展spring-data-jdbc-jooq-example

具有1-n关系的调整模型:

@Data
public class Category {

    private @Id Long id;
    private String name, description;
    private AgeGroup ageGroup;
    private Set<SubCategory> subCategories;

    public Category() {}

    public Category(Long id, String name, String description, AgeGroup ageGroup) {
        this(id, name, description, ageGroup, new HashSet<>());
    }

    public Category(Long id, String name, String description, AgeGroup ageGroup, Set<SubCategory> subCategories) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.ageGroup = ageGroup;
        this.subCategories = subCategories;
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SubCategory {

    private @Id Long id;
    private String title;

}

我写了两个查询,一个通过@Query中的CrudRepository-注释,一个通过JOOQ中的JooqRepository帮助。

interface CategoryRepository extends CrudRepository<Category, Long>, JooqRepository {

    @Query("SELECT * FROM category")
    List<Category> findAllWithQuery();

}
public interface JooqRepository {

    List<Category> findAllWithJooq();
}
public class JooqRepositoryImpl implements JooqRepository {

    private final DSLContext dslContext;

    public JooqRepositoryImpl(DSLContext dslContext) {
        this.dslContext = dslContext;
    }

    @Override
    public List<Category> findAllWithJooq() {
        return dslContext.select()
                .from(CATEGORY)
                .fetchInto(Category.class);
    }
}

(对我来说,这两个方法应返回相同的结果集b / c,它们执行相同的查询?!)

但是我的单元测试失败:

@Test
public void exerciseRepositoryForSimpleEntity() {
  // create some categories
  SubCategory sub0 = new SubCategory(null, "sub0");
  SubCategory sub1 = new SubCategory(null, "sub1");
  Category cars = new Category(null, "Cars", "Anything that has approximately 4 wheels", AgeGroup._3to8, Sets.newLinkedHashSet(sub0, sub1));

  // save category
  repository.saveAll(asList(cars));

  // execute
  List<Category> actual = repository.findAllWithJooq();
  List<Category> compare = repository.findAllWithQuery();
  Output.list(actual, "JOOQ");
  Output.list(compare, "Query");

  // verify
  assertThat(actual).as("same size of categories").hasSize(compare.size());
  assertThat(actual.get(0).getSubCategories()).as("same size of sub-categories").hasSize(compare.get(0).getSubCategories().size());
}

with

java.lang.AssertionError: [same size of sub-categories] 
Expecting actual not to be null

如您在以下输出中看到的,将不会加载JOOQ查询的子类别:

2019-11-26 16:28:00.749  INFO 18882 --- [           main] example.springdata.jdbc.jooq.Output      : ==== JOOQ ====
Category(id=1,
    name=Cars,
    description=Anything that has approximately 4 wheels,
    ageGroup=_3to8,
    subCategories=null)
2019-11-26 16:28:00.749  INFO 18882 --- [           main] example.springdata.jdbc.jooq.Output      : ==== Query ====
Category(id=1,
    name=Cars,
    description=Anything that has approximately 4 wheels,
    ageGroup=_3to8,
    subCategories=[SubCategory(id=1,
    title=sub0),
    SubCategory(id=2,
    title=sub1)])

这是使用的数据库格式:

CREATE TABLE IF NOT EXISTS category (
  id INTEGER IDENTITY PRIMARY KEY,
  name VARCHAR(100),
  description VARCHAR(2000),
  age_group VARCHAR(20)
);

CREATE TABLE IF NOT EXISTS sub_category (
    id INTEGER IDENTITY PRIMARY KEY,
    title VARCHAR(100),
    category INTEGER
)
spring-boot jooq spring-data-jdbc
1个回答
0
投票

在JOOQ变体中,JOOQ执行从ResultSet到对象实例的转换。由于JOOQ不了解聚合的解释,因为它是由Spring Data JDBC完成的,因此它仅对Category本身进行水合,而不对Set所包含的SubCategory进行水合。

另一方面,Spring Data JDBC解释了Category的结构,并在此基础上执行另一条语句来加载子类别。

© www.soinside.com 2019 - 2024. All rights reserved.