JPA with Hibernateã¨MyBatisã®ã©ã¡ããæ¡ç¨ãã¹ãã
ä¼ç¤¾ã§æ¸ããã¡ã¢ãè²¼ãã
å²ã¨ã¾ããã«æ¸ãããã©ãããããããã¨ã¯ãããããé·ç¶ãããªãããã
ä¸ã®æç« è¦ã¦æã£ããã¨ã¯Javaå´ã§ãã¼ã¿æä½ãããªã£ã¦æ¸ãããã©ãã¹ãã¢ãããã·ã¼ã¸ã£ã使ãã£ã¦ãã¨ã§ã¯ãªãã®ã§ãMyBatisã§ã¹ãã¢ãããã·ã¼ã¸ã£ã¯ãã¾ã使ããªãæ¹ãããã¨æãã移æ¤æ§æªããªãããã
ãã¨ãããã¯ä¸ã¤ã®æè¦ãªã®ã§å¥ã«Hibernateãé¸ãã§ãããã¨æãããããã
æåã«çµè«ãè¨ãã¨ãç§ã¯MyBatisãæ¡ç¨ãã¹ãã ã¨èãã¦ãã¾ãããã¡ããcase by caseãªã®ã§ã©ã®ãããªå ´åã«ãMyBatisãè¯ãããã§ã¯ããã¾ããããã ãããã¦ãã®å ´åã¯MyBatisãé¸ã¶æ¹ãè¯ãã¨èãã¾ãã
MyBatisãé¸ã¶çç±ã¯2ã¤ã§ãã
- ã»ã¼çã®SQLãè¨è¿°ã§ããéææ§ããã
- Hibernateãé·æéç¨ããéã®ãã¡ãªãããã¡ãªãããä¸åã
è¦ããã«Hibernateããã¡ã ããã§ãã
ã§ã¯ããªãHibernateããã¡ãã¨è¨ãã¨ä¸è¨ã®2ã¤ã®çç±ãããã¾ãã
- èªåçæããããã¼ãã«ãçºè¡ãããSQLããã©ãã¯ããã¯ã¹åããã¦ãã
- å¦ç¿ã³ã¹ããé«ã
éã«è¨ãã¨ããããã®ãã¡ãªããã解決ã§ããã°Hibernateãæ¡ç¨ãã¦ãè¯ãã¨èãã¾ãã
çç±ã®1ã¤ç®ã®ãã©ãã¯ããã¯ã¹åã§ãããçæããããã¼ãã«ã«ãã ããããªããçºè¡ãããSQLãå¤å°åé·ã§ãåé¡ãªããã°æ¡ç¨ãã¦ãè¯ãã¨æãã¾ããåç´ãªãã¼ã¿ããæããªãã¢ããªã±ã¼ã·ã§ã³ã§ããã°Hibernateã使ããã¨ã§çç£æ§ã¯ä¸ããã¾ãã
çç±ã®2ã¤ç®ã®å¦ç¿ã³ã¹ãã§ãããéçºãã¼ã å ã«Hibernateã«ã¤ãã¦çç¥ãã¦ãã人ãããã°åé¡ããã¾ãããçç¥ãã¦ããã¨ã¯ãN+1åé¡ãçºçããããé¢é£ããªããã¦JOINããªãã¦æ¸ããã¼ãã«æ§æã«ãããããªã©ã¨è¨ã人ã®ãã¨ã§ã¯ããã¾ãããé©åã«N+1åé¡ã解決ã§ããã©ãããå ´åã«æãéãã®SQLæãçºè¡ãããããææ¡ãã¦ãããHibernateã§åé¡ãçºçããã¨ãã«é©åã«è§£æ±ºã§ãã人ã®ãã¨ã§ãããããã人ãããªããã°Hibernateãæ¡ç¨ãã¹ãã§ã¯ãªãã¨æãã¾ãã
Hibernateã®ã¡ãªãã
Hibernateã®ã¡ãªããã¨ã¯ä½ã§ãããããããããä¸è¨ã®ãããªãã®ã ã¨æãã¾ãã
1ã¤ç®ã®ã¡ãªããã¯ç¢ºãã«ãã®éãã ã¨æãã¾ãããã ããSQLãç¥ããªãã¦è¯ããã¨è¨ãããã¨ããããããã§ã¯ããã¾ãããSQLãç¥ã£ã¦ããªãã¨Hibernateã¯ä½¿ãããªãã¾ãããåºæ¬çã«JPAã使ãã®ã§ããã°JPQLã使ç¨ãããã¨ã«ãªãã¨æãã¾ãããJPQLã¯SQLã«ä¼¼ã¦ãããããã»ã¨ãã©SQLãæ¸ããããªãã®ã§ãããã¡ããJPQLã使ããªãã§æ¸ãåç´ãªãã¼ã¿ããæã£ã¦ããªãã®ã§ããã°ãJPQLããæ¸ããªãã¦æ¸ãã§ããããã
2ã¤ç®ãHibernateãå©ç¨ããæ大ã®ã¡ãªããã ã¨æãã¾ãããããããã®ã¡ãªããã¯ä»ã®ä»£æ¿æ段ã§ã«ãã¼ãããã¨ãã§ãã¾ããããã¯ãRDBMSã«ä¾åããªãSQLæãæ¸ããã¨ã§ãã
ãããã£ã¦ãSQLã«åå詳ãããæ¹è¨ã使ç¨ããªãSQLæãæ¸ããã®ã§ããã°Hibernateã使ãã¡ãªããã¯ãã¾ããªãã¨æãã¾ãã
人å£æ¯
Hibernateã«ç²¾éãã¦ãã人ã¨SQLã«ç²¾éãã¦ãã人ã®ã©ã¡ããå¤ãã§ãããããæ®éã«èããã°SQLã«ç²¾éãã¦ãã人ã®æ¹ãå¤ãã¨æãã¾ããã¾ããä»å¾ã®ãã£ãªã¢ãã¹ãã«ã¢ãããèããå ´åãHibernateã«ç²¾éããããSQLã«ç²¾éããããã¨ããã°ãSQLã«ç²¾éãããã¨å¤ãã®äººã¯èããã®ã§ã¯ãªãã§ããããã
ã½ããã¦ã§ã¢ã¯ä½ãã°çµããã§ã¯ããã¾ãããä»å¾ä½å¹´éãä¿å®ã»éç¨ãã¦ãããªããã°ãªãã¾ãããä¿å®ã»éç¨ãèããå ´åã人ãå ¥ãæ¿ããç«ã¡æ¿ãããã¦ã確ä¿ãããã人æãå¿ è¦ã§ããHibernateã¨SQLã®ã©ã¡ããä¿å®ã»éç¨ããããã§ããããã
è¨è¨æ¹éã®éã
Hibernateã®ãã¡ãªããã«ãã©ãã¯ããã¯ã¹åã¨ããã®ãããã¾ãããè¯ãè¨ãæ¹ãããã¨æ½è±¡åã§ããããã¯Hibernateã®è¨è¨ææ³ã«è¡¨ãã¦ãã¾ãã
O/R Mapperãè¨è¨ããå ´åéè¦ãªã®ã¯ä½ã解決ããããã§ããHibernateã¯O/Rã®Objectå´ãåªå ãã¦è¨è¨ãã¦ãã¾ããä¸æ¹ãMyBatisã¯Relationalå´ãåªå ãã¦è¨è¨ãã¦ãã¾ãã
Objectå´ãåªå ããè¨è¨ã¨ã¯ãObjectå´ã®è¨è¨ãé¢é£ï¼One to One, One To Many, Many to One, Many to Manyï¼ãæãéè¦ã§ãæ°¸ç¶åã®æ段ã¯ã©ãã§ãè¯ãã¨ãããã¨ã§ããã¤ã¾ããRDBã§ãªãã¦ãããã¡ã¤ã«ã§ãNoSQLã§ãä½ã«ã©ããã£ã¦æ°¸ç¶åãããã¯é¢å¿ãããã¾ãããããã¯ä¸è¦ããã¨è¯ãè¨è¨ã§ãããç¾å®çã«ã¯éãã¾ãããªããªããRDBã®å ´åãããã©ã¼ãã³ã¹ã«å¤§ããå½±é¿ããããã§ããå ã»ã©ã®æ°¸ç¶åæ段ã®ç¡é¢å¿ãã¯ãæªãè¨ãæ¹ãããã¨ãåé·ãªãã¼ãã«æ§æã«ãªããããSQLæãä½åçºè¡ãããããã©ãã§ãè¯ãã¨ãããã¨ã«ãªãã¾ãããã¡ããHibernateã«ã¯åé·ãªãã¼ãã«æ§æãé²ãæ段ãN+1åé¡ã解決ããæ段ãããã¾ããããã®ããã«ã¯Hibernateã«ã¤ãã¦çç¥ããå¿ è¦ãããã¾ãã
ä¸æ¹ãRelationalå´ãåªå ããè¨è¨ã¨ã¯ãDBã®ãã¼ãã«ãããã®è¨è¨ã§ããæ¢ã«åºæ¥ä¸ãã£ããã¼ãã«ã«åããã¦ãã¼ã¿ããªãã¸ã§ã¯ãã«ãããã³ã°ãã¾ãããããã£ã¦ãObjectå´ã®é¢é£ãå¤å°ãããããããããããªãã®ã«ã¯é¢å¿ããªãã¨ãããã¨ã§ãããã¡ããMyBatisã«ãObjectå´ã®é¢é£ã解決ããæ段ãããã¾ãããHibernateã»ã©ç°¡åã«ã¯ãããã³ã°ã§ãã¾ããã
ãã®è¨è¨ææ³ã®éãã«ããã¦ããHibernateã¨MyBatisã®ã©ã¡ããæ¡ç¨ãã¹ãããè¦ãã¦ãã¾ããããã©ã¼ãã³ã¹ãèãããã¼ãã«æ§æããã¡ãã¨å®ç¾©ããè¤éãªSQLæãçºè¡ãããã®ã§ããã°MyBatisã使ã£ãæ¹ãè¯ãã§ããããåç´ãªãã¼ã¿ãæ±ããããã©ã¼ãã³ã¹ãããçç£æ§ãåªå ããã®ã§ããã°Hibernateã®æ¹ãè¯ãã¨æãã¾ãã
ãã ããä»å¾ã®æ¡å¼µæ§ãèããã¨ãã¼ãã«æ§æããã¡ãã¨å®ç¾©ããæ¹ãè¯ãã¨æãã¾ãããã¼ã¿ã¯æ¯ãèãã«æ¯ã¹å¤æ´ã¯å¤ãããã¾ãããã¾ããè¤éãªSQLæãçºè¡ã§ããæ¹ãã¡ãªããã大ããã§ããDBã§ã§ããæä½ã¯DBã§ãããªãã¨RDBãæ¡ç¨ããæå³ãããã¾ãããRDBã使ãã®ã§ããã°Javaå´ï¼ãã¸ãã¹ãã¸ãã¯å´ï¼ã§ãã¼ã¿æä½ãããã®ã¯ã§ããéãé¿ããã¹ãã§ãã
ã¾ã¨ã
æåã«è¿°ã¹ãããã«ç§ã¯åºæ¬çã«MyBatisãæ¡ç¨ãã¹ãã ã¨èãã¦ãã¾ãã
ãã ããcase by caseãªã®ã§ã©ã¡ããéçºã«ããé©ãã¦ããããèããæ¹ãããã¨æãã¾ããããã¦ãä¿å®ã»éç¨ã®é¢ã§ãã©ã¡ããããæå¹ãã©ãããèæ ®ã«å ¥ããã¹ãã§ãã
åèæç®
http://blogs.wankuma.com/oakbow/archive/2010/08/12/192280.aspx
http://tech.a-listers.jp/2011/06/16/orm_is_an_antipattern/
http://s2container.seasar.org/2.4/ja/s2jdbc_abstract.html