Search code examples
hibernatekotlinjpaquerydsl

QueryDsl Embedded is not mapped


I'm trying to add a where condition on a query to check if any value in a list is present in another list.

jpaQuery.where(adminDsl.role.profiles.any().`in`(getUserRole().profiles))

But I'm getting a "QuerySyntaxException: Role is not mapped". I think it is because the Role is embedded in the Administrator (see below for code).

[INFO] GCLOUD: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Role is not mapped [select sqlAdministrator
[INFO] GCLOUD: from com.wizy.emm.domain.sql.SqlAdministrator sqlAdministrator
[INFO] GCLOUD:   left join sqlAdministrator.federation
[INFO] GCLOUD: where sqlAdministrator.federation.name = ?1 and exists (select 1
[INFO] GCLOUD: from Role sqlAdministrator_role_530393770 --This line is the issue 
[INFO] GCLOUD:   inner join sqlAdministrator_role_530393770.profiles as sqlAdministrator_role_profiles_0
[INFO] GCLOUD: where sqlAdministrator_role_530393770 = sqlAdministrator.role and sqlAdministrator_role_profiles_0 = ?2)]
@Entity
@Table(uniqueConstraints = [UniqueConstraint(columnNames = ["email", "federation_id"])])
class SqlAdministrator internal constructor(
        federation: SqlCustomer,
        override var firstName: String,
        override var lastName: String,
        override var email: String,
        @Embedded
        @Column(nullable = false)
        @Target(SqlRole::class)
        override var role: Role
): Administrator

interface Administrator
    : ModelEntity<UUID> {
    var email: String
    var firstName: String
    var lastName: String
    var role: Role
}
@GenerateDefaultConstructor
class SqlRole(
        @Column(nullable = false)
        @Enumerated(EnumType.STRING)
        override val type: RoleType,
        profiles: Set<Policy> = setOf(),
        endpoints: Set<EndpointAction> = setOf(),
        @Enumerated(EnumType.STRING)
        override val restrictionType: RestrictionType = RestrictionType.NONE
): Role

interface Role {
    val type: RoleType
    var profiles: Set<Policy>
    var endpoints: Set<EndpointAction>
    val restrictionType: RestrictionType
}

On solution could be to move the Role to a separate table to be an entity but that would imply a migration task.

Does a solution without a migration task exist ?

Thanks


Solution

  • You just have to register SqlRole as an Embeddable in your persistence.xml (or annotate it with @Embeddable. I am also not sure if @Target is allowed for embeddables, I am pretty sure though that it isn't. So you'd have to change the fields type to SqlRole as well.