ebean: Q: Why can't kapt kotlin generate var Set to query bean

@Entity
@Table(name = "sys_user")
open class SysUser {
    var name: String? = null

    @ManyToMany
    var roles: Set<SysRole>? = null

    @OneToOne
    @JoinColumn(name = "job_id")
    var job: SysJob? = null
}
@Generated("io.ebean.querybean.kotlin-generator")
@TypeQueryBean
class QSysUser : TQRootBean<SysUser, QSysUser> {

  companion object {
    /**
     * shared 'Alias' instance used to provide
     * properties to select and fetch clauses
     */
    val _alias = QSysUser(true)
  }

  lateinit var name: PString<QSysUser>
  lateinit var job: QAssocSysJob<QSysUser>

  /**
   * Construct with a given EbeanServer.
   */
  constructor(server: EbeanServer) : super(SysUser::class.java, server)

  /**
   * Construct using the default EbeanServer.
   */
  constructor() : super(SysUser::class.java)

  /**
   * Construct for Alias.
   */
  private constructor(dummy: Boolean) : super(dummy)
}

lost roles in query bean

If I replace

var roles: Set < SysRole >? = null 

for

val roles: Set < SysRole >? = null

It generates

lateinit var roles: QAssocSysRole<QSysUser>

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (13 by maintainers)

Most upvoted comments

Yes I see that just arrive, great thanks!!

If more people / organisations did that we could dedicate more time, get better documentation etc. Thanks for the donation!!

Cheers, Rob.

I donated $10 as a thank you. I really like your project and admire your work.

obtained through circular numbers

Yes quite right. Note that UUID can be encoded to binary(16) for databases that don’t have native uuid support. So binary(16) could be considered better that varchar in the sense of smaller storage, smaller indexes etc.

We can review the db-create-all.sql for the generated foreign key names like:

alter table sys_user add constraint fk_sys_user_job_id foreign key (job_id) references sys_job (id) on delete restrict on update restrict;

alter table sys_user add constraint fk_sys_user_dept_id foreign key (dept_id) references sys_dept (id) on delete restrict on update restrict;

Generally if this is a new DB schema I’d pretty much not expect any @JoinColumn or @JoinTable annotations.as these all have good defaults via the naming convention.

For example, the @JoinColumn can be removed - that matches the naming convention so isn’t needed.

    @OneToOne
    @JoinColumn(name = "job_id")   // <-- we can remove this as it matches the naming convention
    var job: SysJob? = null

Side notes, suggestions for making more things Kotlin non-nullable types:

Instead of this …

    @NotNull
    var enabled: Boolean? = null

Make it a non nullable type …

    var enabled: Boolean = false

Instead of …

open class SysUser : BaseModel() {

    @NotBlank
    @Column(unique = true)
    var username: String? = null

Have username as a constructor parameter of non-nullable String …

open class SysUser(username: String) : BaseModel() {

    @NotBlank
    @Column(unique = true)
    var username: String = username

Note that for we also need this fix: https://github.com/ebean-orm/ebean/issues/1804