Records and bi-directional relationships
Gunnar Morling
gunnar at hibernate.org
Sat Dec 14 21:58:03 UTC 2019
Hi all,
I was exploring the records feature in the latest 14 EA build a bit. An
interesting question came up in how records will work for
bi-directional relationships between two record types.
I found no way to instantiate such pair of records without creating a
temporary instance of one of the two (unlike with regular classes, where I
could wire the relationship from within the constructor of one). Did I miss
an alternative way to do so (see below for what I came up with)?
A related question: this program will raise a StackOverflowError in the
toString() method. Is this an accepted limitation?
Thanks,
--Gunnar
public class RecordsBidi {
public static void main (String... args) {
Surface surface = new Surface(new Point(1, 2));
System.out.println(surface);
}
record Point(int x, int y, Surface owning) {
public Point(int x, int y) {
this(x, y, null);
}
}
record Surface(Point center) {
public Surface(Point center) {
this.center = new Point(center.x, center.y, this);
}
}
}
More information about the amber-dev
mailing list