How to check if an object is connected to another in sleep mode

Imagine two classes of domain objects, Aand B. Ahas a one-to-many bidirectional relationship to B. Arefers to thousands B. Relations should be unique, it is impossible to duplicate.

To verify that the instance is Balready connected to this instance A, we could perform a simple one INNER JOIN, but this will only provide the stored relationship.

What about the current transitional relationship?

class A {
   @OneToMany
   private List<B> listOfB;
}

If we turn to listOfBand perform a check contains(), this will extract all related instances Bthat are lazy from the data source. I just want to check them by their primary key.

Is there a simple solution where I can do things like "Is this instance associated Awith this instance B?" without loading all this data into memory and doing a collection search?

+3
source share
4 answers

Thanks for all the answers. This lazy collection did the trick for me. I configured the connection @OneToManywith annotation LazyCollection.

@IndexColumn(name = "index", base = 1)
@LazyCollection(LazyCollectionOption.EXTRA)

Hibernate: Extra-lazy builds helped me do this. When you use this option #size(), #contains(), #get()etc. Do not start collection initialization.

+2
source

, . B,  B listOfB . .

B , :

select count(*) from B b where b.a.id = :aId

, , A B.

+1

Thinking about the last paragraph of your question a little more, since the association is bidirectional, I would search for your specific instance of B with the association with A.

0
source

Why can't you perform INNER JOIN?

Using the default mode for stealth mode, Hibernate will clear the session before executing the request, so unsaved collection items will not be a problem.

0
source

Source: https://habr.com/ru/post/1783908/


All Articles