Issue
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....
Solution
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
Maybe you can try putting the annotations on the getter instead to force Hibernate to use property-based access... Try modifying the annotations to:
@get:Id
@get:GeneratedValue(strategy = GenerationType.AUTO)
var id: Long = -1
... or even worst case expose the field as public (as you are asking for) with...
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JvmField
var id: Long = -1
Answered By - Per Huss
Answer Checked By - Gilberto Lyons (JavaFixing Admin)