Wpis z mikrobloga

Mam pytanie odnośnie tego jak działa Lazy Loading.
Załóżmy że mamy encje User i Address, w relacji 1 do 1.

Conceptually, lazy loading with proxies only makes sense if Hibernate knows

that there is a linked shippingAddress. If the property were nullable, Hibernate

would have to check in the database whether the property value is NULL, by querying

the ADDRESS table. If you have to check the database, you might as well load

the value right away, because there would be no benefit in using a proxy.


Dlaczego Address nie może być null? W momencie wywołania user.getAddress() zostanie wygenerowany np taki sql
select * from Address where userId = 1 , co stoi na przeszkodzie żeby to zwróciło null?

Taki sam przykład tu:
https://developer.jboss.org/wiki/SomeExplanationsOnLazyLoadingone-to-one

#programowanie #naukaprogramowania #hibernate #bazydanych
  • 9
@Vetinari: nie wiem czy jest tam null ale co to za różnica czy zapytanie zwróci null czy właściwy adres?
Przy ładowaniu obiektu user address jest ustawiany na null, a kiedy zostanie wywołane
user.getAddress() to zostanie wykonany sql select * from Address where userId = 1 i zostanie zwrócony albo null albo właściwy adres, czegoś tu nie rozumiem ( ͡° ʖ̯ ͡°)
@Godziu73: Dobra. Inaczej:

Right after loading B, you may call getCee() to obtain C. But look, getCee() is a method of YOUR class and Hibernate has no control over it. Hibernate does not know when someone is going to call getCee(). That means Hibernate must put an appropriate value into "cee" property at the moment it loads B from database.


Więc w encji (klasie User) masz pole Address. To pole wypełnia
@Vetinari: czyli jest to spowodowane tym że w przypadku kolekcji, Hibernate tworząc proxy używa interfejsu Set, a gdy mamy swoją klasę w one to one, nie jest to możliwe (brak takiego interfejsu), tak?
@Vetinari: kurcze, jeszcze jednej rzeczy nie rozumiem z tego przykładu ( ͡° ʖ̯ ͡°)

getCee() is a method of YOUR class and Hibernate has no control over it


ale przecież getBees() powyżej też jest metodą w mojej klasie, ale tam już ma kontrolę nad tym. Czy jest to związane z tym że działa na interfejsie Set?
@Godziu73: Tak, chodzi o to, że klasa Set pochodzi z Hibernate, więc masz 2 możliwości:

Twoja metoda GetB:

GetB()
{
return b;
}

Wersja tego samego w klasie Set z Hibernate:

Set::GetBees() {
if (IsLoaded(this)
{
return bees;
}
else
{
bees = LoadB();
return bees;
}
@Vetinari: dalej tego nie rozumiem xD

Da się przecież zrobić proxy na klasie, tak jak jest napisane dalej

If proxy is enabled for C, Hibernate can put a C-proxy object which is not loaded yet, but will be loaded when someone uses it. This gives lazy loading for one-to-one.


Ten fragment jest dla mnie niezrozumiały

Hibernate must set correct value of "cee" at the moment it set B (because it does