亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

Designing “query by criteria”(hibernate 2.1)

2019-11-18 13:56:59
字體:
來源:轉載
供稿:網友

  Designing "query by criteria" 01. Dec 2003, 14:47
by gavin@hibernate.org Link 16 comments
One of the great imPRovements in Hibernate 2.1 is that we finally have a mature Criteria query API. For a very long time I let this feature languish because I just wasn´t sure what it should really look like. Every QBC API I´ve looked at is designed differently and there is certainly nothing like a standard API to learn from. I´ve seen everything from this:

new Criteria(Project.class)
.addEq("name", "Hibernate")
.addLike("descr .execute();


to this:

Criteria crit = new Criteria(Project.class)
crit.getProperty("name").eq("Hibernate);
crit.getProperty("description).like("%ORM%");
crit.execute();


I don´t like either of these approaches because the addition of new types of criterion requires the uncontrolled growth of a single central interface (Criteria, in the first case; Property in the second).

I like the second approach even less because it is very difficult to chain method calls. What should the eq() method return? Well, it seems most reasonable that it would return the receiving object (ie. the property). But it is very unusual to apply multiple criteria to the same property! So we would really prefer it to return the Criteria if we wanted to chain method calls. Well, I don´t know about you, but I think that any API that returned the receiver from two calls ago might not be considered "intuitive". So we are stUCk with that evil temp variable.

I´d seriously consider improving this second approach to look like this:

new Criteria(Project.class)
.getProperty("name").eq("Hibernate)
.and()
.getProperty("description).like("%ORM%");
.execute();



Which is actually very clean. Unfortunately, the interfaces themselves are quite bizzare: and() is an
Operation defined by .... Criterion? The and() method returns .... the Criteria? This doesn´t feel like a very natural OO design. And I think it would confuse new users. I´ll come back to another reason why "and" and "or" should not be operations at all.

As a variation upon the first approach, I have seen the following:

new Criteria(Project.class)
.add( new Equals("name", "Hibernate") )
.add( new Like("description", "%ORM%") )
.execute();


This avoids the problem of the Criteria interface growing out of control. But I hate java constructors almost as much as I hate temp variables! The problem with constructors in Java is that they cannot be given meaningful names. We can´t call a constructor EqualsIgnoreCase() if the class is named Equals. Secondly, once we start using constructors, we pretty much permanently nail down the Criterion class hierarchy. We tie client code directly to the concrete classes. I can´t change my mind later and decide that Equals and EqualsIgnoreCase should be different classes.

Eventually I ended up being most influenced by the Cayenne query API (whch I presume was in turn influenced by Apple´s WebObjects). Cayenne uses a class with static factory methods to create Criterion instances. Actually, Cayenne misnames the criterion class EXPression and I stupidly inherited this misnaming in our (Hibernate) factory class. So, we ended up with:

session.createCriteria(Project.class)
.add( Expression.eq("name", "Hibernate") )
.add( Expression.like("description", "%ORM%") )
.list();


Notice that this code does not use any concrete classes other than the static factory class - its all interfaces!

The downside of this design is that there are more characters in "add( Expression.eq())" than in "add( new Eq())" or "addEq()". So it is certainly more verbose. It is also noisy. What stands out in the code above is the two occurrences of "Expression". But they are the least important thing in the code.

Fortunately for me, JDK1.5 will come along and give us static imports. Static imports have been very unfairly maligned in the past, so let me try to set the record straight. If I add "import net.sf.hibernate.expression.Expression.*", the code example above becomes:

session.createCriteria(Project.class)
.add( eq("name", "Hibernate") )
.add( like("description", "%ORM%") )
.list();


This is now less verbose and more readable than the version that used constructors. I´m halfway done.

A second problem is the logical combination of criterions. "and" and "or" are each associative, but a string of both "and"s and "or"s is certainly not associative. So it seemed critically important to me that the precedence of the logical operators is crystal clear from the structure of the code. I hate the following:

session.createCriteria(Project.class)
.addAnd( eq("name", "Hibernate") )
.addAnd( like("description", "%ORM%") )
.addOr( like("description", "%object/relational%") )
.list();


I´ve seen a number of APIs like this and I still don´t have a clue how the previous code is intended to be read. The same problem applies to this variation:

new Criteria(Project.class)
.getProperty("name").eq("Hibernate)
.and()
.getProperty("description).like("%ORM%")
.or()
.getProperty("description).like("%object/relational%")
.execute();


OK, OK, I actually do know that conjunction usually has a higher precendence than disjunction - but I would never, ever write code that depended upon this. It just isn´t readable. And we certainly can´t always rely upon operator precedence - we do need some way to express grouping. Anyway, I think this problem would affect any API which offers and() and or() as methods. So let´s not make and() and or() be operations at all.

By the way, worst of all is this:

new Criteria(Project.class)
.getProperty("name").eq("Hibernate)
.and( crit.getProperty("description).like("%ORM%") )
.execute();


"And" is a symmetrical operation! This symmetry should be obvious.

The solution is to treat Conjunction and Disjunction in exactly the same way as atomic Criterions. Make them Criterions, not operations.

session.createCriteria(Project.class)
.add( Expression.disjunction()
.add( eq("name", "Hibernate") )
.add( like("description", "%ORM%") )
)
.list();


Well, that´s a couple too many parentheses for my taste. I´m considering supporting something like the following in Hibernate:

session.createCriteria(Project.class)
.createDisjunction()
.add( eq("name", "Hibernate") )
.add( like("description", "%ORM%") )
.list();


My big problem here is that createDisjunction() would need to return a new instance of Criteria (wrapping a Disjunction) just so that we can call list() without needing a new temp variable. I´m not sure if I like this. Currently Expression.disjunction() just returns an instance of Disjunction directly - and Disjunction implements only the Criterion interface. I guess we´re still searching for perfection...

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产亚洲欧美日韩精品| 在线观看国产精品91| 国产欧美一区二区三区久久人妖| 久久久国产在线视频| 欧美性极品xxxx做受| 久久精品亚洲热| 久久久噜噜噜久噜久久| 久久精品一本久久99精品| 亚洲欧洲美洲在线综合| 亚州精品天堂中文字幕| 91免费电影网站| 国产精品精品国产| 国产成人av网址| 欧美特级www| 5252色成人免费视频| 久久99久久99精品免观看粉嫩| 国产成人精品日本亚洲专区61| 色视频www在线播放国产成人| 91精品久久久久久久久不口人| 国产精品视频一区二区高潮| 中文字幕日韩高清| 久久久免费av| 久热精品视频在线观看| 1769国内精品视频在线播放| 亚洲国内高清视频| 26uuu另类亚洲欧美日本老年| 在线色欧美三级视频| 成人黄色免费看| 日韩av一区在线| 国产婷婷色综合av蜜臀av| 国产丝袜视频一区| 欧美猛交免费看| 国产成人精品亚洲精品| 性欧美办公室18xxxxhd| 国产99久久精品一区二区| 国产精品都在这里| 欧美在线观看网站| 午夜精品理论片| 成人女保姆的销魂服务| 久久久久五月天| 8050国产精品久久久久久| www.亚洲男人天堂| 亚洲国产三级网| 亚洲第一福利视频| 欧美激情一区二区久久久| 国产成人涩涩涩视频在线观看| 日韩欧美精品免费在线| 国产久一一精品| 中文字幕国产精品| 91色中文字幕| 欧美黄网免费在线观看| 欧美激情日韩图片| 亚洲永久免费观看| 欧美专区福利在线| 啪一啪鲁一鲁2019在线视频| 欧美亚洲另类在线| 亚洲欧美日本精品| 亚洲欧洲自拍偷拍| 色噜噜狠狠狠综合曰曰曰88av| 2019av中文字幕| 国产成人精品优优av| 色噜噜狠狠色综合网图区| 日韩精品在线视频美女| 日韩精品一区二区视频| 国产精品爽爽爽爽爽爽在线观看| 国产精品老牛影院在线观看| 国产亚洲一区二区精品| 亚洲国产婷婷香蕉久久久久久| 久久久久久亚洲精品| 成人福利视频在线观看| 欧美另类99xxxxx| 久久久av一区| 日本精品一区二区三区在线| 精品日韩美女的视频高清| 久久久中精品2020中文| 精品久久在线播放| 欧美在线观看网站| 97在线精品国自产拍中文| 国产精品对白刺激| 不卡伊人av在线播放| 成人性生交xxxxx网站| 国产精品高潮在线| 97免费视频在线| 亚洲人成网站在线播| 色偷偷91综合久久噜噜| 亚洲无限乱码一二三四麻| 91精品久久久久久久久不口人| 亚洲欧美激情一区| 成人精品视频99在线观看免费| 欧美一区深夜视频| 亚洲在线免费看| 538国产精品一区二区免费视频| 97国产真实伦对白精彩视频8| 欧美一区二区三区精品电影| 成人精品在线观看| 国产亚洲美女精品久久久| 欧美成人免费大片| 亚洲高清久久网| 国产综合香蕉五月婷在线| 欧美午夜精品久久久久久久| 亚洲美女视频网站| 精品国产视频在线| 欧美福利小视频| 国产极品jizzhd欧美| 欧美尺度大的性做爰视频| www.xxxx精品| 国产日韩欧美在线看| 欧美黑人一级爽快片淫片高清| 欧美激情手机在线视频| 国产精品第一视频| 亚洲色图色老头| 一本色道久久综合狠狠躁篇的优点| 91亚洲国产成人久久精品网站| 最新亚洲国产精品| 午夜精品国产精品大乳美女| 青青草精品毛片| 亚洲18私人小影院| 国产不卡一区二区在线播放| 日本欧美精品在线| 韩国美女主播一区| 日韩欧美第一页| 国产97色在线| 91中文字幕在线观看| 久久黄色av网站| 亚洲免费一在线| 久久精品99无色码中文字幕| 国产亚洲精品一区二区| 国产成人一区二区三区小说| 欧美一级在线亚洲天堂| 中国china体内裑精亚洲片| 日本中文字幕成人| 91社影院在线观看| 久久久国产精品x99av| 国产亚洲一级高清| 另类图片亚洲另类| 久久久精品中文字幕| 国产美女久久精品香蕉69| 神马久久久久久| 亚洲男人av在线| 国产亚洲欧美日韩一区二区| 日韩视频欧美视频| 欧美丰满少妇xxxxx| 欧美精品免费在线| 精品中文字幕视频| 国产91色在线播放| 亚洲一区二区国产| 国产精品成人国产乱一区| 日韩中文在线中文网在线观看| 亚洲成色999久久网站| 中文字幕久热精品在线视频| 日本一本a高清免费不卡| 久久成人av网站| 成人黄色免费片| 国产精品第100页| 国产精品丝袜久久久久久高清| 欧美xxxx18性欧美| 97成人在线视频| 亚洲片国产一区一级在线观看| 日韩免费av在线| 国产精品成人av性教育| 国产在线一区二区三区| 欧美影院成年免费版| 国产精品91久久| 色综合伊人色综合网|