Micronaut 数据 JDBC 嵌套实体

Micronaut Data JDBC Nested Entities

我一直在研究使用 Micronaut 数据 JDBC 作为现有 jdbi 查询的增强。是否可以嵌套 MappedEntity 和 return children?

示例适用于具有以下关系的简单仓库管理系统:

company
+- warehouse
   +- inventory

我想要做的是从 inventory table 查询并获得 warehouse 和 grand[=43= 的 parent ] 的 company。这是一个one-to-many关系,一个公司可以有多个仓库,一个仓库可以有多个存货。

我的实体看起来像

我一直在玩的示例项目位于 wms-api

@JdbcRepository
interface CompanyRepository : PageableRepository<Company, UUID>


@MappedEntity
data class Company(

   @field:Id
   @field:GeneratedValue
   var id: UUID? = null,

   @field:GeneratedValue
   var timeCreated: OffsetDateTime? = null,

   @field:GeneratedValue
   var timeUpdated: OffsetDateTime? = null,

   var name: String
)
@JdbcRepository
interface WarehouseRepository : GenericRepository<Warehouse, UUID> { // used GenericRepository to allow the mapping of Company

   @Join(value = "company")
   fun findById(id: UUID): Warehouse?

   @Join(value = "company")
   fun findByCompany(company: Company, pageable: Pageable): Slice<Warehouse>

   @Join(value = "company")
   fun findAllByCompany(company: Company, pageable: Pageable): Slice<Warehouse>

   @Join(value = "company")
   fun existsByLocationAndCompany(location: String, company: Company): Boolean

   fun save(warehouse: Warehouse): Warehouse

   fun update(warehouse: Warehouse): Warehouse
}

@MappedEntity
data class Warehouse(

   @field:Id @field:GeneratedValue
   var id: UUID? = null,

   @field:GeneratedValue
   var timeCreated: OffsetDateTime? = null,

   @field:GeneratedValue
   var timeUpdated: OffsetDateTime? = null,

   var location: String,

   @field:Relation(value = MANY_TO_ONE)
   var company: Company
)
@JdbcRepository
interface InventoryRepository : GenericRepository<Inventory, UUID> {

   fun save(inventory: Inventory): Inventory

   @Join(value = "warehouse")
   // FIXME need some way to tell repo to also pull company which is attached to warehouse
   fun findById(id: UUID): Inventory?

}

@MappedEntity
data class Inventory(

   @field:Id
   @field:GeneratedValue
   var id: UUID? = null,

   @field:GeneratedValue
   var timeCreated: OffsetDateTime? = null,

   @field:GeneratedValue
   var timeUpdated: OffsetDateTime? = null,

   var manufacturer: String,

   var barcode: String,

   var name: String,

   @field:Relation(value = MANY_TO_ONE)  // FIXME the problem is caused by this.
   var warehouse: Warehouse,
)

堆栈跟踪的相关部分

Caused by: io.micronaut.core.reflect.exception.InstantiationException: Null argument specified for [company]. If this argument is allowed to be null annotate it with @Nullable
    at io.micronaut.core.beans.AbstractBeanIntrospection.instantiate(AbstractBeanIntrospection.java:121)
    at io.micronaut.core.beans.BeanIntrospection.instantiate(BeanIntrospection.java:81)
    at io.micronaut.data.runtime.mapper.sql.SqlResultEntityTypeMapper.readEntity(SqlResultEntityTypeMapper.java:345)

如果可能的话,我不想让 Warehouse.company 属性 可选。

在链接的项目中有一个 docker-compose.yml under support 可以用来启动 Postgres 然后 运行 测试套件,问题应该作为失败弹出。

谢谢!

想通了。必须将 InventoryRepository 的 findById 方法更改为看起来像

@JoinSpecifications(
   Join(value = "warehouse"),
   Join(value = "warehouse.company")
)
fun findById(id: UUID): Inventory?