question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GeneratedValue annotation doesn't update kotlin data class while insert

See original GitHub issue
@MappedEntity
data class Model(@Id @GeneratedValue val id: Long, val name: String)

@JdbcRepository(dialect = Dialect.H2)
interface Repository {
    fun save(model: Model): Single<Model>
}

val savedModel = repository.save(Model(id = 0, name = "Test")).blockingGet()

savedModel.id is always 0. Changing val id: Long to var id: Long or even var id: Long? doesn’t work. I have to move id parameter to mutable id field:

@MappedEntity
data class Model(val name: String) {
    @Id @GeneratedValue var id: Long = 0
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
piotrwalkusz1commented, Oct 23, 2019

I forgot to write that I added JPA plugin too:

plugins {
  id "org.jetbrains.kotlin.plugin.jpa" version "1.3.50"
}

Decompilation of Kotlin class shows that no-arg constructor was generated. After more investigation, I found out that code:

@Entity
data class Model(@Id @GeneratedValue val id: Long, val name: String)

compiles to:

public final class Model {
   @GeneratedValue
   private final long id;
   ...
   public Model(@Id long id, @NotNull String name) {
      ...
   }
   ...
   public Model() {
   }

and code:

@Entity
data class Model(val name: String) {
    @Id
    @GeneratedValue
    var id: Long = 0
}

compiles to:

public final class Model {
   @Id
   @GeneratedValue
   private long id;
   ...
   public Model(@NotNull String name) {
      ...
   }
   ...
   public Model() {
   }

In the first case, annotations @Id and @GeneratedValue are on the field and the argument, in the second case, both annotations are on the field.

Workaround

The correct workaround that works for me is:

@Entity
data class Model(@field:Id @GeneratedValue var id: Long? = null, val name: String)

Please notice @field:Id instead of @Id. But still id field has to be mutable…

0reactions
stale[bot]commented, Feb 5, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jpa Repository save() doesn't update existing data
Save this question. Show activity on this post. I am trying to update data and as I know save() method saves entity if...
Read more >
Working with Kotlin and JPA - Baeldung
In this tutorial, we'll explore how to use Kotlin Classes as JPA entities. 2. Dependencies.
Read more >
Best Practices and Common Pitfalls of Using JPA (Hibernate ...
Primary constructors are one of the most loved features in Kotlin. However, adding a primary constructor we lose the default one, so if...
Read more >
How to persist creation and update timestamps with Hibernate
You can use the Hibernate-specific @CreationTimestamp and @UpdateTimestamp annotations and let Hibernate trigger the required updates. It's obvious that the ...
Read more >
Micronaut Data - GitHub Pages
Since Micronaut Data is a build time tool, it will not work correctly unless your ... class User { @GeneratedValue @Id private Long...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found