【问题标题】:Micronaut Data JDBC Nested EntitiesMicronaut 数据 JDBC 嵌套实体
【发布时间】:2021-04-20 18:05:26
【问题描述】:

我一直在研究使用 Micronaut Data JDBC 作为现有 jdbi 查询的扩充。是否可以嵌套 MappedEntity 并返回孩子?

示例是具有关系的简单仓库管理系统:

company
+- warehouse
   +- inventory

我想要做的是从inventory 表中查询并获取warehouse 的父级和company 的祖父级。它与能够拥有多个仓库的公司和能够拥有多个库存的仓库之间是一对多的关系。

我的实体看起来像

我一直在玩的示例项目位于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 在支持下,可以用来启动 Postgres 然后运行测试套件,问题应该会作为失败弹出。

谢谢!

【问题讨论】:

    标签: kotlin micronaut-data


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 2010-12-06
      • 2012-04-04
      • 2017-11-27
      相关资源
      最近更新 更多