Getting “Incompatible types: HibernateProxy and ErrorRowError” when upgrading to Micronaut4

I am upgrading our application to Micronaut 4 (from 3).
Their guide states we have to migrate from javax to jakarta.

Here is one of the classes that is causing the error (kotlin):

import java.util.UUID
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import org.hibernate.annotations.BatchSize
import org.hibernate.proxy.HibernateProxy

@Entity
@Table(name = ERROR_ROW_ERROR, schema = SCHEMA)
@BatchSize(size = BATCH_SIZE)
data class ErrorRowError(
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id: UUID? = null,
    @Column(name = "error")
    val error: String,
    @ManyToOne
    @JoinColumn(name = "error_row_id", nullable = false)
    var errorRow: ErrorRow? = null
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null) return false
        val oEffectiveClass =
            if (other is HibernateProxy) other.hibernateLazyInitializer.persistentClass else other.javaClass
        val thisEffectiveClass =
            if (this is HibernateProxy) this.hibernateLazyInitializer.persistentClass else this.javaClass
        if (thisEffectiveClass != oEffectiveClass) return false
        other as ErrorRowError

        return id != null && id == other.id
    }

    override fun hashCode(): Int = javaClass.hashCode()

    @Override
    override fun toString(): String {
        return this::class.simpleName + "(id = $id , error = $error , errorRow = $errorRow )"
    }

}

when I replace javax with jakarta I am getting:

Incompatible types: HibernateProxy and ErrorRowError on the line where thisEffectiveClass is created.

Any idea on how to solve this?

  • In order to reproduce this, we’ll need full code for the class in question along with imports before/after migrating to MN:4. BR

    – 




  • 1

    @RoarS. Added the whole class code.

    – 

  • Please try to replace data class ErrorRowError with open class ErrorRowErrorBR

    – 

  • I don’t think I’d want to do that as I need that class to be a kotlin data class. Besides, before upgrading to Micronaut4 or without replacing javax with jakarta, it worked perfectly fine.

    – 




  • 1

    Ok I got it! I had to add jakarta.persistence.Entity to the allOpen plugin! That solved the issue. @RoarS. Thanks for pointing me to the right direction!

    – 




Ok I got it! I had to add jakarta.persistence.Entity to the allOpen plugin! That solved the issue.

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlinVersion}</version>
    <configuration>
        <compilerPlugins>
            <plugin>all-open</plugin>
            <plugin>jpa</plugin>
        </compilerPlugins>
        <pluginOptions> 
            <option>all-open:annotation=jakarta.persistence.Entity</option>
         </pluginOptions>    
    </configuration>
    ...
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlinVersion}</version>
        </dependency>
        ...

Leave a Comment