c******n 发帖数: 4965 | 1 my model has 2 entities ,
A , B , related 1-1.
normally I do
A a = MyDaoForA.getA() ;
// do something with a
but sometimes right after the above code, I do
B b = a.getB();
so if in the first getA(), I eagerly fetch B too, I would save an
extra SELECT; but in cases where I don't run the latter B, it's a waste to
pull in too many entities through outer JOIN.
so MyDaoForA.getA() should have 2 versions, or even multiple versions, if I
have further C , D associated .....
how do you take care of this? | r*****s 发帖数: 985 | 2 that's exactly what your MyDaoForA is for
instead of annotating entities with "EAGER" fetching;
overloading the methods or put a flag parameter there;
otherwise you don't even need a MyDAOForA,
a baseDAO suffices.
to
【在 c******n 的大作中提到】 : my model has 2 entities , : A , B , related 1-1. : normally I do : A a = MyDaoForA.getA() ; : // do something with a : but sometimes right after the above code, I do : B b = a.getB(); : so if in the first getA(), I eagerly fetch B too, I would save an : extra SELECT; but in cases where I don't run the latter B, it's a waste to : pull in too many entities through outer JOIN.
| c******n 发帖数: 4965 | 3 sorry you mean MyDaoForA().getA() should load both A and B?
then in use cases where I need A only, it will be a waste, how to deal with
that?
【在 r*****s 的大作中提到】 : that's exactly what your MyDaoForA is for : instead of annotating entities with "EAGER" fetching; : overloading the methods or put a flag parameter there; : otherwise you don't even need a MyDAOForA, : a baseDAO suffices. : : to
| t*******e 发帖数: 684 | 4 Set lazy as the default loading scheme, and apply eager in specific queries
when performance is significant. | r*****s 发帖数: 985 | 5 Maybe I don't fully get your question:
* where you need A only use MyDAOForA.getA()
* where you need A and B use MyDAOForA.getAandB()
or something like MyDAOForA.getA(Boolean doIneedB).
doesn't it solve your problem?
Do NOT put B in A as "Eager" in entity def;
instead set the fetch mode as EAGER in getAandB() if necessary,
or call .fetch(B) in your DAO impl.
with
【在 c******n 的大作中提到】 : sorry you mean MyDaoForA().getA() should load both A and B? : then in use cases where I need A only, it will be a waste, how to deal with : that?
|
|