Home > front end >  How to force variable to be public kotlin
How to force variable to be public kotlin

Time:11-12

i have the following code:


@Entity
@Table(name = "`users`")
class User(
    var name: String,
    var avatar: ByteArray
) {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    var id: Long = -1


    fun getAvatarAsImage(): BufferedImage? {
        val img: InputStream = ByteArrayInputStream(avatar)
        return ImageIO.read(img)
    }

    fun setAvatarAsImage(img: BufferedImage) {
        val out = ByteArrayOutputStream()
        ImageIO.write(img, "PNG", out)
        avatar = out.toByteArray()
    }

}

but hibernate says that id is private (thats because kotlin auto-generating getters and setters), so compiled to java it seems like:

@Id
@GeneratedValue(
   strategy = GenerationType.AUTO
)
private long id;

public long getId() {
   return this.id;
}

public void setId(long var1) {
   this.id = var1;
}

QUESTION: how to make it compiled like:

@Id
@GeneratedValue(
   strategy = GenerationType.AUTO
)
public long id;

???

idk what is this....

CodePudding user response:

I'm not sure you have interpreted the error message from Hibernate properly, as the @Id-annotation seems correct. But I'm wondering if you perhaps forgot to use the kotlin-jpa compiler plugin that helps creating Hibernate-friendly classes?

Read more here: https://kotlinlang.org/docs/no-arg-plugin.html#jpa-support

CodePudding user response:

Setter and getter of a variable are public by default in Kotlin. As you can see in your Java code, getter and setter are public as well. So this should be fine.

Can you show us the warning/error message?

  • Related