小编Tho*_*axl的帖子

Maven 使用 AnnotationProcessor 构建,它解析 src/main/java 中的文件并生成源到 generate-test-sources/test-annotations

我正在使用 maven 3.0.3 和 Java7。

我有一个 AnnotationProcessor,它应该解析src/main/java( not src/test/java ) 中带注释的 java 文件并为 JUnit-Tests 生成 Helper-Classes。这些助手类应该存储在其中,target/generated-test-sources/test-annotations因为它们使用仅在测试范围内可用的库。(注意:只要此依赖项不在测试范围内,一切都可以正常工作,但是构建会立即失败。它绝对只需要在测试范围内/在单元测试和测试类编译期间。)

我尝试了几种配置但没有任何运气:

  1. 我将 配置为maven-compiler-plugincompile:compile. 生成的 HelperClass 将存储在generated-sources/annotations. generated-test-sources/test-annotations如需要的话。结果是,不会使用测试范围的依赖项。由于编译错误“找不到符号”,构建失败。失败

  2. 我使用了上面的配置并重新定义了生成的SourcesDirectory:

    <generatedSourcesDirectory>
       ${project.build.directory}/generated-test-sources/test-annotations
    </generatedSourcesDirectory>
    
    Run Code Online (Sandbox Code Playgroud)

    生成的类将按generated-test-sources/test-annotations预期存储,但构建仍然失败,因为它尝试按照上述方式编译该文件并错过了测试范围的依赖项。失败

  3. 我尝试使用上述配置并排除**/generated-test-sources/test-annotations/**/*.java以防止编译器在此阶段编译:

    <excludes>
       <exclude>**/generated-test-sources/test-annotations/**/*.java</exclude>
    </excludes>
    
    Run Code Online (Sandbox Code Playgroud)

    没运气。与上述相同的编译器错误。失败

  4. 我将 配置为maven-compiler-plugintest-compile:testCompile. 该助手类理论上可能已经在发生generated-test-sources/test-annotations,但AnnotationProcessor会在位于在注释类不失足src/main/java,不src/test/java,这是编译范围的过程中AFAIK test-compile:testCompile。因此不会找到 Annotated 类,不会生成 HelperClass,因此无法存储在generated-test-sources. 失败

  5. 试图在compile:testCompileand期间运行它test-compile:compile,这在两种情况下都会导致 src/main/java …

java unit-testing code-generation annotations maven

3
推荐指数
1
解决办法
7040
查看次数

使用相关模型的Count加载laravel eloquent模型

鉴于我有两个雄辩的模型:预订和客户。

当我将所有预订与相应客户一起列出时,我还想显示相应客户的总预订量(此预订的数量 + 所有其他预订)。

示例输出:

  • 预订 1:客户 A(总共有 20 个预订)
  • 预订2:客户B(总共有10个预订)
  • 预订3:客户C(VIP:总共有100个预订)

为了避免 n+1 问题(显示此内容时每个预订额外查询一个),我想bookingsCount为客户急切加载。

关系是:

预订: public function customer() { return $this->belongsTo(Customer::class) }

顾客: public function bookings() { return $this->hasMany(Booking::class) }

使用预先加载查询预订的示例

工作,但没有急切地加载bookingsCount:

Booking::whereNotCancelled()->with('customer')->get();
Run Code Online (Sandbox Code Playgroud)

不工作:

Booking::whereNotCancelled()->with('customer')->withCount('customer.bookings')->get();
Run Code Online (Sandbox Code Playgroud)

我了解到,您不能withCount在相关模型的字段上使用,但您可以创建一个hasManyThrough关系并调用withCount该关系,例如Booking::whereNotCancelled()->withCount('customerBookings');请参阅此处接受的答案)。

但是:这不起作用。我想,这是因为一个预订属于一个客户,而一个客户有许多预订。

这是类 Booking 的 hasManyThrough 关系

public function customerBookings()
{
    // return the bookings of this booking's customer
    return $this->hasManyThrough(Booking::class, Customer::class);
}
Run Code Online (Sandbox Code Playgroud)

这是 hasManyThrough 的失败测试

/**
 * @test …
Run Code Online (Sandbox Code Playgroud)

php eager-loading laravel eloquent

3
推荐指数
1
解决办法
2252
查看次数