Howto check if a object is connected to another in hibernate
Posted
by
codevourer
on Stack Overflow
See other posts from Stack Overflow
or by codevourer
Published on 2011-01-06T17:49:28Z
Indexed on
2011/01/06
21:54 UTC
Read the original article
Hit count: 212
Imagine two domain object classes, A
and B
. A
has a bidirectional one-to-many relationship to B
. A
is related to thousands of B
. The relations must be unique, it's not possible to have a duplicate.
To check if an instance of B
is already connected to a given instance of A
, we could perform an easy INNER JOIN
but this will only ensure the already persisted relations.
What about the current transient relations?
class A {
@OneToMany
private List<B> listOfB;
}
If we access the listOfB
and perform a check of contains()
this will fetch all the connected instances of B
lazy from the datasource. I only want to validate them by their primary-key.
Is there an easy solution where I can do things like "Does this instance of A
is connected with this instance of B
?" Without loading all these data into memory and perform a based on collections?
© Stack Overflow or respective owner