»º´æÊǹØÓÚÓ¦ÓóÌÐòÐÔÄܵÄÓÅ»¯£¬½µµÍÁËÓ¦ÓóÌÐò¶ÔÎïÀíÊý¾ÝÔ´·ÃÎʵÄƵ´Î£¬´Ó¶øÌá¸ßÓ¦ÓóÌÐòµÄÔËÐÐÐÔÄÜ¡£
»º´æ¶Ô Hibernate À´ËµÒ²ÊǷdz£ÖØÒªµÄ£¬¶øHibernateµÄÒ»¼¶»º´æÓÖ½Ð×ösession»º´æºÍÊÂÎñ¼¶µÄ»º´æ¡£
ÆäÌص㣺
Ø Ö»»º´æʵÌå¡£
Ø ÉúÃüÖÜÆÚºÍsessionÒ»Ö¡£
Ø ²»ÄܶÔÆä½øÐйÜÀí¡£
Ø ²»ÓÃÏÔʾµÄµ÷Óá£
Ø ÊÇÒ»ÖÖÇ¿ÖÆÐԵĻº´æ£¬Session±Ø¶¨»áÖ´ÐС£
µ±Ó¦ÓóÌÐòµ÷ÓÃSessionµÄsave()¡¢update()¡¢saveOrUpdate()¡¢get()»òload()£¬ÒÔ¼°µ÷Óòéѯ½Ó¿ÚµÄ list()¡¢iterate()»òfilter()·½·¨Ê±£¬Èç¹ûÔÚSession»º´æÖл¹²»´æÔÚÏàÓ¦µÄ¶ÔÏó£¬Hibernate¾Í»á°Ñ¸Ã¶ÔÏó¼ÓÈëµ½µÚÒ»¼¶»º´æÖС£
µ±ÇåÀí»º´æʱ£¬Hibernate»á¸ù¾Ý»º´æÖжÔÏóµÄ״̬±ä»¯À´Í¬²½¸üÐÂÊý¾Ý¿â¡£
SessionΪӦÓóÌÐòÌṩÁËÁ½¸ö¹ÜÀí»º´æµÄ·½·¨£º
Ø evict(Object obj)£º´Ó»º´æÖÐÇå³ý²ÎÊýÖ¸¶¨µÄ³Ö¾Ã»¯¶ÔÏó¡£
Ø clear()£ºÇå¿Õ»º´æÖÐËùÓг־û¯¶ÔÏó¡£
ͨ¹ýÒ»ÕÅͼ£¬À´¿´¿´HibernateµÄÒ»¼¶»º´æ£¬ÊÇÔõô¸öÖ´ÐÐÁ÷³Ì£º
ÓÐͼÓÐÕæÏ࣬µ±ÎÒÃǵÚ1´Î²éѯµÄʱºò£¬Session»º´æÖÐÊÇûÓÐÊý¾ÝµÄ£¬µ±Session²Ù×÷Íê³É£¬²éѯµ½Êý¾ÝÉÏ£¬»á°Ñ²éѯµ½µÄ½á¹û£¬»º´æµ½ÄÚ´æÖС£µÚ2´Î¼°ÒÔºóµÄ²Ù×÷£¬Session»áÏȵ½ÄÚ´æÖÐȥƥÅäÏàÓ¦µÄÊý¾Ý£¬Èç¹ûûÓУ¬¾ÍÖ´ÐÐеIJéѯ²Ù×÷£¬Èç¹ûÓÐÆ¥Åäµ½Êý¾Ý£¬Ö±½Ó·µ»Ø¡£
½ÓÏÂÀ´Í¨¹ýÀ´Ò»¸ö¼òµ¥µÄʾÀý½øÐÐÑéÖ¤Ò»²¨£º
packagecom.edu118.test;
importorg.hibernate.Session; importorg.junit.Test; importcom.edu118.domain.Customer; importcom.edu118.utils.HibernateUtil;
/** * ÑÝʾHibernateµÄ3ÖÖ״̬ * <br>ÐÅÓ¯´ï¿Æ¼¼ 2017Äê5ÔÂ6ÈÕ ÏÂÎç2:52:19 * @authormuyu */ public classHibernateState_Demo1 { @Test public void test1() throwsException { Session sessgion =HibernateUtil.getSessgion();
Customer customer = sessgion.get(Customer.class, 4); System.out.println("µÚ1´Î²éѯid = 4µÄ¿Í»§Êý¾Ý£¬¶ÔÏóµØַΪ£º" + customer.hashCode());
Customer customer1 = sessgion.get(Customer.class, 4); System.out.println("µÚ2´Î²éѯid = 4µÄ¿Í»§Êý¾Ý£¬¶ÔÏóµØַΪ£º" + customer1.hashCode());
sessgion.close(); } } |
ʾÀý·Ç³£¼òµ¥£¬Öظ´È¥²éѯͬһ¸ö¶ÔÏó£¬Ö»Òª»ñÈ¡µ½µÄ¶ÔÏóÄÚ´æµØÖ·ÛúÊÇÒ»ÑùµÄ£¬¾Í˵Ã÷ÊÇͬһ¸ö¶ÔÏó£¬ÔÙÕߣ¬´ÓÊý¾Ý¿âÖвéѯÊý¾Ý£¬¿ÉÒÔ¹Û²ìSQLÓï¾äµÄÖ´ÐдÎÊý£¬Èç¹ûÖ»Ö´ÐÐÒ»´Î£¬Í¬Ñù»¹ÊÇ˵Ã÷ÁË»º´æµÄ×÷Óá£
¿´½á¹û£ºINFO: HHH10001501: Connection obtained from JdbcConnectionAccess[org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@5149f008]for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'localtransaction' will be committed and the Connection will be set into auto-commitmode. Hibernate: select customer0_.cid as cid1_0_0_, customer0_.c_name as c_name2_0_0_, customer0_.c_age as c_age3_0_0_, customer0_.c_money as c_money4_0_0_ from t_customercustomer0_ where customer0_.cid=? µÚ1´Î²éѯid = 4µÄ¿Í»§Êý¾Ý£¬¶ÔÏóµØַΪ£º623051334 µÚ2´Î²éѯid = 4µÄ¿Í»§Êý¾Ý£¬¶ÔÏóµØַΪ£º623051334 |
´Ó½á¹ûÉÏÀ´¿´£¬SQLÓï¾ä£¬Ö»Ö´ÐÐÁËÒ»´Î£¬¶øÁ½´Î²éѯ£¬»ñÈ¡µ½µÄ¶ÔÏó£¬ÔÚÄÚ´æÖеĵØÖ·ÊÇͬһ¸ö£¬Õâ¾Í˵Ã÷ÁË£¬HibernateÖУ¬SessionÒ»¼¶»º´æÊÇ´æÔڵģ¬²¢ÇÒÊÇÇ¿ÖÆÖ´Ðеġ£
ÕâÊÇ»º´æµÄÓ¦Óà ¡£½ÓÏÂÀ´£¬À´¿´¿´£¬HibernateÖеĿìÕÕ¡£
ÔÚ˵¿ìÕÕ֮ǰ£¬ÏÈÀ´¿´Ò»¸öʾÀý¡£
²Ù×÷Ç°µÄÊý¾Ý±íÊý¾Ý£º
ʾÀý£º
packagecom.edu118.test;
importorg.hibernate.Session; importorg.hibernate.Transaction; importorg.junit.Test; importcom.edu118.domain.Customer; importcom.edu118.utils.HibernateUtil;
/** * ÑÝʾHibernateµÄ¿ìÕÕÑÝʾ * <br>ÐÅÓ¯´ï¿Æ¼¼ 2017Äê5ÔÂ6ÈÕ ÏÂÎç2:52:19 * @authormuyu */ public classDataMirror_Demo { @Test public void test1() throwsException { Session sessgion =HibernateUtil.getSessgion(); Transaction tx = sessgion.beginTransaction(); //²éѯid=4µÄ¿Í»§ÐÅÏ¢ Customer customer = sessgion.get(Customer.class,4); System.out.println("²éѯµ½µÄ¿Í»§Êý¾Ý" + customer);
//Ð޸IJéѯµ½µÄ¿Í»§Êý¾Ý£¬°ÑÃû³ÆÐÞ¸ÄΪedu118 customer.setName("edu118");
//×¢Òâ¿´ÕâÀ°ÑÐÞ¸ÄÓû§Êý¾ÝµÄ·½·¨¸ø×¢ÊÍÁË£¡£¡£¡£¡ //sessgion.update(customer);
System.out.println("Ð޸ĺóµÄ¿Í»§Êý¾Ý" + customer);
tx.commit(); sessgion.close(); } } |
×¢ÒâÁË£¬ÕâÀïÎÒÃÇûÓе÷ÓÃSessionµÄÐÞ¸ÄÊý¾ÝµÄ·½·¨¡£
²é¿´Ö´ÐеĽá¹û£º
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess[org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@5149f008]for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'localtransaction' will be committed and the Connection will be set into auto-commitmode. Hibernate: select customer0_.cid as cid1_0_0_, customer0_.c_name as c_name2_0_0_, customer0_.c_age as c_age3_0_0_, customer0_.c_moneyas c_money4_0_0_ from t_customercustomer0_ where customer0_.cid=? Hibernate: select orders0_.cust_id as cust_id6_1_0_, orders0_.oid as oid1_1_0_, orders0_.oid as oid1_1_1_, orders0_.orderNum as orderNum2_1_1_, orders0_.productName as productN3_1_1_, orders0_.price as price4_1_1_, orders0_.count as count5_1_1_, orders0_.cust_id as cust_id6_1_1_ from t_orderorders0_ where orders0_.cust_id=? ²éѯµ½µÄ¿Í»§Êý¾ÝCustomer [cid=4,name=xyd, age=9, money=9999, orders=[]] Hibernate: update t_customer set c_name=?, c_age=?, c_money=? where cid=? Ð޸ĺóµÄ¿Í»§Êý¾ÝCustomer [cid=4,name=edu118, age=9, money=9999, orders=[]] |
¿´Êý¾Ý±í£º
ÉñÆ棬ûÓе÷ÓÃÐÞ¸ÄÊý¾ÝµÄSession·½·¨£¬×îÖյĽá¹û£¬È´ÊÇÖ´ÐÐÁËÒ»´Îupdate()·½·¨£¬ÐÞ¸ÄÁËÊý¾Ý¿âÖеÄÊý¾Ý£¬ÕâÊÇΪʲô£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿
ÔÚHibernateÖУ¬¶ÔÏóÓÐÈýÖÖ״̬£¬·Ö±ðÊÇÁÙʱ̬£¬³Ö¾Ã̬ºÍÓÎÀë̬¡£
Õâ3ÖÖ״̬ÖУ¬×îÖØÒªµÄÊdz־Ã̬£¬²»µ±µÄ²Ù×÷£¬ÓпÉÄÜ»áÔì³ÉÊý¾Ý¿âÖеÄÊý¾Ý·¢Éú¸Ä±ä¡£
ÕâÊÇÒòΪ£¬HibernateµÄÒ»¼¶»º´æÖУ¬Session³ýÁË°ÑÊý¾Ý»º´æµ½ÄÚ´æÕâÒ»¸ö²Ù×÷Ö®ÎÒ£¬»¹×öÁËÁíÒ»¸ö²Ù×÷£¬¶øÎÒÃÇûÓе÷ÓÃÐÞ¸ÄÊý¾ÝµÄupdate()·½·¨£¬½á¹ûÊý¾ÝÈ´·¢ÉúÁ˸ı䣬¾ÍÊÇÒòΪÕâ¸ö²Ù×÷ÒýÆðµÄ¡£
Õâ¸ö²Ù×÷£¬¾Í½Ð×ö¿ìÕÕ¡£
Éú»îÖеĿìÕÕ£¬ÊÇÅÄÏÂÒ»ÕÅÕÕƬºó£¬¿ìËٵĴòÓ¡Ò²À´£¬Áô×÷¼ÍÄî»òÕ߰칫ʹÓá£
¶øÔÚHibernateÖУ¬¿ìËÙ¾ÍÊǰѲéѯµ½µÄÊý¾Ý½øÐÐÒ»´Î±¸·ÝÒ»£¬Ïȷŵ½ÄÚ´æÖУ¬Ö÷Òª×÷ÓÃÊÇÓÃÀ´ºÍÒ»¼¶»º´æµÄÊý¾Ý½øÐжԱȣ¬·½±ã¶Ô³Ö¾Ã̬µÄ¶ÔÏóÊý¾Ý½øÐÐά»¤¡£
Ò²¾ÍÊÇ˵£¬µ±Session²éѯÊý¾ÝÖ®ºó£¬»á°ÑÕâ¸ö½á¹û£¬»º´æµ½ÄÚ´æÖеÄÁ½¿é²»Í¬ÇøÓò£¬Ã¿¸öÇøÓò¶¼Óв»Í¬µÄ×÷Óãº
1. Ò»¼¶»º´æÇø£¬Ö÷ÒªÊÇ´æ´¢Êý¾Ý£¬·½±ãºóÃæµÄ²éѯÖÐÄÜ¿ìËٵĻñÈ¡µ½Êý¾Ý¡£
2. ¿ìÕÕÇøÓò£¬×¨ÃÅÊÇÓÃÀ´ÓëÒ»¼¶»º´æÇøÖеÄÊý¾Ý½øÐбȶԣ¬ÅжÏÊý¾ÝÊÇ·ñÓëÊý¾Ý¿âÖеÄÊý¾ÝÒ»Ö¡£
¿´Í¼Ëµ»°£º´ÓÉÏÃæµÄͼ¿ÉÒÔ·¢ÏÖ£¬SessionÔÚget()µ½Êý¾ÝµÄʱºò£¬»á°ÑÊý¾Ý·Ö±ð±£´æµ½Á½¸öÄÚ´æÇøÓò£¬µ±ÎÒÃÇÐÞ¸ÄÁËÄÚ´æÖеÄÊý¾Ýºó£¬Ìá½»ÊÂÎñ£¬´ËʱHibernate»á°ÑÒ»¼¶»º´æÖеÄÊý¾Ý£¬ºÍÊý¾Ý¿âÖеı¸·ÝÊý¾Ý½øÐбȽϣ¬Èç¹û²»Ò»Ö£¬Hibernate»áÖ´ÐÐupdate()£¬ÐÞ¸ÄÊý¾Ý¿âÖеÄÊý¾Ý¡£
µ±Êý¾Ý¿âÖеÄÊý¾ÝÐÞ¸ÄÍê³ÉÖ®ºó£¬»á°Ñ¿ìÕÕÇøµÄÊý¾ÝÒ²½øÐÐÐ޸ģ¬Ö»ÎªÁ˱£Ö¤Êý¾Ý¿âÖеÄÊý¾ÝÓ뻺´æÖеÄÊý¾ÝÒ»Ö£¬±£³Öͬ²½¡£
ÕâÊÇHibernate¶ÔÓÚÊý¾Ý»º´æµÄÒ»ÖÖÓ¦ÓûúÖÆ£¬±ØÐèÒª×ñÑ£¬Ê¹ÓõÄʱºòÒ»¶¨Òª×¢Òâ¡£
ÓеÄʱºòÐèÒªÁÙʱÐÞ¸ÄÊý¾Ý£¬µ«ÓÖ²»Ïë°ÑÊý¾Ý¿âÖеÄÊý¾ÝÒ»ÆðÐ޸ģ¬¿ÉÒÔʹÓÃSessionµÄ evict(Objectobj)ºÍclear()Á½¸ö·½·¨£¬°ÑÒ»¼¶»º´æÖеÄÊý¾ÝÇå³ý£¬Òª×¢ÒâµÄ£¬clear()ÊÇÇå³ýËùÓлº´æ¶ÔÏóÊý¾Ý¡£
°Ñ²»ÐèÒªÔÙ´Îͬ²½µÄ¶ÔÏ󻺴æÇå³ý£¬ÔÙÐ޸ĺÍÌá½»ÊÂÎñ£¬¾Í²»»áÓ°Ïìµ½Êý¾Ý¿âÖеÄÊý¾ÝÁË¡£
END£¡£¡£¡