Join queries aren't using same "root" instance #1258
Answered
by
dstepanov
Goldmensch
asked this question in
Q&A
-
@MappedEntity
public record Cart(
@Id @GeneratedValue @Nullable
Long id,
String brand,
@Relation(value = Kind.ONE_TO_MANY, mappedBy = "cart", cascade = Cascade.ALL)
List<CartPart> parts) {} @MappedEntity()
public record CartPart(
@Id @GeneratedValue @Nullable
Long id,
String type,
@Relation(Kind.MANY_TO_ONE) @Nullable
Cart cart
) {} @JdbcRepository
public interface TestRepo extends CrudRepository<Cart, Long> {
@Override
@Join(value = "parts")
Optional<Cart> findById(@Id @Nullable Long id);
} So when I fetch a Cart via test: var cart = new Cart(null, "VW", List.of(
new CartPart(null, "door", null),
new CartPart(null, "motor", null)));
var saved = repo.save(cart);
var foundCart = repo.findById(saved.id()).orElseThrow();
System.out.println(foundCart);
System.out.println(foundCart.parts().get(0).cart());
System.out.println(System.identityHashCode(foundCart));
System.out.println(System.identityHashCode(foundCart.parts().get(0)));
System.out.println(System.identityHashCode(foundCart.parts().get(1))); output:
|
Beta Was this translation helpful? Give feedback.
Answered by
dstepanov
Jan 10, 2022
Replies: 1 comment 3 replies
-
You should probably post the output of:
When using immutable records you cannot simply modify the parent and have the children reference the updated parent. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Goldmensch
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should probably post the output of:
When using immutable records you cannot simply modify the parent and have the children reference the updated parent.