Hej! If I have a SQL table that defines such a tab...
# spring
Hej! If I have a SQL table that defines such a table
Copy code
CREATE TABLE elements (
  resource_id VARCHAR(50) UNIQUE DEFAULT uuid_generate_v4(),
  name VARCHAR(100) NOT NULL
and a class like
Copy code
@Table(name = "elements")
data class Element(
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int?,
    var resourceId: String?,
    var name: String?
How can I only create new objects via
val elem = Element(name="foo")
and let the rest be automatically created via JPA/Hibernate Is that even possible in my case? Do I just need different constructors then?
var id: Int? = null, var resourceId: String? = UUID.randomUUID().toString()
? If you let the database generate the UUID, you’d be forced forced to refresh the entity right after it’s been created in order for Hibernate to know what UUID the database has generated. So why not generate it in Java? That said, you shouldn’t use data classes for JPA entities. Their toString(), hashCode() and equals() implementation are not really adequate, and can even be dangerous if you have associations.
Thank you! Things are working fine now and I’ve removed the “DEFAULT” column value 🙂