Redian新闻
>
Design question --- hibernate
avatar
Design question --- hibernate# Java - 爪哇娇娃
c*n
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?
avatar
r*s
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.

avatar
c*n
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

avatar
t*e
4
Set lazy as the default loading scheme, and apply eager in specific queries
when performance is significant.
avatar
r*s
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?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。