Records can be used for defining DTO's but understanding how DTO's are manged in the persistence context will help in understanding how records can be used with springdata. This video is a thorough explanation on that: th-cam.com/video/SyP258VgzSs/w-d-xo.html
@@DanVega i looked into spring data jdbc, found a tutorial and the guy said “it’s preferred to work with immutable classes”. is that the reason why it works with records?
I was using Kotlin mainly because I love how simple it is to make data classes but now I am just finding out a very similar thing is in Java now?? Let’s go!
What about classes which have a lot of properties? This is nice for small data classes, but what if it has something like 20 different properties that must be set?
@@DanVega That's good. Although I'm mainly thinking about usage. So it would be readable inside the class, but when its used somewhere in the code, a 20 arg constructor is pretty unweildy. Say I wanted most of these values to have sensible defaults, but still be immutable and gain the advantage of a record type. Something usually solved with the builder pattern, but wondering if this could be applied in some way to simplify that.
There isn't much you can do in that case. You can't use the compact constructor to delegate to another constructor so setting defaults that way isn't possible. If you need something like that just use a regular class and use records where possible.
@@domincwild One approach is to write custom constructors that delegate to the canonical constructor with default values. And if you need something more like a builder, there are third party libraries that provide builder functionality for Java Records, but you can write your own "wither" if you want: For example: record Point3D(int x, int y, int z) { Point3D() { this(0,0,0); } public Point3D withX(int newX) { return new Point3D(newX, y, z); } } var point = new Point3D(); point = point.withX(5);
Looks good in theory but when record has a lot of properties then I can imagine its not great to read and write when using the constructor, in those cases a standard class with a lombok builder would be a more readable approach.
yes please, i’d love a tutorial about records with springdata
Records can be used for defining DTO's but understanding how DTO's are manged in the persistence context will help in understanding how records can be used with springdata. This video is a thorough explanation on that:
th-cam.com/video/SyP258VgzSs/w-d-xo.html
Would be great to see how we can use records in Spring Data
I will work on a tutorial for that. If you don't want to wait check out Spring Data JDBC, you won't be able to use them with JPA.
@@DanVega i looked into spring data jdbc, found a tutorial and the guy said “it’s preferred to work with immutable classes”. is that the reason why it works with records?
Spring Dat JPA requires no args constructor and setters. record does not provide it
I was using Kotlin mainly because I love how simple it is to make data classes but now I am just finding out a very similar thing is in Java now?? Let’s go!
nice presentation with clear understanding. Please provide next with Spring data
Will do, thanks for watching!
Fantastic. Please do the JPA!
You can’t use records with JPA
Great content, nice explanation, keep going 💪🏻
Hey Dan, what plugin are you using to be able to get those intellisense auto completion suggestions ?
I think that is Github copilot
can we use records for request and response body in spring-rest?
Hi! What is the Intellij plugin that helps you to complete code? It is suggesting in grey.
GitHub Copilot
++ Spring Boot Latest update, Dan you are awesome keep sharing the best of you.
so how do java records and spring data fit together?
What about classes which have a lot of properties? This is nice for small data classes, but what if it has something like 20 different properties that must be set?
It will work for those as well. I have IntelliJ automatically put each argument on a new line for readability.
@@DanVega That's good. Although I'm mainly thinking about usage. So it would be readable inside the class, but when its used somewhere in the code, a 20 arg constructor is pretty unweildy. Say I wanted most of these values to have sensible defaults, but still be immutable and gain the advantage of a record type.
Something usually solved with the builder pattern, but wondering if this could be applied in some way to simplify that.
There isn't much you can do in that case. You can't use the compact constructor to delegate to another constructor so setting defaults that way isn't possible. If you need something like that just use a regular class and use records where possible.
@@domincwild One approach is to write custom constructors that delegate to the canonical constructor with default values. And if you need something more like a builder, there are third party libraries that provide builder functionality for Java Records, but you can write your own "wither" if you want:
For example:
record Point3D(int x, int y, int z) {
Point3D() {
this(0,0,0);
}
public Point3D withX(int newX) {
return new Point3D(newX, y, z);
}
}
var point = new Point3D();
point = point.withX(5);
Just keep it in this way :-)
Looks good in theory but when record has a lot of properties then I can imagine its not great to read and write when using the constructor, in those cases a standard class with a lombok builder would be a more readable approach.
I still feel scared that somehow there's a jpa bug waiting around somewhere when I use record...
👍
9th.. Thanks.
Hm... Hm... Case class?
Hello Dan, while using records can I annotate, Json annotations, @Column annotations as I would one a normal class