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:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I forgot to write that I added JPA plugin too:
Decompilation of Kotlin class shows that no-arg constructor was generated. After more investigation, I found out that code:
compiles to:
and code:
compiles to:
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:
Please notice
@field:Id
instead of@Id
. But stillid
field has to be mutable…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.