锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲一卡2卡3卡4卡乱码 在线,亚洲精品国产啊女成拍色拍,亚洲av最新在线观看网址http://m.tkk7.com/sealyu/category/30668.html--- The devil's in the Detailszh-cnMon, 26 Apr 2010 05:47:09 GMTMon, 26 Apr 2010 05:47:09 GMT60Hibernate涓瑿riteria鐨勫畬鏁寸敤娉?杞?http://m.tkk7.com/sealyu/archive/2010/04/24/319238.htmlsealsealFri, 23 Apr 2010 21:55:00 GMThttp://m.tkk7.com/sealyu/archive/2010/04/24/319238.htmlhttp://m.tkk7.com/sealyu/comments/319238.htmlhttp://m.tkk7.com/sealyu/archive/2010/04/24/319238.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/319238.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/319238.html 顥ヮ棩
璁捐涓婂彲浠ョ伒媧葷殑鏍規嵁 Criteria 鐨勭壒鐐規潵鏂逛究鍦拌繘琛屾煡璇㈡潯浠剁殑緇勮銆傜幇鍦ㄥ Hibernate鐨凜riteria 鐨勭敤娉曡繘琛屾葷粨錛?/font>
   Hibernate 璁捐浜?CriteriaSpecification 浣滀負 Criteria 鐨勭埗鎺ュ彛錛屼笅闈㈡彁渚涗簡 Criteria鍜孌etachedCriteria 銆?
   Criteria 鍜?DetachedCriteria 鐨勪富瑕佸尯鍒湪浜庡垱寤虹殑褰㈠紡涓嶄竴鏍鳳紝 Criteria 鏄湪綰跨殑錛屾墍
浠ュ畠鏄敱 Hibernate Session 榪涜鍒涘緩鐨勶紱鑰?DetachedCriteria 鏄綰跨殑錛屽垱寤烘椂鏃犻渶
Session錛孌etachedCriteria 鎻愪緵浜?2 涓潤鎬佹柟娉? forClass(Class) 鎴?forEntityName(Name)
榪涜DetachedCriteria 瀹炰緥鐨勫垱寤恒?Spring 鐨勬鏋舵彁渚涗簡getHibernateTemplate
().findByCriteria(detachedCriteria) 鏂規硶鍙互寰堟柟渚垮湴鏍規嵁DetachedCriteria 鏉ヨ繑鍥炴煡璇㈢粨
鏋溿?
   Criteria 鍜?DetachedCriteria 鍧囧彲浣跨敤 Criterion 鍜?Projection 璁劇疆鏌ヨ鏉′歡銆傚彲浠ヨ

緗?FetchMode( 鑱斿悎鏌ヨ鎶撳彇鐨勬ā寮?) 錛岃緗帓搴忔柟寮忋傚浜? Criteria 榪樺彲浠ヨ緗?FlushModel
錛堝啿鍒?Session 鐨勬柟寮忥級鍜?LockMode 錛堟暟鎹簱閿佹ā寮忥級銆?
涓嬮潰瀵?Criterion 鍜?Projection 榪涜璇︾粏璇存槑銆?/font>
     Criterion 鏄?Criteria 鐨勬煡璇㈡潯浠躲侰riteria 鎻愪緵浜?add(Criterion criterion) 鏂規硶鏉?/font>
娣誨姞鏌ヨ鏉′歡銆?br />      Criterion 鎺ュ彛鐨勪富瑕佸疄鐜板寘鎷細 Example 銆?Junction 鍜?SimpleExpression 銆傝?
Junction 鐨勫疄闄呬嬌鐢ㄦ槸瀹冪殑涓や釜瀛愮被 conjunction 鍜?disjunction 錛屽垎鍒槸浣跨敤 AND 鍜?OR 鎿?/font>
浣滅榪涜鏉ヨ仈 緇撴煡璇㈡潯浠墮泦鍚堛?br />      Criterion 鐨勫疄渚嬪彲浠ラ氳繃 Restrictions 宸ュ叿綾繪潵鍒涘緩錛孯estrictions 鎻愪緵浜嗗ぇ閲忕殑闈欐?/font>
鏂規硶錛屽 eq 錛堢瓑浜庯級銆? ge 錛堝ぇ浜庣瓑浜庯級銆?between 絳夋潵鏂規硶鐨勫垱寤?Criterion 鏌ヨ鏉′歡
錛圫impleExpression 瀹炰緥錛夈傞櫎姝や箣澶栵紝 Restrictions 榪樻彁渚涗簡鏂規硶鏉ュ垱寤? conjunction 鍜?
disjunction 瀹炰緥錛岄氳繃寰璇ュ疄渚嬬殑 add(Criteria) 鏂規硶鏉ュ鍔犳煡璇㈡潯浠跺艦鎴愪竴涓煡璇㈡潯浠墮泦鍚?/font>
銆?br />      鑷充簬 Example 鐨勫垱寤烘湁鎵涓嶅悓錛?Example 鏈韓鎻愪緵浜嗕竴涓潤鎬佹柟娉?create(Object
entity) 錛屽嵆鏍規嵁涓涓璞★紙瀹為檯浣跨敤涓竴鑸槸鏄犲皠鍒版暟鎹簱鐨勫璞★級鏉ュ垱寤恒傜劧鍚庡彲浠ヨ緗竴浜?/font>
榪囨護鏉′歡錛?
Example exampleUser =Example.create(u)
.ignoreCase() // 蹇界暐澶у皬鍐?
.enableLike(MatchMode.ANYWHERE);
// 瀵?String 綾誨瀷鐨勫睘鎬э紝鏃犺鍦ㄩ偅閲屽煎湪閭i噷閮藉尮閰嶃傜浉褰撲簬 %value%

  Project 涓昏鏄 Criteria 鑳藉榪涜鎶ヨ〃鏌ヨ錛屽茍鍙互瀹炵幇鍒嗙粍銆?Project 涓昏鏈?
SimpleProjection 銆?ProjectionList 鍜?Property 涓変釜瀹炵幇銆傚叾涓? SimpleProjection 鍜?
ProjectionList 鐨勫疄渚嬪寲鏄敱鍐呭緩鐨?Projections 鏉ュ畬鎴愶紝濡傛彁渚涚殑 avg 銆?count 銆?max 銆?
min 銆?sum 鍙互璁╁紑鍙戣呭緢瀹規槗瀵規煇涓瓧孌佃繘琛岀粺璁℃煡璇€?
       Property 鏄鏌愪釜瀛楁榪涜鏌ヨ鏉′歡鐨勮緗紝濡傞氳繃Porperty.forName(“color”).in

(new String[]{“black”,”red”,”write”}); 鍒欏彲浠ュ垱寤轟竴涓?Project 瀹炰緥銆傞氳繃
criteria 鐨?add(Project) 鏂規硶鍔犲叆鍒版煡璇㈡潯浠朵腑鍘匯?
    浣跨敤 Criteria 榪涜鏌ヨ錛屼富瑕佽娓呮櫚鐨勬槸 Hibernate 鎻愪緵浜嗛偅浜涚被鍜屾柟娉曟潵婊¤凍寮鍙戜腑鏌?/font>
璇㈡潯浠剁殑鍒涘緩鍜岀粍瑁咃紝涓嬮潰浠嬬粛鍑犵鐢ㄦ硶錛?/font>
1. 鍒涘緩涓涓狢riteria 瀹炰緥
org.hibernate.Criteria 鎺ュ彛琛ㄧず鐗瑰畾鎸佷箙綾葷殑涓涓煡璇€係ession鏄?Criteria瀹炰緥鐨勫伐鍘傘?br /> Criteria crit = sess.createCriteria(Cat.class);
crit.setMaxResults(50);
List cats = crit.list();
 
2. 闄愬埗緇撴灉闆嗗唴瀹?br /> 涓涓崟鐙殑鏌ヨ鏉′歡鏄? org.hibernate.criterion.Criterion 鎺ュ彛鐨勪竴涓疄渚嬨?/font>

org.hibernate.criterion.Restrictions綾? 瀹氫箟浜嗚幏寰楁煇浜涘唴緗瓹riterion綾誨瀷鐨勫伐鍘傛柟娉曘?br /> List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "Fritz%") )
    .add( Restrictions.between("weight", minWeight, maxWeight) )
    .list();


綰︽潫鍙互鎸夐昏緫鍒嗙粍銆?
 
List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "Fritz%") )
    .add( Restrictions.or(
        Restrictions.eq( "age", new Integer(0) ),
        Restrictions.isNull("age")
    ) )
    .list();
 
List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.in( "name", new String[] { "Fritz", "Izi", "Pk" } ) )
    .add( Restrictions.disjunction()
        .add( Restrictions.isNull("age") )
        .add( Restrictions.eq("age", new Integer(0) ) )
        .add( Restrictions.eq("age", new Integer(1) ) )
        .add( Restrictions.eq("age", new Integer(2) ) )
    ) )
    .list();
 
Hibernate鎻愪緵浜嗙浉褰撳鐨勫唴緗甤riterion綾誨瀷(Restrictions 瀛愮被), 浣嗘槸灝ゅ叾鏈夌敤鐨勬槸鍙互鍏佽


浣犵洿鎺ヤ嬌鐢? SQL銆?br />  
List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.sql("lower({alias}.name) like lower(?)", "Fritz%",


Hibernate.STRING) )
    .list();
 
{alias} 鍗犱綅絎﹀簲褰撹鏇挎崲涓鴻鏌ヨ瀹炰綋鐨勫垪鍒悕銆?
Property瀹炰緥鏄幏寰椾竴涓潯浠剁殑鍙﹀涓縐嶉斿緞銆備綘鍙互閫氳繃璋冪敤 Property.forName() 鍒涘緩涓涓?/font>

Property銆?
 
  Property age = Property.forName("age");
List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.disjunction()
        .add( age.isNull() )
        .add( age.eq( new Integer(0) ) )
        .add( age.eq( new Integer(1) ) )
        .add( age.eq( new Integer(2) ) )
    ) )
    .add( Property.forName("name").in( new String[] { "Fritz", "Izi", "Pk" } ) )
    .list();
 
3. 緇撴灉闆嗘帓搴?br /> 浣犲彲浠ヤ嬌鐢╫rg.hibernate.criterion.Order鏉ヤ負鏌ヨ緇撴灉鎺? 搴忋?
 
List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "F%")
    .addOrder( Order.asc("name") )
    .addOrder( Order.desc("age") )
    .setMaxResults(50)
    .list();
 
List cats = sess.createCriteria(Cat.class)
    .add( Property.forName("name").like("F%") )
    .addOrder( Property.forName("name").asc() )
    .addOrder( Property.forName("age").desc() )
    .setMaxResults(50)
    .list();
 
4. 鍏寵仈
浣犲彲浠ヤ嬌鐢╟reateCriteria()闈炲父瀹規槗鐨勫湪浜掔浉鍏寵仈鐨勫疄浣撻棿寤虹珛 綰︽潫銆?br />  
List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "F%")
    .createCriteria("kittens")
        .add( Restrictions.like("name", "F%")
    .list();



娉ㄦ剰絎簩涓?createCriteria()榪斿洖涓涓柊鐨? Criteria瀹炰緥錛岃瀹炰緥寮曠敤kittens 闆嗗悎涓殑鍏冪礌銆?
鎺ヤ笅鏉ワ紝鏇挎崲褰㈡佸湪鏌愪簺鎯呭喌涓嬩篃鏄緢鏈夌敤鐨勩?br />  
List cats = sess.createCriteria(Cat.class)
    .createAlias("kittens", "kt")
    .createAlias("mate", "mt")
    .add( Restrictions.eqProperty("kt.name", "mt.name") )
    .list();



(createAlias()騫朵笉鍒涘緩涓涓柊鐨?Criteria瀹炰緥銆?
Cat瀹炰緥鎵淇濆瓨鐨勪箣鍓嶄袱嬈℃煡璇㈡墍榪斿洖鐨刱ittens闆嗗悎鏄?娌℃湁琚潯浠墮榪囨護鐨勩傚鏋滀綘甯屾湜鍙幏寰?/font>

絎﹀悎鏉′歡鐨刱ittens錛?浣犲繀欏諱嬌鐢╮eturnMaps()銆?
 
List cats = sess.createCriteria(Cat.class)
    .createCriteria("kittens", "kt")
    .add( Restrictions.eq("name", "F%") )
    .returnMaps()
    .list();
Iterator iter = cats.iterator();
while ( iter.hasNext() ) {
    Map map = (Map) iter.next();
    Cat cat = (Cat) map.get(Criteria.ROOT_ALIAS);
    Cat kitten = (Cat) map.get("kt");
}


5. 鍔ㄦ佸叧鑱旀姄鍙?br /> 浣犲彲浠ヤ嬌鐢╯etFetchMode()鍦ㄨ繍琛屾椂瀹氫箟鍔? 鎬佸叧鑱旀姄鍙栫殑璇箟銆?
 
List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "Fritz%") )
    .setFetchMode("mate", FetchMode.EAGER)
    .setFetchMode("kittens", FetchMode.EAGER)
    .list();
 
榪欎釜鏌ヨ鍙互閫氳繃澶栬繛鎺ユ姄鍙杕ate鍜宬ittens銆?br />  
6. 鏌ヨ紺轟緥
org.hibernate.criterion.Example 綾誨厑璁鎬綘閫氳繃涓涓粰瀹氬疄渚?鏋勫緩涓涓潯浠舵煡璇€?br />  
Cat cat = new Cat();
cat.setSex('F');
cat.setColor(Color.BLACK);
List results = session.createCriteria(Cat.class)
    .add( Example.create(cat) )
    .list();



鐗堟湰灞炴с佹爣璇嗙鍜屽叧鑱旇蹇界暐銆傞粯璁ゆ儏鍐典笅鍊間負null鐨勫睘鎬у皢琚帓闄ゃ?
鍙互鑷璋冩暣 Example浣夸箣鏇村疄鐢ㄣ?
 
Example example = Example.create(cat)
    .excludeZeroes()           //exclude zero valued properties
    .excludeProperty("color")  //exclude the property named "color"
    .ignoreCase()              //perform case insensitive string comparisons
    .enableLike();             //use like for string comparisons
List results = session.createCriteria(Cat.class)
    .add(example)
    .list();



鐢氳嚦鍙互浣跨敤 examples鍦ㄥ叧鑱斿璞′笂鏀劇疆鏉′歡銆?br />  
List results = session.createCriteria(Cat.class)
    .add( Example.create(cat) )
    .createCriteria("mate")
        .add( Example.create( cat.getMate() ) )
    .list();



7. 鎶曞獎(Projections)銆佽仛鍚堬紙aggregation錛夊拰鍒嗙粍錛坓rouping錛?br /> org.hibernate.criterion.Projections 鏄?Projection 鐨勫疄渚嬪伐鍘傘傛垜浠氳繃璋冪敤


setProjection()搴旂敤鎶曞獎鍒頒竴涓煡璇€?
 
List results = session.createCriteria(Cat.class)
    .setProjection( Projections.rowCount() )
    .add( Restrictions.eq("color", Color.BLACK) )
    .list();
 
List results = session.createCriteria(Cat.class)
    .setProjection( Projections.projectionList()
        .add( Projections.rowCount() )
        .add( Projections.avg("weight") )
        .add( Projections.max("weight") )
        .add( Projections.groupProperty("color") )
    )
    .list();

顥ヮ棩


鍦ㄤ竴涓潯浠舵煡璇腑娌℃湁蹇呰鏄懼紡鐨勪嬌鐢?"group by" 銆傛煇浜涙姇褰辯被鍨嬪氨鏄瀹氫箟涓?鍒嗙粍鎶曞獎錛屼粬


浠篃鍑虹幇鍦⊿QL鐨刧roup by瀛愬彞涓?

鍙互閫夋嫨鎶婁竴涓埆鍚嶆寚媧劇粰涓涓姇褰憋紝榪欐牱鍙互浣挎姇褰卞艱綰︽潫鎴栨帓搴忔墍寮曠敤銆備笅闈㈡槸涓ょ涓嶅悓鐨?/font>

瀹炵幇鏂瑰紡錛?br />  
List results = session.createCriteria(Cat.class)
    .setProjection( Projections.alias( Projections.groupProperty("color"), "colr" ) )
    .addOrder( Order.asc("colr") )
    .list();


顥ヮ棩 List results = session.createCriteria(Cat.class)
    .setProjection( Projections.groupProperty("color").as("colr") )
    .addOrder( Order.asc("colr") )
    .list();
 
alias()鍜宎s()鏂規硶綆渚跨殑灝嗕竴涓姇褰卞疄渚嬪寘瑁呭埌鍙? 澶栦竴涓?鍒悕鐨凱rojection瀹炰緥涓傜畝鑰岃█涔嬶紝


褰撲綘娣誨姞涓涓姇褰卞埌涓涓姇褰卞垪琛ㄤ腑鏃?浣犲彲浠ヤ負瀹冩寚瀹氫竴涓埆鍚嶏細
 
List results = session.createCriteria(Cat.class)
    .setProjection( Projections.projectionList()
        .add( Projections.rowCount(), "catCountByColor" )
        .add( Projections.avg("weight"), "avgWeight" )
        .add( Projections.max("weight"), "maxWeight" )
        .add( Projections.groupProperty("color"), "color" )
    )
    .addOrder( Order.desc("catCountByColor") )
    .addOrder( Order.desc("avgWeight") )
    .list();



List results = session.createCriteria(Domestic.class, "cat")
    .createAlias("kittens", "kit")
    .setProjection( Projections.projectionList()
        .add( Projections.property("cat.name"), "catName" )
        .add( Projections.property("kit.name"), "kitName" )
    )
    .addOrder( Order.asc("catName") )
    .addOrder( Order.asc("kitName") )
    .list();



涔熷彲浠ヤ嬌鐢? Property.forName()鏉ヨ〃紺烘姇褰憋細
 
List results = session.createCriteria(Cat.class)
    .setProjection( Property.forName("name") )
    .add( Property.forName("color").eq(Color.BLACK) )
    .list();

List results = session.createCriteria(Cat.class)
    .setProjection( Projections.projectionList()
        .add( Projections.rowCount().as("catCountByColor") )
        .add( Property.forName("weight").avg().as("avgWeight") )
        .add( Property.forName("weight").max().as("maxWeight") )
        .add( Property.forName("color").group().as("color" )
    )
    .addOrder( Order.desc("catCountByColor") )
    .addOrder( Order.desc("avgWeight") )
    .list();



8. 紱葷嚎(detached)鏌ヨ鍜屽瓙鏌ヨ
DetachedCriteria 綾諱嬌浣犲湪涓涓猻ession鑼冨洿涔嬪鍒涘緩涓涓煡璇紝騫朵笖鍙互浣跨敤浠繪剰鐨?Session鏉?/font>

鎵ц瀹冦?br />  
DetachedCriteria query = DetachedCriteria.forClass(Cat.class)
    .add( Property.forName("sex").eq('F') );
//鍒涘緩涓涓猄ession
Session session = .;
Transaction txn = session.beginTransaction();
List results = query.getExecutableCriteria(session).setMaxResults(100).list();
txn.commit();
session.close();



DetachedCriteria涔熷彲浠ョ敤浠ヨ〃紺哄瓙鏌ヨ銆傛潯浠跺疄渚嬪寘鍚瓙鏌? 璇㈠彲浠ラ氳繃 Subqueries鎴栬?/font>
Property 鑾峰緱銆?br />  
DetachedCriteria avgWeight = DetachedCriteria.forClass(Cat.class)
    .setProjection( Property.forName("weight").avg() );
session.createCriteria(Cat.class)
    .add( Property.forName("weight).gt(avgWeight) )
    .list();

DetachedCriteria weights = DetachedCriteria.forClass(Cat.class)
    .setProjection( Property.forName("weight") );
session.createCriteria(Cat.class)
    .add( Subqueries.geAll("weight", weights) )
    .list();


鐩鎬簰鍏寵仈鐨勫瓙鏌ヨ涔熸槸鏈夊彲鑳界殑錛?br />  
DetachedCriteria avgWeightForSex = DetachedCriteria.forClass(Cat.class, "cat2")
    .setProjection( Property.forName("weight").avg() )
    .add( Property.forName("cat2.sex").eqProperty("cat.sex") );
session.createCriteria(Cat.class, "cat")
    .add( Property.forName("weight).gt(avgWeightForSex) )
    .list();


seal 2010-04-24 05:55 鍙戣〃璇勮
]]>
illegal attempt to dereference collection(杞?http://m.tkk7.com/sealyu/archive/2010/04/23/319225.htmlsealsealFri, 23 Apr 2010 14:44:00 GMThttp://m.tkk7.com/sealyu/archive/2010/04/23/319225.htmlhttp://m.tkk7.com/sealyu/comments/319225.htmlhttp://m.tkk7.com/sealyu/archive/2010/04/23/319225.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/319225.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/319225.html棣栧厛璇翠竴鍙ワ細鏄増鏈殑闂!

鎴戝啓鐨刪ql涓猴細

from Department as d where d.employees.name='Tom';

榪愯鏃跺嚭鐜板紓甯革細org.hibernate.QueryException: illegal attempt to dereference collection

鏄洜涓猴細鍦ㄤ笂闈㈢殑HQL璇彞 涓紝Department鐨勫叧鑱斿疄浣揺mployees鏄竴涓泦鍚堬紝鑰屼笉鐩存帴鏄竴涓狤mployee瀹炰綋銆?/p>

鍦? Hibernate3.2.2浠ュ墠鐨勭増鏈紝Hibernate浼氬鍏寵仈瀹炰綋鑷姩浣跨敤闅愬紡鐨刬nner join錛?/p>

涔熷氨鏄濡備笅SQL璇彞 涓嶄細鏈変換浣曢棶棰?nbsp;:from Department as d where d.employees.name='Tom';

浠? Hibernate3.2.3浠ュ悗錛孒ibernate鏀瑰彉浜嗚繖縐嶉殣寮忕殑inner join鐨勭瓥鐣?

瀵逛簬濡備笅榪欐潯璇彞錛?/p>

from Department as d where d.employees.name='Tom';

濡傛灉employees鏄櫘閫氱粍浠跺睘 鎬э紝鎴栧崟涓殑鍏寵仈瀹炰綋錛屽垯Hibernate浼氳嚜鍔ㄧ敓鎴愰殣寮忕殑inner join

濡傛灉myEvents鏄篃涓涓泦鍚堬紝閭d箞瀵逛笉璧鳳紒緋? 緇熷皢浼氬嚭鐜?org.hibernate.QueryException: illegal attempt to dereference collection寮傚父銆?br /> 鎹瓾ibernate瀹樻柟璇存硶:
榪欐牱鍙互璁╄繖浣垮緱闅愬惈鍏寵仈鏇村叿紜畾鎬э紙鍘熸枃錛歍his makes implicit joins more deterministic 錛夈?/p>

鎺ㄨ崘榪欐牱鍐欙細

from Department as d inner join fetch d.employees e where e.name='Tom';

seal 2010-04-23 22:44 鍙戣〃璇勮
]]>
getHibernateTemplate()鍜実etSession()鐨勫尯鍒紙杞級http://m.tkk7.com/sealyu/archive/2010/02/22/313642.htmlsealsealMon, 22 Feb 2010 08:43:00 GMThttp://m.tkk7.com/sealyu/archive/2010/02/22/313642.htmlhttp://m.tkk7.com/sealyu/comments/313642.htmlhttp://m.tkk7.com/sealyu/archive/2010/02/22/313642.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/313642.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/313642.html

鍦ㄥ墠涓綃囨棩蹇椾腑鑷姩鐢熸垚hibernate閰嶇疆鏂囦歡鐨勬椂鍊欙紝浼氬湪dao灞傜敤鍒癵etSession()鏂規硶鏉ユ搷浣滄暟鎹簱璁板綍錛屼絾鏄粬榪樻湁涓柟娉昰etHibernateTemplate()錛岃繖涓や釜鏂規硶絀剁珶鏈変粈涔堝尯鍒憿錛?/p>

1.浣跨敤getSession()鏂規硶浣犲彧瑕佺戶鎵縮essionFactory錛岃屼嬌鐢╣etHibernateTemplate()鏂規硶蹇呴』緇ф壙 HibernateDaoSupport褰撶劧鍖呮嫭sessionFactory錛岃繖鐐瑰尯鍒兘涓嶆槸鐗瑰埆閲嶈鐨勶紝涓嬮潰榪欎簺鍖哄埆灝卞緢閲嶈浜?/p>

2.getSession()鏂規硶鏄病鏈夌粡榪噑pring鍖呰鐨勶紝spring浼氭妸鏈鍘熷鐨剆ession緇欎綘錛屽湪浣跨敤瀹屼箣鍚庡繀欏昏嚜宸辮皟鐢ㄧ浉搴旂殑 close鏂規硶錛岃屼笖涔熶笉浼氬澹版槑寮忎簨鍔¤繘琛岀浉搴旂殑綆$悊錛屼竴鏃︽病鏈夊強鏃跺叧闂繛鎺ワ紝灝變細瀵艱嚧鏁版嵁搴撹繛鎺ユ睜鐨勮繛鎺ユ暟婧? 鍑猴紝getHibernateTemplate()鏂規硶鏄粡榪噑pring灝佽鐨勶紝渚嬪娣誨姞鐩稿簲鐨勫0鏄庡紡浜嬪姟綆$悊錛岀敱spring綆$悊鐩稿簲鐨勮繛鎺ャ?/p>

鍦ㄥ疄闄呯殑浣跨敤榪囩▼涓彂鐜扮殑紜甮etHibernateTemplate()姣攇etSession()鏂規硶瑕佸ソ寰堝錛屼絾鏄湁浜涙柟娉曞湪getHibernateTemplate()騫舵病鏈夋彁渚涳紝榪欐椂鎴戜滑鐢℉ibernateCallback 鍥炶皟鐨勬柟娉曠鐞嗘暟鎹簱.

渚嬪濡備笅浠g爜錛?/p>

/**
     * 浣跨敤 hql 璇彞榪涜鎿嶄綔
     * @param hql HSQL 鏌ヨ璇彞錛堜嬌鐢ㄥ洖璋冨嚱鏁拌闂閮ㄥ彉閲忥紝蹇呴』鏄痜inal鐨勶級
     * @param offset 寮濮嬪彇鏁版嵁鐨勪笅鏍?br />     * @param length 璇誨彇鏁版嵁璁板綍鏁?br />     * @return List 緇撴灉闆?br /> */
public List getListForPage ( final String hql , final int offset , final int length ) {

              List list = getHibernateTemplate().executeFind ( new HibernateCallback ( ) {
                            public Object doInHibernate ( Session session ) throws HibernateException, SQLException {
                                            Query query = session.createQuery ( hql ) ;
                                            query.setFirstResult ( offset ) ;
                                            query.setMaxResults ( length ) ;
                                            List list = query.list ( ) ;
                                            return list ;
                           }
               }) ;
               return list ;
}



seal 2010-02-22 16:43 鍙戣〃璇勮
]]>
Spring 鐨?HibernateDaoSupport 綾葷殑 getSession() 瀵艱嚧鐨勮繛鎺ユ硠闇查棶棰橈紙杞級http://m.tkk7.com/sealyu/archive/2010/02/22/313643.htmlsealsealMon, 22 Feb 2010 08:43:00 GMThttp://m.tkk7.com/sealyu/archive/2010/02/22/313643.htmlhttp://m.tkk7.com/sealyu/comments/313643.htmlhttp://m.tkk7.com/sealyu/archive/2010/02/22/313643.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/313643.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/313643.html闃呰鍏ㄦ枃

seal 2010-02-22 16:43 鍙戣〃璇勮
]]>
Hibernate 鐨勯厤緗枃浠舵ā鏉?/title><link>http://m.tkk7.com/sealyu/archive/2010/02/09/312395.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Tue, 09 Feb 2010 03:05:00 GMT</pubDate><guid>http://m.tkk7.com/sealyu/archive/2010/02/09/312395.html</guid><wfw:comment>http://m.tkk7.com/sealyu/comments/312395.html</wfw:comment><comments>http://m.tkk7.com/sealyu/archive/2010/02/09/312395.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/sealyu/comments/commentRss/312395.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/sealyu/services/trackbacks/312395.html</trackback:ping><description><![CDATA[榪欎釜妯℃澘鍦ㄦ瘡涓狧ibernate鍙戣鐗堜笅project/etc涓嬶紝鍚嶅瓧涓篽ibernate.properties.<br /> 涓烘柟渚挎煡璇紝鎶?.3.2瀵瑰簲鐨勫唴瀹硅漿涓嬫潵錛屾柟渚夸互鍚庢煡璇細<br /> <br /> ######################<br /> ### Query Language ###<br /> ######################<br /> <br /> ## define query language constants / function names<br /> <br /> hibernate.query.substitutions yes 'Y', no 'N'<br /> <br /> <br /> ## select the classic query parser<br /> <br /> #hibernate.query.factory_class org.hibernate.hql.classic.ClassicQueryTranslatorFactory<br /> <br /> <br /> <br /> #################<br /> ### Platforms ###<br /> #################<br /> <br /> ## JNDI Datasource<br /> <br /> #hibernate.connection.datasource jdbc/test<br /> #hibernate.connection.username db2<br /> #hibernate.connection.password db2<br /> <br /> <br /> ## HypersonicSQL<br /> <br /> hibernate.dialect org.hibernate.dialect.HSQLDialect<br /> hibernate.connection.driver_class org.hsqldb.jdbcDriver<br /> hibernate.connection.username sa<br /> hibernate.connection.password<br /> hibernate.connection.url jdbc:hsqldb:./build/db/hsqldb/hibernate<br /> #hibernate.connection.url jdbc:hsqldb:hsql://localhost<br /> #hibernate.connection.url jdbc:hsqldb:test<br /> <br /> ## H2 (www.h2database.com)<br /> #hibernate.dialect org.hibernate.dialect.H2Dialect<br /> #hibernate.connection.driver_class org.h2.Driver<br /> #hibernate.connection.username sa<br /> #hibernate.connection.password<br /> #hibernate.connection.url jdbc:h2:mem:./build/db/h2/hibernate<br /> #hibernate.connection.url jdbc:h2:testdb/h2test<br /> #hibernate.connection.url jdbc:h2:mem:imdb1<br /> #hibernate.connection.url jdbc:h2:tcp://dbserv:8084/sample;     <br /> #hibernate.connection.url jdbc:h2:ssl://secureserv:8085/sample;     <br /> #hibernate.connection.url jdbc:h2:ssl://secureserv/testdb;cipher=AES<br /> <br /> ## MySQL<br /> <br /> #hibernate.dialect org.hibernate.dialect.MySQLDialect<br /> #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect<br /> #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect<br /> #hibernate.connection.driver_class com.mysql.jdbc.Driver<br /> #hibernate.connection.url jdbc:mysql:///test<br /> #hibernate.connection.username gavin<br /> #hibernate.connection.password<br /> <br /> <br /> ## Oracle<br /> <br /> #hibernate.dialect org.hibernate.dialect.Oracle8iDialect<br /> #hibernate.dialect org.hibernate.dialect.Oracle9iDialect<br /> #hibernate.dialect org.hibernate.dialect.Oracle10gDialect<br /> #hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver<br /> #hibernate.connection.username ora<br /> #hibernate.connection.password ora<br /> #hibernate.connection.url jdbc:oracle:thin:@localhost:1521:orcl<br /> #hibernate.connection.url jdbc:oracle:thin:@localhost:1522:XE<br /> <br /> <br /> ## PostgreSQL<br /> <br /> #hibernate.dialect org.hibernate.dialect.PostgreSQLDialect<br /> #hibernate.connection.driver_class org.postgresql.Driver<br /> #hibernate.connection.url jdbc:postgresql:template1<br /> #hibernate.connection.username pg<br /> #hibernate.connection.password<br /> <br /> <br /> ## DB2<br /> <br /> #hibernate.dialect org.hibernate.dialect.DB2Dialect<br /> #hibernate.connection.driver_class com.ibm.db2.jcc.DB2Driver<br /> #hibernate.connection.driver_class COM.ibm.db2.jdbc.app.DB2Driver<br /> #hibernate.connection.url jdbc:db2://localhost:50000/somename<br /> #hibernate.connection.url jdbc:db2:somename<br /> #hibernate.connection.username db2<br /> #hibernate.connection.password db2<br /> <br /> ## TimesTen<br /> <br /> #hibernate.dialect org.hibernate.dialect.TimesTenDialect<br /> #hibernate.connection.driver_class com.timesten.jdbc.TimesTenDriver<br /> #hibernate.connection.url jdbc:timesten:direct:test<br /> #hibernate.connection.username<br /> #hibernate.connection.password <br /> <br /> ## DB2/400<br /> <br /> #hibernate.dialect org.hibernate.dialect.DB2400Dialect<br /> #hibernate.connection.username user<br /> #hibernate.connection.password password<br /> <br /> ## Native driver<br /> #hibernate.connection.driver_class COM.ibm.db2.jdbc.app.DB2Driver<br /> #hibernate.connection.url jdbc:db2://systemname<br /> <br /> ## Toolbox driver<br /> #hibernate.connection.driver_class com.ibm.as400.access.AS400JDBCDriver<br /> #hibernate.connection.url jdbc:as400://systemname<br /> <br /> <br /> ## Derby (not supported!)<br /> <br /> #hibernate.dialect org.hibernate.dialect.DerbyDialect<br /> #hibernate.connection.driver_class org.apache.derby.jdbc.EmbeddedDriver<br /> #hibernate.connection.username<br /> #hibernate.connection.password<br /> #hibernate.connection.url jdbc:derby:build/db/derby/hibernate;create=true<br /> <br /> <br /> ## Sybase<br /> <br /> #hibernate.dialect org.hibernate.dialect.SybaseDialect<br /> #hibernate.connection.driver_class com.sybase.jdbc2.jdbc.SybDriver<br /> #hibernate.connection.username sa<br /> #hibernate.connection.password sasasa<br /> #hibernate.connection.url jdbc:sybase:Tds:co3061835-a:5000/tempdb<br /> <br /> <br /> ## Mckoi SQL<br /> <br /> #hibernate.dialect org.hibernate.dialect.MckoiDialect<br /> #hibernate.connection.driver_class com.mckoi.JDBCDriver<br /> #hibernate.connection.url jdbc:mckoi:///<br /> #hibernate.connection.url jdbc:mckoi:local://C:/mckoi1.0.3/db.conf<br /> #hibernate.connection.username admin<br /> #hibernate.connection.password nimda<br /> <br /> <br /> ## SAP DB<br /> <br /> #hibernate.dialect org.hibernate.dialect.SAPDBDialect<br /> #hibernate.connection.driver_class com.sap.dbtech.jdbc.DriverSapDB<br /> #hibernate.connection.url jdbc:sapdb://localhost/TST<br /> #hibernate.connection.username TEST<br /> #hibernate.connection.password TEST<br /> #hibernate.query.substitutions yes 'Y', no 'N'<br /> <br /> <br /> ## MS SQL Server<br /> <br /> #hibernate.dialect org.hibernate.dialect.SQLServerDialect<br /> #hibernate.connection.username sa<br /> #hibernate.connection.password sa<br /> <br /> ## JSQL Driver<br /> #hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver<br /> #hibernate.connection.url jdbc:JSQLConnect://1E1/test<br /> <br /> ## JTURBO Driver<br /> #hibernate.connection.driver_class com.newatlanta.jturbo.driver.Driver<br /> #hibernate.connection.url jdbc:JTurbo://1E1:1433/test<br /> <br /> ## WebLogic Driver<br /> #hibernate.connection.driver_class weblogic.jdbc.mssqlserver4.Driver<br /> #hibernate.connection.url jdbc:weblogic:mssqlserver4:1E1:1433<br /> <br /> ## Microsoft Driver (not recommended!)<br /> #hibernate.connection.driver_class com.microsoft.jdbc.sqlserver.SQLServerDriver<br /> #hibernate.connection.url jdbc:microsoft:sqlserver://1E1;DatabaseName=test;SelectMethod=cursor<br /> <br /> ## The New Microsoft Driver <br /> #hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver<br /> #hibernate.connection.url jdbc:sqlserver://localhost<br /> <br /> ## jTDS (since version 0.9)<br /> #hibernate.connection.driver_class net.sourceforge.jtds.jdbc.Driver<br /> #hibernate.connection.url jdbc:jtds:sqlserver://1E1/test<br /> <br /> ## Interbase<br /> <br /> #hibernate.dialect org.hibernate.dialect.InterbaseDialect<br /> #hibernate.connection.username sysdba<br /> #hibernate.connection.password masterkey<br /> <br /> ## DO NOT specify hibernate.connection.sqlDialect<br /> <br /> ## InterClient<br /> <br /> #hibernate.connection.driver_class interbase.interclient.Driver<br /> #hibernate.connection.url jdbc:interbase://localhost:3060/C:/firebird/test.gdb<br /> <br /> ## Pure Java<br /> <br /> #hibernate.connection.driver_class org.firebirdsql.jdbc.FBDriver<br /> #hibernate.connection.url jdbc:firebirdsql:localhost/3050:/firebird/test.gdb<br /> <br /> <br /> ## Pointbase<br /> <br /> #hibernate.dialect org.hibernate.dialect.PointbaseDialect<br /> #hibernate.connection.driver_class com.pointbase.jdbc.jdbcUniversalDriver<br /> #hibernate.connection.url jdbc:pointbase:embedded:sample<br /> #hibernate.connection.username PBPUBLIC<br /> #hibernate.connection.password PBPUBLIC<br /> <br /> <br /> ## Ingres<br /> <br /> ## older versions (before Ingress 2006)<br /> <br /> #hibernate.dialect org.hibernate.dialect.IngresDialect<br /> #hibernate.connection.driver_class ca.edbc.jdbc.EdbcDriver<br /> #hibernate.connection.url jdbc:edbc://localhost:II7/database<br /> #hibernate.connection.username user<br /> #hibernate.connection.password password<br /> <br /> ## Ingres 2006 or later<br /> <br /> #hibernate.dialect org.hibernate.dialect.IngresDialect<br /> #hibernate.connection.driver_class com.ingres.jdbc.IngresDriver<br /> #hibernate.connection.url jdbc:ingres://localhost:II7/database;CURSOR=READONLY;auto=multi<br /> #hibernate.connection.username user<br /> #hibernate.connection.password password<br /> <br /> ## Mimer SQL<br /> <br /> #hibernate.dialect org.hibernate.dialect.MimerSQLDialect<br /> #hibernate.connection.driver_class com.mimer.jdbc.Driver<br /> #hibernate.connection.url jdbc:mimer:multi1<br /> #hibernate.connection.username hibernate<br /> #hibernate.connection.password hibernate<br /> <br /> <br /> ## InterSystems Cache<br /> <br /> #hibernate.dialect org.hibernate.dialect.Cache71Dialect<br /> #hibernate.connection.driver_class com.intersys.jdbc.CacheDriver<br /> #hibernate.connection.username _SYSTEM<br /> #hibernate.connection.password SYS<br /> #hibernate.connection.url jdbc:Cache://127.0.0.1:1972/HIBERNATE<br /> <br /> <br /> #################################<br /> ### Hibernate Connection Pool ###<br /> #################################<br /> <br /> hibernate.connection.pool_size 1<br /> <br /> <br /> <br /> ###########################<br /> ### C3P0 Connection Pool###<br /> ###########################<br /> <br /> #hibernate.c3p0.max_size 2<br /> #hibernate.c3p0.min_size 2<br /> #hibernate.c3p0.timeout 5000<br /> #hibernate.c3p0.max_statements 100<br /> #hibernate.c3p0.idle_test_period 3000<br /> #hibernate.c3p0.acquire_increment 2<br /> #hibernate.c3p0.validate false<br /> <br /> <br /> <br /> ##############################<br /> ### Proxool Connection Pool###<br /> ##############################<br /> <br /> ## Properties for external configuration of Proxool<br /> <br /> hibernate.proxool.pool_alias pool1<br /> <br /> ## Only need one of the following<br /> <br /> #hibernate.proxool.existing_pool true<br /> #hibernate.proxool.xml proxool.xml<br /> #hibernate.proxool.properties proxool.properties<br /> <br /> <br /> <br /> #################################<br /> ### Plugin ConnectionProvider ###<br /> #################################<br /> <br /> ## use a custom ConnectionProvider (if not set, Hibernate will choose a built-in ConnectionProvider using hueristics)<br /> <br /> #hibernate.connection.provider_class org.hibernate.connection.DriverManagerConnectionProvider<br /> #hibernate.connection.provider_class org.hibernate.connection.DatasourceConnectionProvider<br /> #hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider<br /> #hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider<br /> <br /> <br /> <br /> #######################<br /> ### Transaction API ###<br /> #######################<br /> <br /> ## Enable automatic flush during the JTA beforeCompletion() callback<br /> ## (This setting is relevant with or without the Transaction API)<br /> <br /> #hibernate.transaction.flush_before_completion<br /> <br /> <br /> ## Enable automatic session close at the end of transaction<br /> ## (This setting is relevant with or without the Transaction API)<br /> <br /> #hibernate.transaction.auto_close_session<br /> <br /> <br /> ## the Transaction API abstracts application code from the underlying JTA or JDBC transactions<br /> <br /> #hibernate.transaction.factory_class org.hibernate.transaction.JTATransactionFactory<br /> #hibernate.transaction.factory_class org.hibernate.transaction.JDBCTransactionFactory<br /> <br /> <br /> ## to use JTATransactionFactory, Hibernate must be able to locate the UserTransaction in JNDI<br /> ## default is java:comp/UserTransaction<br /> ## you do NOT need this setting if you specify hibernate.transaction.manager_lookup_class<br /> <br /> #jta.UserTransaction jta/usertransaction<br /> #jta.UserTransaction javax.transaction.UserTransaction<br /> #jta.UserTransaction UserTransaction<br /> <br /> <br /> ## to use the second-level cache with JTA, Hibernate must be able to obtain the JTA TransactionManager<br /> <br /> #hibernate.transaction.manager_lookup_class org.hibernate.transaction.JBossTransactionManagerLookup<br /> #hibernate.transaction.manager_lookup_class org.hibernate.transaction.WeblogicTransactionManagerLookup<br /> #hibernate.transaction.manager_lookup_class org.hibernate.transaction.WebSphereTransactionManagerLookup<br /> #hibernate.transaction.manager_lookup_class org.hibernate.transaction.OrionTransactionManagerLookup<br /> #hibernate.transaction.manager_lookup_class org.hibernate.transaction.ResinTransactionManagerLookup<br /> <br /> <br /> <br /> ##############################<br /> ### Miscellaneous Settings ###<br /> ##############################<br /> <br /> ## print all generated SQL to the console<br /> <br /> #hibernate.show_sql true<br /> <br /> <br /> ## format SQL in log and console<br /> <br /> hibernate.format_sql true<br /> <br /> <br /> ## add comments to the generated SQL<br /> <br /> #hibernate.use_sql_comments true<br /> <br /> <br /> ## generate statistics<br /> <br /> #hibernate.generate_statistics true<br /> <br /> <br /> ## auto schema export<br /> <br /> #hibernate.hbm2ddl.auto create-drop<br /> #hibernate.hbm2ddl.auto create<br /> #hibernate.hbm2ddl.auto update<br /> #hibernate.hbm2ddl.auto validate<br /> <br /> <br /> ## specify a default schema and catalog for unqualified tablenames<br /> <br /> #hibernate.default_schema test<br /> #hibernate.default_catalog test<br /> <br /> <br /> ## enable ordering of SQL UPDATEs by primary key<br /> <br /> #hibernate.order_updates true<br /> <br /> <br /> ## set the maximum depth of the outer join fetch tree<br /> <br /> hibernate.max_fetch_depth 1<br /> <br /> <br /> ## set the default batch size for batch fetching<br /> <br /> #hibernate.default_batch_fetch_size 8<br /> <br /> <br /> ## rollback generated identifier values of deleted entities to default values<br /> <br /> #hibernate.use_identifer_rollback true<br /> <br /> <br /> ## enable bytecode reflection optimizer (disabled by default)<br /> <br /> #hibernate.bytecode.use_reflection_optimizer true<br /> <br /> <br /> <br /> #####################<br /> ### JDBC Settings ###<br /> #####################<br /> <br /> ## specify a JDBC isolation level<br /> <br /> #hibernate.connection.isolation 4<br /> <br /> <br /> ## enable JDBC autocommit (not recommended!)<br /> <br /> #hibernate.connection.autocommit true<br /> <br /> <br /> ## set the JDBC fetch size<br /> <br /> #hibernate.jdbc.fetch_size 25<br /> <br /> <br /> ## set the maximum JDBC 2 batch size (a nonzero value enables batching)<br /> <br /> #hibernate.jdbc.batch_size 5<br /> #hibernate.jdbc.batch_size 0<br /> <br /> <br /> ## enable batch updates even for versioned data<br /> <br /> hibernate.jdbc.batch_versioned_data true<br /> <br /> <br /> ## enable use of JDBC 2 scrollable ResultSets (specifying a Dialect will cause Hibernate to use a sensible default)<br /> <br /> #hibernate.jdbc.use_scrollable_resultset true<br /> <br /> <br /> ## use streams when writing binary types to / from JDBC<br /> <br /> hibernate.jdbc.use_streams_for_binary true<br /> <br /> <br /> ## use JDBC 3 PreparedStatement.getGeneratedKeys() to get the identifier of an inserted row<br /> <br /> #hibernate.jdbc.use_get_generated_keys false<br /> <br /> <br /> ## choose a custom JDBC batcher<br /> <br /> # hibernate.jdbc.factory_class<br /> <br /> <br /> ## enable JDBC result set column alias caching <br /> ## (minor performance enhancement for broken JDBC drivers)<br /> <br /> # hibernate.jdbc.wrap_result_sets<br /> <br /> <br /> ## choose a custom SQL exception converter<br /> <br /> #hibernate.jdbc.sql_exception_converter<br /> <br /> <br /> <br /> ##########################<br /> ### Second-level Cache ###<br /> ##########################<br /> <br /> ## optimize chache for minimal "puts" instead of minimal "gets" (good for clustered cache)<br /> <br /> #hibernate.cache.use_minimal_puts true<br /> <br /> <br /> ## set a prefix for cache region names<br /> <br /> hibernate.cache.region_prefix hibernate.test<br /> <br /> <br /> ## disable the second-level cache<br /> <br /> #hibernate.cache.use_second_level_cache false<br /> <br /> <br /> ## enable the query cache<br /> <br /> #hibernate.cache.use_query_cache true<br /> <br /> <br /> ## store the second-level cache entries in a more human-friendly format<br /> <br /> #hibernate.cache.use_structured_entries true<br /> <br /> <br /> ## choose a cache implementation<br /> <br /> #hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider<br /> #hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProvider<br /> hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider<br /> #hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider<br /> #hibernate.cache.provider_class org.hibernate.cache.OSCacheProvider<br /> #hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider<br /> <br /> <br /> ## choose a custom query cache implementation<br /> <br /> #hibernate.cache.query_cache_factory<br /> <br /> <br /> <br /> ############<br /> ### JNDI ###<br /> ############<br /> <br /> ## specify a JNDI name for the SessionFactory<br /> <br /> #hibernate.session_factory_name hibernate/session_factory<br /> <br /> <br /> ## Hibernate uses JNDI to bind a name to a SessionFactory and to look up the JTA UserTransaction;<br /> ## if hibernate.jndi.* are not specified, Hibernate will use the default InitialContext() which<br /> ## is the best approach in an application server<br /> <br /> #file system<br /> #hibernate.jndi.class com.sun.jndi.fscontext.RefFSContextFactory<br /> #hibernate.jndi.url file:/<br /> <br /> #WebSphere<br /> #hibernate.jndi.class com.ibm.websphere.naming.WsnInitialContextFactory<br /> #hibernate.jndi.url iiop://localhost:900/<br /> <br /> <br /> <br /> <img src ="http://m.tkk7.com/sealyu/aggbug/312395.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/sealyu/" target="_blank">seal</a> 2010-02-09 11:05 <a href="http://m.tkk7.com/sealyu/archive/2010/02/09/312395.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>ORA-12519, TNS:no appropriate service handler found(杞?http://m.tkk7.com/sealyu/archive/2010/02/03/311753.htmlsealsealWed, 03 Feb 2010 01:02:00 GMThttp://m.tkk7.com/sealyu/archive/2010/02/03/311753.htmlhttp://m.tkk7.com/sealyu/comments/311753.htmlhttp://m.tkk7.com/sealyu/archive/2010/02/03/311753.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/311753.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/311753.html

閬囧埌榪欑閿欒錛屼竴鑸槸鍥犱負騫跺彂榪囧ぇ寮曡搗鐨勩?/p>

鏌ヨ瑙e喅鏂規硶:

鏌ョ湅涓涓嬫暟鎹簱鐜版湁鐨勮繘紼嬫暟錛屾槸鍚﹀凡緇忚揪鍒板弬鏁皃rocesses鐨勫ぇ灝忋?/p>

1.select count(*) from v$process;                         鍙栧緱鏁版嵁搴撶洰鍓嶇殑榪涚▼鏁般?br /> 2.select value from v$parameter where name = 'processes'; 鍙栧緱榪涚▼鏁扮殑涓婇檺銆?br /> 3.濡傚凡杈懼埌涓婇檺錛屼慨鏀筰nitSID.ora涓殑processes鐨勫ぇ灝忋?br /> 4.閲嶆柊鍚姩鏁版嵁搴撳埌nomount鐘舵佷笅錛屾墽琛宑reate spfile from pfile; 騫秙tartup open銆?/p>


鏌ヨ鏁版嵁搴撹嚜鍚姩浠ユ潵鏈澶х殑騫跺彂鏁伴噺

select * from v$license



seal 2010-02-03 09:02 鍙戣〃璇勮
]]>
Hibernate鎶ラ敊org.hibernate.LazyInitializationException: illegal access to loading collectionhttp://m.tkk7.com/sealyu/archive/2010/01/21/310432.htmlsealsealThu, 21 Jan 2010 12:36:00 GMThttp://m.tkk7.com/sealyu/archive/2010/01/21/310432.htmlhttp://m.tkk7.com/sealyu/comments/310432.htmlhttp://m.tkk7.com/sealyu/archive/2010/01/21/310432.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/310432.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/310432.html   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
  at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)


鍘熷洜鍦ㄤ笌pojo閲岄潰鐨?  public int hashCode() {... } 鏂規硶璋冪敤浜唖et瀵硅薄鎴愬憳鐨刪ashCode()鏂規硶
濡倀his.getSets().hashCode();
鍒犻櫎璇ユ柟娉曠殑璋冪敤鍗沖彲錛涜皟鐢ㄩ潪set瀵硅薄鎴愬憳鐨?hashCode()鏂規硶娌℃湁闂銆?

seal 2010-01-21 20:36 鍙戣〃璇勮
]]>
鍩轟簬鎸塧nnotation鐨刪ibernate涓婚敭鐢熸垚絳栫暐錛堣漿錛?/title><link>http://m.tkk7.com/sealyu/archive/2010/01/21/310386.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 21 Jan 2010 05:57:00 GMT</pubDate><guid>http://m.tkk7.com/sealyu/archive/2010/01/21/310386.html</guid><wfw:comment>http://m.tkk7.com/sealyu/comments/310386.html</wfw:comment><comments>http://m.tkk7.com/sealyu/archive/2010/01/21/310386.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/sealyu/comments/commentRss/310386.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/sealyu/services/trackbacks/310386.html</trackback:ping><description><![CDATA[     鎽樿: 涓銆丣PA閫氱敤絳栫暐鐢熸垚鍣? 閫氳繃annotation鏉ユ槧灝刪ibernate瀹炰綋鐨?鍩轟簬annotation鐨刪ibernate涓婚敭鏍囪瘑涓篅Id, 鍏剁敓鎴愯鍒欑敱@GeneratedValue璁懼畾鐨?榪欓噷鐨凘id鍜孈GeneratedValue閮芥槸JPA鐨勬爣鍑嗙敤娉? JPA鎻愪緵鍥涚鏍囧噯鐢ㄦ硶,鐢盄GeneratedValue鐨勬簮浠g爜鍙互鏄庢樉鐪嬪嚭. Java浠g爜 ...  <a href='http://m.tkk7.com/sealyu/archive/2010/01/21/310386.html'>闃呰鍏ㄦ枃</a><img src ="http://m.tkk7.com/sealyu/aggbug/310386.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/sealyu/" target="_blank">seal</a> 2010-01-21 13:57 <a href="http://m.tkk7.com/sealyu/archive/2010/01/21/310386.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>OpenSessionInViewFilter 鐨勪袱涓紓甯?Illegal attempt to associate a collection with two open sessions; Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL)http://m.tkk7.com/sealyu/archive/2009/10/29/300275.htmlsealsealThu, 29 Oct 2009 14:23:00 GMThttp://m.tkk7.com/sealyu/archive/2009/10/29/300275.htmlhttp://m.tkk7.com/sealyu/comments/300275.htmlhttp://m.tkk7.com/sealyu/archive/2009/10/29/300275.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/300275.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/300275.html浣跨敤 Spring 鏁村悎 Hibernate, 鍦ㄦ噿鍔犺澆鐨勬儏鍐典笅, 鏈夋椂鍊欓渶瑕佸湪 JSP/View 灞傛樉紺烘暟鎹? 榪欐椂鍊欏氨瑕佺敤鍒癝pring鍐呯疆鐨? OpenSessionInViewFilter, 涓鑸潵璇撮厤緗涓?web.xml):

<filter>
<filter-name>hibernateFilter</filter-name>
<
filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<
init-param>
<
param-name>singleSession</param-name>
<param-value>true</param-value>
</
init-param><!-- 鍜?spring 涓殑sesssionfactory ID 涓鑷?-->
</filter>
<
filter-mapping><filter-name>hibernateFilter</filter-name><url-pattern>*.do</url-pattern><!-- *.jsp, *.do--></filter-mapping>
涓嶈繃, 榪欐椂鍊欏張浼氬鑷存洿鏂版暟鎹椂鎶涘嚭濡備笅寮傚父:
Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

榪欐椂鍊欏啀鍘葷綉涓婃壘瑙e喅鏂規, 浼氭湁浜鴻: 鎶婂弬鏁?singleSession鏀逛負false, 灝辮浜? 涓嶈繃, 鏀瑰畬鍚? 浼拌涓嶄箙灝變細閬囧埌鍙︿竴涓儊闂風殑寮傚父:

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

榪欎笅瀹屼簡, 涓や釜鏂規閮戒笉琛? 鍒板簳鎬庝箞鍔? 榪樺ソ, 鍦?a title="http://xuliangyong.javaeye.com/blog/144818" >http://xuliangyong.javaeye.com/blog/144818鐨勪富欏典笂, 緇欎簡涓涓柟妗? 灝辨槸鏀瑰啓 OpenSessionInViewFilter 鐨勪唬鐮? 闈炲父鎰熻阿, 涓嬮潰緇欏嚭鐨勫氨鏄渶緇堟柟妗?

web.xml

< filter-name >hibernateFilter</filter-name>

< filter-class > org.springframework.orm.hibernate3.support.OurOpenSessionInViewFilter </filter-class>

OurOpenSessionInViewFilter.java 浠g爜:

package org.springframework.orm.hibernate3.support;



import org.hibernate.*;



/** * 鍗晄ession妯″紡涓? 榛樿浼氬彂鐢熸棤娉曟彁浜ょ殑閿欒: * Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. * 闇瑕佽緗瓼lushMode騫跺埛鏂皊ession. * 鍙傝? http://xuliangyong.javaeye.com/blog/144818 * @author 鍒橀暱鐐?/span> */publicclass OurOpenSessionInViewFilter extends OpenSessionInViewFilter {



public OurOpenSessionInViewFilter() {

super.setFlushMode(FlushMode.AUTO);

}



protectedvoid closeSession(Session session, SessionFactory sessionFactory) {

session.flush();



try {

session.getTransaction().commit();

} catch (HibernateException e) {

// TODO Auto-generated catch block//e.printStackTrace();

}



super.closeSession(session, sessionFactory);

}

}

濡傛灉鍚勪綅鏈夋洿濂界殑瑙e喅鏂規, 嬈㈣繋璁ㄨ鍝?!!

棰樺璇?

鎰熻 Spring + Hibernate 鐨勫仴澹ц繕鏄笉澶熷晩! 瀹規槗鎶涘紓甯? 榪欐槸浜嬪疄, 涔熻榪欐槸寮婧愯蔣浠剁殑閫氱梾鍚?



seal 2009-10-29 22:23 鍙戣〃璇勮
]]>
Solving the Simultaneously Fetch Multiple Bags Exception using idbags錛堣漿錛?/title><link>http://m.tkk7.com/sealyu/archive/2009/08/27/292845.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 27 Aug 2009 08:56:00 GMT</pubDate><guid>http://m.tkk7.com/sealyu/archive/2009/08/27/292845.html</guid><wfw:comment>http://m.tkk7.com/sealyu/comments/292845.html</wfw:comment><comments>http://m.tkk7.com/sealyu/archive/2009/08/27/292845.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/sealyu/comments/commentRss/292845.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/sealyu/services/trackbacks/292845.html</trackback:ping><description><![CDATA[<div> <p style="text-align: justify;">About a year and an half ago I wrote an <a >entry</a> about the problem that rises when mapping an entity with multiple bags using <em>eager</em> fetching. At the end of the entry I suggested three different solutions: (a) to use lazy fetching, (b) to use sets instead of bags, and (c) to use the @IndexColumn annotation. Few months later I <a >elaborated</a> on the usage of @IndexColumn, this time another way – using the @CollectionId annotation.</p> <p>Environment</p> <p style="margin-left: 0.5in; text-indent: -0.25in;"><span style="font-size: 10pt; font-family: Symbol;"><span>·<span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span></span><span dir="LTR">Hibernate Entity Manager – 3.3.1.GA</span></p> <p style="margin-left: 0.5in; text-indent: -0.25in;"><span style="font-size: 10pt; font-family: Symbol;"><span>·<span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span></span><span dir="LTR">Hibernate core – 3.2.5.GA</span></p> <p style="margin-left: 0.5in; text-indent: -0.25in;"><span style="font-size: 10pt; font-family: Symbol;"><span>·<span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span></span><span dir="LTR">Hibernate annotations- 3.3.0.GA</span></p> <p style="margin-left: 0.5in; text-indent: -0.25in;"><span style="font-size: 10pt; font-family: Symbol;"><span>·<span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span></span><span dir="LTR">Database – PostgreSQL 8.1</span></p> <h3>First one, first…</h3> <p style="text-align: justify;">Just before we start a warning – the @CollectionId annotation is a Hibernate specific annotation – not a part of the specification. And it doesn't work on one-to-many associations (but it can be used in conjunction with one-to-many associations). After putting that behind of us lets see the problem.</p> <p style="text-align: center;" align="center"> <script type="text/javascript"> <!-- google_ad_client = "pub-7548643028974164"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; google_ad_channel =""; google_color_border = "9191CD"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_url = "008000"; google_color_text = "000000"; //--> </script> <script>google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);</script>  </p> <h3>The Problem</h3> <p style="margin-bottom: 0.0001pt; text-align: justify;">Assume the following entities relation, a parent entity has two collections of child entities. Both collections should be eagerly loaded.</p> <img src="http://jroller.com/eyallupu/resource/idbag-class-diagram.gif" border="0" height="192" width="288" alt="" /><br /> <p style="margin-bottom: 0.0001pt;">First try will be to just to map it as is (Child1 has a many-to-many association; Child2 has a one-to-many):</p> <div style="border: 1pt solid black; padding: 1pt; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"> </pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">@<strong>ManyToMany</strong>( fetch = FetchType.EAGER, cascade=CascadeType.ALL)</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">@JoinTable(name = "PARENT_CHILD1", </pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">joinColumns = @JoinColumn(name = "PARENT_ID", referencedColumnName = "ID"), </pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">inverseJoinColumns = @JoinColumn(name = "CHILD1_ID", referencedColumnName = "ID"))<br /> </pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">List<Child1> child1s = new LinkedList<Child1>();</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"> </pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">@<strong>OneToMany</strong>(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">List<Child2> child2s = new LinkedList<Child2>();</pre> </div> <p style="margin-bottom: 12pt; text-align: justify;">But when loading a persistence unit with the above configuration the "org.hibernate.HibernateException: cannot simultaneously fetch multiple bags" will be thrown.</p> <h3>Using an idbag</h3> <p style="margin-bottom: 12pt; text-align: justify;">The reason is that when we add the @CollectionId to a List or a Collection its semantics changes from "just a bag" to "a bag with primary key" (a.k.a, idbag). It means that a surrogate key is assigned to each row on the collection</p> <p style="margin-bottom: 12pt; text-align: justify;">When transforming the association to Child1 into an <a >idbag</a> (using the @CollectionId annotation) the problem is solved. The reason is that when we switch the association semantics from "a simple bag" to "a bag with primary key" (a.k.a, idbag) it means that a surrogate key is assigned to each row on the collection.</p> <div style="border: 1pt solid black; padding: 1pt; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">@Entity</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">public class Parent {</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">………</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">@ManyToMany( fetch = FetchType.EAGER, cascade=CascadeType.ALL)</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">@JoinTable(name = "PARENT_CHILD1", </pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">joinColumns = @JoinColumn(name = "PARENT_ID", referencedColumnName = "ID"), </pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">inverseJoinColumns = @JoinColumn(name = "CHILD1_ID", referencedColumnName = "ID"))</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"><strong>@GenericGenerator(name="uuid-gen", strategy = "uuid")</strong></pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"><strong>@CollectionId(columns = @Column(name = "COL_ID"), type = @Type(type = "string"), generator = "uuid-gen")</strong>List<Child1> child1s = new LinkedList<Child1>();</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"> </pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">List<Child2> child2s = new LinkedList<Child2>();</pre> <pre style="border: medium none ; padding: 0in; background: #cccccc none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">}</pre> </div> <p style="margin-bottom: 12pt; text-align: justify;">The problem with bags is that when Hibernate eagerly loads multiple collections it issues an outer join select to the RDBMS which can cause multiple entries on some of the collections. But when using an idbag each row in the collections is uniquely identified by the surrogate key, therefore Hibernate can identify it within the collection, even if the SQL statement returns an entry more than once.</p> <p style="margin-bottom: 12pt; text-align: justify;">Notice the usage of a generator assigned to the collection id, the generator is responsible for creating the surrogate keys of the collection rows. I decided to use the UUID strategy. You can, of course, use JPA standard generators (such as Sequence or Table generators). The @CollectionId references the COL_ID column on PARENT_CHILD1 table (the join table). The value of the collection id is invisible to the application (it is not being mapped to a property).</p> <p style="text-align: center;" align="center"> <script type="text/javascript"> <!-- google_ad_client = "pub-7548643028974164"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; google_ad_channel =""; google_color_border = "9191CD"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_url = "008000"; google_color_text = "000000"; //--> </script> <script>google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);</script>  </p> <h3>So why not on one-to-many</h3> <p style="margin-bottom: 12pt; text-align: justify;">The Hibernate <em>annotations</em> documentation says that to announce idbag semantics you should assign the @CollectionId to a @ManyToMany, @CollectionOfElements, or @OneToMany (look at the table on <a >this</a> section), but the Hibernate <em>core</em> documentation it says "Hibernate provides a feature that allows you to map <u>many to many</u> associations and <u>collections of values</u> to a table with a surrogate key." (<a >here</a>). I've tried it and indeed when annotating a @OneToMany collection with the @CollectionId an exception with the message "one-to-many collections with identifiers are not supported " is thrown by Hibernate.</p> <h3>Idbag - Not Just For Eager Fetching</h3> <p style="text-align: justify;">Don't forge that you can use idbag for reasons other than solving multiple eager associations. For example it can give a major performance boost over simple bags for mutative operations. If entries in a collection have surrogate keys Hibernate will be able to locate the matching rows in the database using these keys (each row in the association table becomes unique) – there is no need for the fetch -> delete-all -> insert-all cycle when updating the collection.</p> </div> <br /> <a ><img src="http://jroller.com/resources/e/eyallupu/delicious.gif" border="0" alt="" /></a> <a ><img src="http://jroller.com/resources/e/eyallupu/digg.gif" border="0" alt="" /></a> <a ><img src="http://jroller.com/resources/e/eyallupu/technorati.gif" border="0" alt="" /></a> <br /> <p> Posted at <a > 03:49PM Jan 18, 2008</a> by Eyal Lupu in Persistence  |  <a class="commentsLink">Comments[6]</a> </p> <div class="mqycqak" id="comments"> <div>Comments:</div> <br /> <div odd="" id="comment1"> Actually a @OneToMany @JoinTable would work (as opposed to @OneToMany @JoinColumn) <p> Posted by <strong>Emmanuel Bernard</strong> on January 21, 2008 at 09:33 PM GMT+02:00 <a class="entrypermalink" title="comment permalink">#</a> </p> </div> <div even="" id="comment2"> Thanks Emmanuel, <br /> <br /> Maybe it worth a comment in the documentation. I guess this is actually an Hibernate core's issue (not an Hibernate annotation one - is it?). <p> Posted by <strong>Eyal Lupu</strong> on January 21, 2008 at 11:56 PM GMT+02:00 <a class="entrypermalink" title="comment permalink">#</a> </p> </div> <div odd="" id="comment3"> So, if I have 2 parallel OneToMany collections it's not fixable with: <br /> <br /> @OneToMany @CollectionId <br /> List<Son> getSons() {...} <br /> <br /> @OneToMany @CollectionId <br /> List<Daughter> getDaughter() {...} <br /> <br /> But if they were ManyToMany collections it is fixable? <br /> <br /> Strange :) Any particular reason? <p> Posted by <strong>Geoffrey De Smet</strong> on January 23, 2008 at 11:11 AM GMT+02:00 <a class="entrypermalink" title="comment permalink">#</a> </p> </div> <div even="" id="comment4"> Hi Geoffrey, <br /> Yes - you have understood it correctly. <br /> <br /> I guess the reason is that regular one-to-many associations have a foreign key on the child record and there is no place to store the collection id there. <br /> <br /> However, see Emmanuel comment above - you can solve it using a join table. <br /> <p> Posted by <a rel="nofollow" ><strong>Eyal Lupu</strong></a> on January 23, 2008 at 11:59 AM GMT+02:00 <a class="entrypermalink" title="comment permalink">#</a> </p> </div> <div odd="" id="comment5"> Year the documentation should be clearer. <p> Posted by <strong>Emmanuel Bernard</strong> on January 23, 2008 at 04:48 PM GMT+02:00 <a class="entrypermalink" title="comment permalink">#</a> </p> </div> <div even="" id="comment6"> There is fourth solution of this problem. Works for both @OneToMany and @ManyToMany: <br /> <br /> @OneToMany(mappedBy="account", cascade=CascadeType.ALL) <br /> @LazyCollection(LazyCollectionOption.FALSE) <br /> private List<Entry> entries; <br /> <br /> Annotation @LazyCollection(LazyCollectionOption.FALSE) makes that collecion is loaded like with FetchType.EAGER and you can use it on two and more collections. <br /> This solution is (in my opinion) better because it's simplier. :) <p> Posted by <strong>Bartosz Jakubowski</strong> on October 22, 2008 at 01:26 PM GMT+02:00 <a class="entrypermalink" title="comment permalink">#</a> </p> </div> </div> <img src ="http://m.tkk7.com/sealyu/aggbug/292845.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/sealyu/" target="_blank">seal</a> 2009-08-27 16:56 <a href="http://m.tkk7.com/sealyu/archive/2009/08/27/292845.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Hibernate涓璯et鍜宭oad鏂規硶鐨勫尯鍒互鍙奵lose()錛宑lear()銆乪vict()絳夌殑鍖哄埆錛堣漿錛?/title><link>http://m.tkk7.com/sealyu/archive/2009/08/19/291759.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Wed, 19 Aug 2009 03:00:00 GMT</pubDate><guid>http://m.tkk7.com/sealyu/archive/2009/08/19/291759.html</guid><wfw:comment>http://m.tkk7.com/sealyu/comments/291759.html</wfw:comment><comments>http://m.tkk7.com/sealyu/archive/2009/08/19/291759.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/sealyu/comments/commentRss/291759.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/sealyu/services/trackbacks/291759.html</trackback:ping><description><![CDATA[<div> <div g_c_pdin="" g_p_center="" c07="" content="" id="blogtext_fks_082066082094085068080084081095085080088070084094094"> <div> <p style="text-indent: 2em;">get鍜宭oad鏂瑰紡鏄牴鎹甶d鍙栧緱涓涓褰?</p> <p style="text-indent: 2em;">涓嬭竟璇︾粏璇翠竴涓媑et鍜宭oad鐨勪笉鍚岋紝鍥犱負鏈変簺鏃跺欎負浜嗗姣斾篃浼氭妸find鍔犺繘鏉ャ?</p> <p style="text-indent: 2em;">1.浠庤繑鍥炵粨鏋滀笂瀵規瘮錛?</p> <p style="text-indent: 2em;">load鏂瑰紡媯绱笉鍒扮殑璇濅細鎶涘嚭org.hibernate.ObjectNotFoundException寮傚父 </p> <p style="text-indent: 2em;">get鏂規硶媯绱笉鍒扮殑璇濅細榪斿洖null </p> <p style="text-indent: 2em;">2.浠庢绱㈡墽琛屾満鍒朵笂瀵規瘮錛?</p> <p style="text-indent: 2em;">get鏂規硶鍜宖ind鏂規硶閮芥槸鐩存帴浠庢暟鎹簱涓绱?</p> <p style="text-indent: 2em;">鑰宭oad鏂規硶鐨勬墽琛屽垯姣旇緝澶嶆潅 </p> <p style="text-indent: 2em;">鈻?棣栧厛鏌ユ壘session鐨刾ersistent Context涓槸鍚︽湁緙撳瓨錛屽鏋滄湁鍒欑洿鎺ヨ繑鍥?</p> <p style="text-indent: 2em;">鈻?濡傛灉娌℃湁鍒欏垽鏂槸鍚︽槸lazy錛屽鏋滀笉鏄洿鎺ヨ闂暟鎹簱媯绱紝鏌ュ埌璁板綍榪斿洖錛屾煡涓嶅埌鎶涘嚭寮傚父 </p> <p style="text-indent: 2em;">鈻?濡傛灉鏄痩azy鍒欓渶瑕佸緩绔嬩唬鐞嗗璞★紝瀵硅薄鐨刬nitialized灞炴т負false錛宼arget灞炴т負null </p> <p style="text-indent: 2em;">鈻?鍦ㄨ闂幏寰楃殑浠g悊瀵硅薄鐨勫睘鎬ф椂,媯绱㈡暟鎹簱錛屽鏋滄壘鍒拌褰曞垯鎶婅璁板綍鐨勫璞″鍒跺埌浠g悊瀵硅薄鐨則arget涓婏紝騫跺皢initialized=true錛屽鏋滄壘涓嶅埌灝辨姏鍑哄紓甯?銆?</p> <p style="text-indent: 2em;">3.鏍規湰鍖哄埆璇存槑 </p> <p style="text-indent: 2em;">鈻? 濡傛灉浣犱嬌鐢╨oad鏂規硶錛宧ibernate璁や負璇d瀵瑰簲鐨勫璞★紙鏁版嵁搴撹褰曪級鍦ㄦ暟鎹簱涓槸涓瀹氬瓨鍦ㄧ殑錛屾墍浠ュ畠鍙互鏀懼績鐨勪嬌鐢紝瀹冨彲浠ユ斁蹇冪殑浣跨敤浠g悊鏉? 寤惰繜鍔犺澆璇ュ璞°傚湪鐢ㄥ埌瀵硅薄涓殑鍏朵粬灞炴ф暟鎹椂鎵嶆煡璇㈡暟鎹簱錛屼絾鏄竾涓鏁版嵁搴撲腑涓嶅瓨鍦ㄨ璁板綍錛岄偅娌″姙娉曪紝鍙兘鎶涘紓甯搞傛墍璇寸殑load鏂規硶鎶涘紓甯告槸鎸囧湪浣跨敤 璇ュ璞$殑鏁版嵁鏃訛紝鏁版嵁搴撲腑涓嶅瓨鍦ㄨ鏁版嵁鏃舵姏寮傚父錛岃屼笉鏄湪鍒涘緩榪欎釜瀵硅薄鏃?娉ㄦ剰錛氳繖灝辨槸鐢變簬“寤惰繜鍔犺澆”鍦ㄤ綔鎬?銆?</p> <p style="text-indent: 2em;">鐢變簬session涓殑緙撳瓨瀵逛簬hibernate鏉ヨ鏄釜鐩稿綋寤変環鐨勮祫婧愶紝鎵浠ュ湪load鏃朵細鍏堟煡涓涓媠ession緙撳瓨鐪嬬湅璇d瀵瑰簲鐨勫璞℃槸鍚﹀瓨鍦紝涓嶅瓨鍦ㄥ垯鍒涘緩浠g悊銆傛墍浠ュ鏋滀綘鐭ラ亾璇d鍦ㄦ暟鎹簱涓竴瀹氭湁瀵瑰簲璁板綍瀛樺湪灝卞彲浠ヤ嬌鐢╨oad鏂規硶鏉ュ疄鐜板歡榪熷姞杞姐?</p> <p style="text-indent: 2em;">鈻?瀵逛簬get鏂規硶錛宧ibernate浼氱‘璁や竴涓嬭id瀵瑰簲鐨勬暟鎹槸鍚﹀瓨鍦紝棣栧厛鍦╯ession緙撳瓨涓煡鎵撅紝鐒跺悗鍦ㄤ簩綰х紦瀛樹腑鏌ユ壘錛岃繕娌℃湁灝辨煡鏁版嵁搴擄紝鏁版嵁搴撲腑娌℃湁灝辮繑鍥瀗ull銆?</p> <p style="text-indent: 2em;">瀵逛簬load鍜実et鏂規硶榪斿洖綾誨瀷錛氳櫧鐒跺ソ澶氫功涓兘榪欎箞璇達細“get()姘歌繙鍙繑鍥炲疄浣? 綾?#8221;錛屼絾瀹為檯涓婅繖鏄笉姝g‘鐨勶紝get鏂規硶濡傛灉鍦? session緙撳瓨涓壘鍒頒簡璇d瀵瑰簲鐨勫璞★紝濡傛灉鍒氬ソ璇ュ璞″墠闈㈡槸琚唬鐞嗚繃鐨勶紝濡傝load鏂規硶浣跨敤榪囷紝鎴栬呰鍏朵粬鍏寵仈瀵硅薄寤惰繜鍔犺澆榪囷紝閭d箞榪斿洖鐨勮繕鏄? 鍘熷厛鐨勪唬鐞嗗璞★紝鑰屼笉鏄疄浣撶被瀵硅薄錛屽鏋滆浠g悊瀵硅薄榪樻病鏈夊姞杞藉疄浣撴暟鎹紙灝辨槸id浠ュ鐨勫叾浠栧睘鎬ф暟鎹級錛岄偅涔堝畠浼氭煡璇簩綰х紦瀛樻垨鑰呮暟鎹簱鏉ュ姞杞芥暟鎹紝浣嗘槸 榪斿洖鐨勮繕鏄唬鐞嗗璞★紝鍙笉榪囧凡緇忓姞杞戒簡瀹炰綋鏁版嵁銆?</p> <p style="text-indent: 2em;">get鏂規硶棣栧厛鏌ヨsession緙撳瓨錛屾病鏈夌殑璇濇煡璇簩綰х紦瀛橈紝鏈鍚庢煡璇㈡暟鎹簱錛涘弽鑰宭oad鏂規硶鍒涘緩鏃墮鍏堟煡璇ession緙撳瓨錛屾病鏈夊氨鍒涘緩浠g悊錛屽疄闄呬嬌鐢ㄦ暟鎹椂鎵嶆煡璇簩綰х紦瀛樺拰鏁版嵁搴撱?</p> <p style="text-indent: 2em;">4.綆鍗曟葷粨 </p> <p style="text-indent: 2em;">鎬諱箣瀵逛簬get鍜宭oad鐨勬牴鏈尯鍒紝涓鍙ヨ瘽錛宧ibernate瀵逛簬load鏂規硶璁や負璇ユ暟鎹湪鏁版嵁搴撲腑涓瀹氬瓨鍦紝鍙互鏀懼績鐨勪嬌鐢ㄤ唬鐞嗘潵寤惰繜鍔犺澆錛屽鏋滃湪浣跨敤榪囩▼涓彂鐜頒簡闂錛屽彧鑳芥姏寮傚父錛涜屽浜巊et鏂規硶錛宧ibernate涓瀹氳鑾峰彇鍒扮湡瀹炵殑鏁版嵁錛屽惁鍒欒繑鍥瀗ull銆?/p> <p style="text-indent: 2em;">鏂囩珷鍑哄錛?a >http://www.diybl.com/course/3_program/java/javajs/2008531/118292.html</a></p> <p style="text-indent: 2em;">hibernate鐨勫悇縐嶄繚瀛樻柟寮忕殑鍖?save,persist,update,saveOrUpdte,merge,flush,lock)鍙?瀵硅薄鐨勪笁縐嶇姸鎬?/p> <p style="text-indent: 2em;">hibernate鐨勪繚瀛?/p> <p style="text-indent: 2em;">hibernate瀵逛簬瀵硅薄鐨勪繚瀛樻彁渚涗簡澶鐨勬柟娉曪紝浠栦滑涔嬮棿鏈夊緢澶氫笉鍚岋紝榪欓噷緇嗚涓涓嬶紝浠ヤ究鍖哄埆銆?/p> <p style="text-indent: 2em;">涓銆侀澶囩煡璇?/p> <p style="text-indent: 2em;">瀵逛簬hibernate錛屽畠鐨勫璞℃湁涓夌鐘舵侊紝transient銆乸ersistent銆乨etached</p> <p style="text-indent: 2em;">涓嬭竟鏄父瑙佺殑緲昏瘧鍔炴硶錛?/p> <p style="text-indent: 2em;">transient錛氱灛鎬佹垨鑰呰嚜鐢辨?/p> <p style="text-indent: 2em;">(new DeptPo(1,”琛屾斂閮?#8221;,20,”琛屾斂鐩稿叧”)錛岃po鐨勫疄渚嬪拰session娌℃湁鍏寵仈錛岃po鐨勫疄渚嬪浜巘ransient)</p> <p style="text-indent: 2em;">persistent錛氭寔涔呭寲鐘舵?/p> <p style="text-indent: 2em;">(鍜屾暟鎹簱涓褰曟兂褰卞皠鐨凱o瀹炰緥錛屽畠鐨勭姸鎬佹槸persistent, 閫氳繃get鍜宭oad絳夊緱鍒扮殑瀵硅薄閮芥槸persistent)</p> <p style="text-indent: 2em;">detached錛氳劚綆$姸鎬佹垨鑰呮父紱繪?/p> <p style="text-indent: 2em;">(1)褰撻氳繃get鎴杔oad鏂規硶寰楀埌鐨刾o瀵硅薄瀹冧滑閮藉浜巔ersistent,浣嗗鏋滄墽 琛宒elete(po)鏃?浣嗕笉鑳芥墽琛屼簨鍔?,璇o鐘舵佸氨澶勪簬detached, (琛ㄧず鍜宻ession鑴辯鍏寵仈),鍥燿elete鑰屽彉鎴愭父紱繪佸彲浠ラ氳繃save鎴杝aveOrUpdate()鍙樻垚鎸佷箙鎬?/p> <p style="text-indent: 2em;">(2)褰撴妸session鍏抽棴鏃訛紝session緙撳瓨涓殑persistent鐨刾o瀵硅薄涔熷彉鎴恉etached</p> <p style="text-indent: 2em;">鍥犲叧闂璼ession鑰屽彉鎴愭父紱繪佺殑鍙互閫氳繃lock銆乻ave銆乽pdate鍙樻垚鎸佷箙鎬?/p> <p style="text-indent: 2em;">鎸佷箙鎬佸疄渚嬪彲浠ラ氳繃璋冪敤 delete()鍙樻垚鑴辯鐘舵併?/p> <p style="text-indent: 2em;">閫氳繃get()鎴杔oad()鏂規硶寰楀埌鐨勫疄渚嬮兘鏄寔涔呭寲鐘舵佺殑銆?/p> <p style="text-indent: 2em;">鑴辯鐘舵佺殑瀹炰緥鍙互閫氳繃璋冪敤lock()鎴栬卹eplicate()榪涜鎸佷箙鍖栥?/p> <p style="text-indent: 2em;">save()鍜宲ersist()灝嗕細寮曞彂SQL鐨処NSERT錛宒elete()浼氬紩鍙慡QLDELETE錛?/p> <p style="text-indent: 2em;">鑰寀pdate()鎴杕erge()浼氬紩鍙慡QL UPDATE銆傚鎸佷箙鍖栵紙persistent錛夊疄渚嬬殑淇敼鍦ㄥ埛鏂版彁浜ょ殑鏃跺欎細琚嫻嬪埌錛屽畠涔熶細寮曡搗SQL UPDATE銆?/p> <p style="text-indent: 2em;">saveOrUpdate()鎴栬卹eplicate()浼氬紩鍙慡QLINSERT鎴栬匲PDATE</p> <p style="text-indent: 2em;">浜屻乻ave 鍜寀pdate鍖哄埆</p> <p style="text-indent: 2em;">鎶婅繖涓瀵規斁鍦ㄧ涓浣嶇殑鍘熷洜鏄洜涓鴻繖涓瀵規槸鏈甯哥敤鐨勩?/p> <p style="text-indent: 2em;">save鐨勪綔鐢ㄦ槸鎶婁竴涓柊鐨勫璞′繚瀛?/p> <p style="text-indent: 2em;">update鏄妸涓涓劚綆$姸鎬佺殑瀵硅薄鎴栬嚜鐢辨佸璞★紙涓瀹氳鍜屼竴涓褰曞搴旓級鏇存柊鍒版暟鎹簱</p> <p style="text-indent: 2em;">涓夈乽pdate 鍜宻aveOrUpdate鍖哄埆</p> <p style="text-indent: 2em;">榪欎釜鏄瘮杈冨ソ鐞嗚В鐨勶紝欏懼悕鎬濅箟錛宻aveOrUpdate鍩烘湰涓婂氨鏄悎鎴愪簡save鍜寀pdate,鑰寀pdate鍙槸update;寮曠敤hibernate reference涓殑涓孌佃瘽鏉ヨВ閲婁粬浠殑浣跨敤鍦哄悎鍜屽尯鍒?/p> <p style="text-indent: 2em;">閫氬父涓嬮潰鐨勫満鏅細浣跨敤update()鎴杝aveOrUpdate()錛?</p> <p style="text-indent: 2em;">紼嬪簭鍦ㄧ涓涓猻ession涓姞杞藉璞?鎺ョ潃鎶妔ession鍏抽棴 </p> <p style="text-indent: 2em;">璇ュ璞¤浼犻掑埌琛ㄧ幇灞?</p> <p style="text-indent: 2em;">瀵硅薄鍙戠敓浜嗕竴浜涙敼鍔?</p> <p style="text-indent: 2em;">璇ュ璞¤榪斿洖鍒頒笟鍔¢昏緫灞傛渶緇堝埌鎸佷箙灞?</p> <p style="text-indent: 2em;">紼嬪簭鍒涘緩絎簩session璋冪敤絎簩涓猻ession鐨剈pdate()鏂規硶鎸佷箙榪欎簺鏀瑰姩 </p> <p style="text-indent: 2em;">saveOrUpdate(po)鍋氫笅闈㈢殑浜? </p> <p style="text-indent: 2em;">濡傛灉璇o瀵硅薄宸茬粡鍦ㄦ湰session涓寔涔呭寲浜嗭紝鍦ㄦ湰session涓墽琛宻aveOrUpdate涓嶅仛浠諱綍浜?</p> <p style="text-indent: 2em;">濡傛灉savaOrUpdate(鏂皃o)涓庡彟涓涓笌鏈瑂ession鍏寵仈鐨刾o瀵硅薄鎷ユ湁鐩稿悓鐨勬寔涔呭寲鏍囪瘑(identifier)錛屾姏鍑轟竴涓紓甯?</p> <p style="text-indent: 2em;">org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [org.itfuture.www.po.Xtyhb#5]</p> <p style="text-indent: 2em;">saveOrUpdate濡傛灉瀵硅薄娌℃湁鎸佷箙鍖栨爣璇?identifier)灞炴э紝瀵瑰叾璋冪敤save() 錛屽惁鍒檜pdate() 榪欎釜瀵硅薄 </p> <p style="text-indent: 2em;">鍥涖乸ersist鍜宻ave鍖哄埆</p> <p style="text-indent: 2em;">榪欎釜鏄渶榪風鐨勪竴瀵癸紝琛ㄩ潰涓婄湅璧鋒潵浣跨敤鍝釜閮借錛屽湪hibernate reference鏂囨。涓篃娌℃湁鏄庣‘鐨勫尯鍒嗕粬浠?</p> <p style="text-indent: 2em;">榪欓噷緇欏嚭涓涓槑紜殑鍖哄垎銆傦紙鍙互璺熻繘src鐪嬩竴涓嬶紝铏界劧瀹炵幇姝ラ綾諱技錛屼絾鏄繕鏄湁緇嗗井鐨勫樊鍒級</p> <p style="text-indent: 2em;">涓昏鍐呭鍖哄埆錛?/p> <p style="text-indent: 2em;">1錛宲ersist鎶婁竴涓灛鎬佺殑瀹炰緥鎸佷箙鍖栵紝浣嗘槸騫?涓嶄繚璇?鏍囪瘑絎?identifier涓婚敭瀵瑰簲鐨勫睘鎬?琚珛鍒誨~鍏ュ埌鎸佷箙鍖栧疄渚嬩腑錛屾爣璇嗙鐨勫~鍏ュ彲鑳借鎺ㄨ繜鍒癴lush鐨勬椂鍊欍?/p> <p style="text-indent: 2em;">2錛宻ave, 鎶婁竴涓灛鎬佺殑瀹炰緥鎸佷箙鍖栨爣璇嗙錛屽強鏃剁殑浜х敓,瀹冭榪斿洖鏍囪瘑絎︼紝鎵浠ュ畠浼氱珛鍗蟲墽琛孲ql insert</p> <p style="text-indent: 2em;">浜斻乻aveOrUpdate,merge鍜寀pdate鍖哄埆</p> <p style="text-indent: 2em;">姣旇緝update鍜宮erge</p> <p style="text-indent: 2em;">update鐨勪綔鐢ㄤ笂杈硅浜嗭紝榪欓噷璇翠竴涓媘erge鐨?/p> <p style="text-indent: 2em;">濡傛灉session涓瓨鍦ㄧ浉鍚屾寔涔呭寲鏍囪瘑(identifier)鐨勫疄渚嬶紝鐢ㄧ敤鎴風粰鍑虹殑瀵硅薄瑕嗙洊session宸叉湁鐨勬寔涔呭疄渚?</p> <p style="text-indent: 2em;">(1)褰撴垜浠嬌鐢╱pdate鐨勬椂鍊欙紝鎵ц瀹屾垚鍚庯紝浼氭姏鍑哄紓甯?</p> <p style="text-indent: 2em;">(2)浣嗗綋鎴戜滑浣跨敤merge鐨勬椂鍊欙紝鎶婂鐞嗚嚜鐢辨佺殑po瀵硅薄A鐨勫睘鎬opy鍒皊ession褰撲腑澶勪簬鎸佷箙鎬佺殑po鐨勫睘鎬т腑錛屾墽琛屽畬鎴愬悗鍘熸潵鏄寔涔呯姸鎬佽繕鏄寔涔呮侊紝鑰屾垜浠彁渚涚殑A榪樻槸鑷敱鎬?/p> <p style="text-indent: 2em;">鍏乫lush鍜寀pdate鍖哄埆</p> <p style="text-indent: 2em;">榪欎袱涓殑鍖哄埆濂界悊瑙?/p> <p style="text-indent: 2em;">update鎿嶄綔鐨勬槸鍦ㄨ嚜鐢辨佹垨鑴辯鐘舵?鍥爏ession鐨勫叧闂屽浜庤劚綆$姸鎬?鐨勫璞?/updateSQL</p> <p style="text-indent: 2em;">鑰宖lush鏄搷浣滅殑鍦ㄦ寔涔呯姸鎬佺殑瀵硅薄銆?/p> <p style="text-indent: 2em;">榛樿鎯呭喌涓嬶紝涓涓寔涔呯姸鎬佺殑瀵硅薄鐨勬敼鍔紙鍖呭惈set瀹瑰櫒錛夋槸涓嶉渶瑕乽pdate鐨勶紝鍙浣犳洿鏀逛簡瀵硅薄鐨勫鹼紝絳夊緟hibernate flush灝辮嚜鍔ㄦ洿鏂版垨淇濆瓨鍒版暟鎹簱浜嗐俬ibernate flush鍙戠敓鍦ㄤ互涓嬪嚑縐嶆儏鍐典腑錛?/p> <p style="text-indent: 2em;">1錛?璋冪敤鏌愪簺鏌ヨ鐨勫拰鎵嬪姩flush(),session鐨勫叧闂丼essionFactory鍏抽棴緇撳悎 </p> <p style="text-indent: 2em;">get()涓涓璞★紝鎶婂璞$殑灞炴ц繘琛屾敼鍙?鎶婅祫婧愬叧闂?/p> <p style="text-indent: 2em;">2錛宼ransaction commit鐨勬椂鍊欙紙鍖呭惈浜唂lush錛?/p> <p style="text-indent: 2em;">涓冦乴ock鍜寀pdate鍖哄埆</p> <p style="text-indent: 2em;">update鏄妸涓涓凡緇忔洿鏀硅繃鐨勮劚綆$姸鎬佺殑瀵硅薄鍙樻垚鎸佷箙鐘舵?/p> <p style="text-indent: 2em;">lock鏄妸涓涓病鏈夋洿鏀硅繃鐨勮劚綆$姸鎬佺殑瀵硅薄鍙樻垚鎸佷箙鐘舵?閽堝鐨勬槸鍥燬ession鐨勫叧闂屽浜庤劚綆$姸鎬佺殑po瀵硅薄(2)錛屼笉鑳介拡瀵瑰洜delete鑰屽浜庤劚綆$姸鎬佺殑po瀵硅薄)</p> <p style="text-indent: 2em;">瀵瑰簲鏇存敼涓涓褰曠殑鍐呭錛屼袱涓殑鎿嶄綔涓嶅悓錛?/p> <p style="text-indent: 2em;">update鐨勬搷浣滄楠ゆ槸錛?/p> <p style="text-indent: 2em;">(1)灞炴ф敼鍔ㄥ悗鐨勮劚綆$殑瀵硅薄鐨勪慨鏀?>璋冪敤update</p> <p style="text-indent: 2em;">lock鐨勬搷浣滄楠ゆ槸錛?/p> <p style="text-indent: 2em;">(2)璋冪敤lock鎶婃湭淇敼鐨勫璞′粠鑴辯鐘舵佸彉鎴愭寔涔呯姸鎬?->鏇存敼鎸佷箙鐘舵佺殑瀵硅薄鐨勫唴瀹?->絳夊緟flush鎴栬呮墜鍔╢lush</p> <p style="text-indent: 2em;">鍏乧lear鍜宔vcit鐨勫尯鍒?/p> <p style="text-indent: 2em;">clear瀹屾暣鐨勬竻闄ession緙撳瓨</p> <p style="text-indent: 2em;">evcit(obj)鎶婃煇涓寔涔呭寲瀵硅薄浠巗ession鐨勭紦瀛樹腑娓呯┖銆?/p> </div> </div> </div> <img src ="http://m.tkk7.com/sealyu/aggbug/291759.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/sealyu/" target="_blank">seal</a> 2009-08-19 11:00 <a href="http://m.tkk7.com/sealyu/archive/2009/08/19/291759.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>EJB3.0涓叧浜嶫pa鐨勫鍚堜富閿紙杞級http://m.tkk7.com/sealyu/archive/2009/08/19/291730.htmlsealsealWed, 19 Aug 2009 00:23:00 GMThttp://m.tkk7.com/sealyu/archive/2009/08/19/291730.htmlhttp://m.tkk7.com/sealyu/comments/291730.htmlhttp://m.tkk7.com/sealyu/archive/2009/08/19/291730.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/291730.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/291730.html Student瀹炰綋綾伙細
 1 import javax.persistence.Column;
 2 import javax.persistence.Entity;
 3 import javax.persistence.Id;
 4 import javax.persistence.IdClass;
 5 
 6 @Entity
 7 @IdClass(StudentPK.class)
 8 public class Student {
 9     
10     private String sid;
11     private String name;
12     private String age;
13 
14     @Id
15     public String getSid() {
16         return sid;
17     }
18     public void setSid(String sid) {
19         this.sid = sid;
20     }
21     
22     @Id
23     public String getName() {
24         return name;
25     }
26     public void setName(String name) {
27         this.name = name;
28     }
29     
30     @Column
31     public String getAge() {
32         return age;
33     }
34     public void setAge(String age) {
35         this.age = age;
36     }
37 
38 }
涓婚敭StudentPK綾伙細
 1 import java.io.Serializable;
 2 
 3 public class StudentPK implements Serializable{
 4     
 5     private static final long serialVersionUID = 1L;
 6     
 7     private String sid;
 8     private String name;
 9     
10     public StudentPK(){}
11     public StudentPK(String sid,String name){
12         this.sid = sid;
13         this.name = name;
14     }
15     
16     public String getSid() {
17         return sid;
18     }
19     public void setSid(String sid) {
20         this.sid = sid;
21     }
22     public String getName() {
23         return name;
24     }
25     public void setName(String name) {
26         this.name = name;
27     }
28     
29     @Override
30     public int hashCode() {
31         final int prime = 31;
32         int result = 1;
33         result = prime * result + ((name == null? 0 : name.hashCode());
34         result = prime * result + ((sid == null? 0 : sid.hashCode());
35         return result;
36     }
37     
38     @Override
39     public boolean equals(Object obj) {
40         if (this == obj)
41             return true;
42         if (obj == null)
43             return false;
44         if (getClass() != obj.getClass())
45             return false;
46         final StudentPK other = (StudentPK) obj;
47         if (name == null) {
48             if (other.name != null)
49                 return false;
50         } else if (!name.equals(other.name))
51             return false;
52         if (sid == null) {
53             if (other.sid != null)
54                 return false;
55         } else if (!sid.equals(other.sid))
56             return false;
57         return true;
58     }
59     
60 }
涓婚敭綾誨繀闇婊¤凍涓嬪垪鏉′歡錛?br /> (1)蹇呴渶琚簭鍒楀寲
(2)蹇呴渶鏈変竴涓叕鍏辯殑鏃犲弬鏋勯犳柟娉?br /> (3)蹇呴渶瀹炵幇equals()鍜宧ashCode()鏂規硶

褰撲綘鏌ヨStudent瀹炰綋鏃跺繀欏諱嬌鐢ㄤ富閿被鎵嶈兘璇嗗埆瀹炰綋濡?
    StudentPK pk = new StudentPK("bj1001","jakin");
    Student student 
= entityManager.find(Student.class, pk);
鍦ㄦ祴璇曟湰渚嬫椂絎旇呯姱浜嗕袱涓綆綰х殑閿欒錛屼竴鏄祴璇曠幆澧冪殑鏁版嵁搴撲笉鏄」鐩繍琛岀殑鏁版嵁搴擄紝浜屾槸瀹炰綋綾諱腑鐨勫睘鎬у懡鍚嶇敤浜嗗叧閿瓧錛岃寮曚互涓烘垝銆?

seal 2009-08-19 08:23 鍙戣〃璇勮
]]>
鏍囨敞鏋氫婦綾誨瀷@Enumerated錛堣漿錛?/title><link>http://m.tkk7.com/sealyu/archive/2009/06/30/284749.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Tue, 30 Jun 2009 05:18:00 GMT</pubDate><guid>http://m.tkk7.com/sealyu/archive/2009/06/30/284749.html</guid><wfw:comment>http://m.tkk7.com/sealyu/comments/284749.html</wfw:comment><comments>http://m.tkk7.com/sealyu/archive/2009/06/30/284749.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/sealyu/comments/commentRss/284749.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/sealyu/services/trackbacks/284749.html</trackback:ping><description><![CDATA[<p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">瀹炰綋</span><font face="Times New Roman">Entity</font><span style="font-family: 瀹嬩綋;">涓氳繃</span><font face="Times New Roman">@Enumerated</font><span style="font-family: 瀹嬩綋;">鏍囨敞鏋氫婦綾誨瀷錛屼緥濡傚皢</span><font face="Times New Roman">CustomerEO</font><span style="font-family: 瀹嬩綋;">瀹炰綋涓鍔犱竴涓?/span><font face="Times New Roman">CustomerType</font><span style="font-family: 瀹嬩綋;">綾誨瀷鐨勬灇涓懼瀷灞炴э紝鏍囨敞瀹炰綋鍚庣殑浠g爜濡備笅鎵紺恒?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">@Entity</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">@Table(name = "customer")</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">public class CustomerEO implements java.io.Serializable {</font></p> <p style="margin: 0cm 0cm 0pt auto; text-indent: 21pt;"><span style="font-family: 榛戜綋;"><font style="background-color: #e0e0e0;">……</font></span></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         private CustomerType type;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         @Enumerated(EnumType.STRING)</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public CustomerType getType() {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   return type;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public void setType(CustomerType type) {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   this.type = type;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         public enum CustomerType {</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">                   COMPETITOR, INVESTOR, PARTNER, VENDER</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         }</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">}</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">鍦ㄥ疄浣撲腑铏界劧鏍囨敞鎴愭灇涓劇被鍨嬶紝浣嗗綋瀹炰綋鎸佷箙鍖栧悗錛岃〃涓墍瀵瑰簲鐨勫間粛鏃ф槸鍩烘湰鐨勬暟鎹被鍨嬶紝浠ヤ笂浠g爜鍒涘緩琛ㄧ殑</span><font face="Times New Roman">SQL</font><span style="font-family: 瀹嬩綋;">璇彞鏄細</span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">CREATE TABLE customer (</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         id int(20) NOT NULL auto_increment,</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         name varchar(255),</font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         type varchar(255),</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         PRIMARY KEY (id)</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">)</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="font-family: 瀹嬩綋;"><font size="3">浣跨敤鏋氫婦綾誨瀷鍚庯紝鍦ㄥ垱寤哄疄浣撴椂渚垮彲浠ョ洿鎺ュ紩鐢ㄦ灇涓劇被鍨嬶紝渚嬪浠ヤ笅浠g爜鎵紺恒?/font></span></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">CustomerEO customer = new CustomerEO();</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">customer.setName("Janet2");</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">customer.setType(<strong>CustomerType.PARTNER</strong>);</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">entityManager.persist(customer);</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">鍦ㄤ嬌鐢?/span><font face="Times New Roman">@Enumerated</font><span style="font-family: 瀹嬩綋;">娉ㄩ噴鏃訛紝闇瑕佹敞鎰忎互涓嬪嚑涓棶棰橈細</span></font></p> <p style="margin: 0cm 0cm 0pt 21pt;"><span style="font-family: Wingdings;"><font size="3">l</font><span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span><font size="3"><span style="font-family: 瀹嬩綋;">鍥犱負鏋氫婦綾誨瀷鐨勬湁鍚嶇О鍜屽間袱涓睘鎬э紝鎵浠ュ湪鎸佷箙鍖栨椂鍙互閫夋嫨鎸佷箙鍖栧悕縐版垨鏄寔涔呭寲鍊箋傞氳繃</span><font face="Times New Roman">EnumType</font><span style="font-family: 瀹嬩綋;">鏉ュ畾涔夛紝瀹冩湁涓や釜鍊煎涓嬫墍紺恒?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">public enum EnumType {</font></p> <p style="margin: 0cm 0cm 0pt auto; text-indent: 21pt;"><font style="background-color: #e0e0e0;">ORDINAL,</font></p> <p style="margin: 0cm 0cm 0pt auto; text-indent: 21pt;"><font style="background-color: #e0e0e0;">STRING</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">}</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹嬩綋;">琛ㄧず鎸佷箙鍖栫殑涓烘灇涓劇被鍨嬬殑鍊鹼紝</span><font face="Times New Roman">STRING</font><span style="font-family: 瀹嬩綋;">琛ㄧず鎸佷箙鍖栫殑涓烘灇涓劇被鍨嬬殑鍚嶇О銆傞粯璁や負</span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹嬩綋;">錛屾寔涔呭寲鍊箋備緥濡備互涓婄ず渚嬩腑鏍囨敞鐨勪負</span><font face="Times New Roman">STRING</font><span style="font-family: 瀹嬩綋;">錛岃繖鏍鋒寔涔呭寲瀹炰綋鍚庯紝鏁版嵁搴撲腑淇濆瓨鐨勬槸鏋氫婦綾誨瀷鐨勫悕縐幫紝濡傚浘</span><span style="font-family: 瀹嬩綋;">鎵紺恒?/span></font></p> <p style="margin: 0cm 0cm 12pt;" align="center"><span style="font-family: 瀹嬩綋;"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/EJB_JPA/5.5.jpg" /></span></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">鑻ユ鏃舵敼鎴?/span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹嬩綋;">錛屼唬鐮佸涓嬶細</span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         @Enumerated(EnumType.ORDINAL)</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public CustomerType getType() {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   return type;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">鍒欏悓鏍鋒寔涔呭寲鐨勫疄浣撳悗錛屾暟鎹簱淇濆瓨鐨勭粨鏋滃鍥?/span><span style="font-family: 瀹嬩綋;">鎵紺恒?/span></font></p> <p style="margin: 0cm 0cm 12pt;" align="center"><span style="font-family: 瀹嬩綋;"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/EJB_JPA/5.6.jpg" /></span></p> <p style="margin: 0cm 0cm 0pt 21pt;"><span style="font-family: Wingdings;"><font size="3">l</font><span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span><font size="3"><span style="font-family: 瀹嬩綋;">濡備綍閫夋嫨</span><font face="Times New Roman">STRING</font><span style="font-family: 瀹嬩綋;">鍜?/span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹嬩綋;">錛?/span></font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">濡傛灉浣跨敤</span><font face="Times New Roman">STRING</font><span style="font-family: 瀹嬩綋;">淇濆瓨錛岃櫧鐒朵粠鏁版嵁搴撲腑鏌ヨ鏁版嵁鏃墮潪甯哥洿瑙傦紝鑳藉娓呮鐨勭湅鍑鴻綾誨瀷浠h〃鎰忎箟錛屼絾榪欐牱涔熶細甯︽潵鍏朵粬鐨勯棶棰樸傝嫢姝ゆ椂鏋氫婦綾誨瀷鐨勫畾涔夋敼鍙橈紝渚嬪涓婁緥涓殑鏋氫婦綾誨瀷鍚嶇О鏀逛負錛?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public enum CustomerType {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">                   CUST_COMPETITOR, INVESTOR, PARTNER, VENDER</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">鍒欐鏃舵暟鎹簱涓繚瀛樼殑“</span><font face="Times New Roman">COMPETITOR</font><span style="font-family: 瀹嬩綋;">”鐨勫煎皢涓嶈兘杞寲涓烘灇涓劇被鍨?/span><font face="Times New Roman">CustomerType</font><span style="font-family: 瀹嬩綋;">涓殑“</span><font face="Times New Roman">CUST_COMPETITOR</font><span style="font-family: 瀹嬩綋;">”鐨勫箋備絾鑻ヤ嬌鐢?/span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹嬩綋;">鍒欎笉浼氬甫鏉ヨ繖縐嶉棶棰樸傛墍浠ュ緩璁嬌鐢?/span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹嬩綋;">綾誨瀷鏉ユ寔涔呭寲鏋氫婦綾誨瀷銆?/span></font></p> <p style="margin: 0cm 0cm 0pt 21pt;"><span style="font-family: Wingdings;"><font size="3">l</font><span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span><font size="3"><span style="font-family: 瀹嬩綋;">鏋氫婦綾誨瀷鐨勫畾涔変綅緗紝瀹炰綋澶栭儴</span><font face="Times New Roman">VS</font><span style="font-family: 瀹嬩綋;">瀹炰綋鍐呴儴銆?/span></font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">涓婁緥涓?/span><font face="Times New Roman">CustomerType</font><span style="font-family: 瀹嬩綋;">鏋氫婦綾誨瀷瀹氫箟鍦?/span><font face="Times New Roman">CustomerEO</font><span style="font-family: 瀹嬩綋;">瀹炰綋鍐呴儴錛岃繖鏄洜涓哄彧鏈?/span><font face="Times New Roman">CustomerEO</font><span style="font-family: 瀹嬩綋;">榪欎釜瀹炰綋浼氫嬌鐢?/span><font face="Times New Roman">CustomerType</font><span style="font-family: 瀹嬩綋;">綾誨瀷錛屽叾浠栫殑瀹炰綋涓嶄細浣跨敤璇ョ被鍨嬨傝綾誨瀷涓庤繖涓疄浣撳叧緋葷揣瀵嗚仈緋匯?/span></font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">浣嗚嫢姝ゆ椂澶氫釜瀹炰綋鍏敤涓涓灇涓劇被鍨嬫椂錛屽垯鍙互灝嗘灇涓劇被鍨嬪崟鐙畾涔夛紝瀹氫箟鍦ㄥ疄浣撶殑澶栭儴銆傛湁榪欐牱涓涓灇涓劇被鍨?/span><font face="Times New Roman">BusinessLine</font><span style="font-family: 瀹嬩綋;">錛屽畠瀹氫箟鍦ㄥ疄浣撳閮紝浠g爜濡備笅錛?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">public enum BusinessLine {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         REAL_ESTATE,FINANCE, NON_PROFIT</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">}</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹嬩綋;">渚嬪</span><font face="Times New Roman">CustomerEO</font><span style="font-family: 瀹嬩綋;">瀹炰綋澧炲姞涓涓?/span><font face="Times New Roman">BusinessLine</font><span style="font-family: 瀹嬩綋;">鐨勬灇涓劇被鍨嬶紝浠g爜濡備笅鎵紺恒?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">@Entity</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">@Table(name = "customer")</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">public class CustomerEO implements java.io.Serializable {</font></p> <p style="margin: 0cm 0cm 0pt auto; text-indent: 21pt;"><span style="font-family: 榛戜綋;"><font style="background-color: #e0e0e0;">……</font></span></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         private BusinessLine businessLine;</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         @Enumerated(EnumType.STRING)</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public BusinessLine getBusinessLine() {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   return businessLine;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public void setBusinessLine(BusinessLine businessLine) {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   this.businessLine = businessLine;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">}</font></p> <img src ="http://m.tkk7.com/sealyu/aggbug/284749.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/sealyu/" target="_blank">seal</a> 2009-06-30 13:18 <a href="http://m.tkk7.com/sealyu/archive/2009/06/30/284749.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>org.hibernate.HibernateException: Found shared references to a collectionhttp://m.tkk7.com/sealyu/archive/2009/05/06/269180.htmlsealsealWed, 06 May 2009 02:45:00 GMThttp://m.tkk7.com/sealyu/archive/2009/05/06/269180.htmlhttp://m.tkk7.com/sealyu/comments/269180.htmlhttp://m.tkk7.com/sealyu/archive/2009/05/06/269180.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/269180.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/269180.htmlBeanUtil.copyProperties()鐨勬柟寮忓鍒朵竴涓疄浣揵ean鐨勬椂鍊欙紝濡傛灉榪欎釜瀹炰綋bean鏈変竴涓泦鍚堝睘鎬э紝鍥犱負榪欎釜鏂規硶鍙槸鍋氫簡涓涓祬鎷瘋礉錛屾墍浠ユ柊鎷瘋礉鐨勫疄浣撹窡鏃х殑瀹炰綋寮曠敤浜嗗悓涓涓泦鍚堬紝鑰岃繖鍦╤ibernate涓槸涓嶅厑璁哥殑錛屾墍浠ユ姤閿欍?br /> 瑙e喅鏂規硶錛?br /> 鍦ㄦ嫹璐濆悗錛屾柊寤轟竴涓泦鍚堬紝灝嗗師鏉ョ殑闆嗗悎鍏冪礌娣誨姞榪涘幓錛屽茍璧嬪肩粰鏂版嫹璐濈殑瀹炰綋銆?br />

seal 2009-05-06 10:45 鍙戣〃璇勮
]]>
Sorted collections in Hibernatehttp://m.tkk7.com/sealyu/archive/2009/05/05/268982.htmlsealsealTue, 05 May 2009 07:09:00 GMThttp://m.tkk7.com/sealyu/archive/2009/05/05/268982.htmlhttp://m.tkk7.com/sealyu/comments/268982.htmlhttp://m.tkk7.com/sealyu/archive/2009/05/05/268982.html#Feedback0http://m.tkk7.com/sealyu/comments/commentRss/268982.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/268982.htmlProblem

We want a collection field belonging to an entity object to be sorted.

Solution

There are two possible solutions:

  • If you want the collection to be sorted when loaded, but not necessarily keep that sorting order all the time, use the OrderBy annotation where you set the actual ORDER BY SQL clause. In that case, the collection can be of any type (e.g. Collection).
  • If you want the collection to be sorted at all times, make it of type SortedSet or SortedMap and set the Sort annotation.

    • If the objects in the collection implements Comparable set type to SortType.NATURAL.
    • Otherwise set it to SortType.COMPARATOR and specify your own Comparator class in the comparator attribute.

Remarks

  • There exists an annotation with name OrderBy in both the javax.persistence package as well as the org.hibernate.annotations package. The former should be used.


seal 2009-05-05 15:09 鍙戣〃璇勮
]]>
javax.persistence.PersistenceException: org.hibernate.MappingException: Cannot use identity column key generation with <union-subclass> mapping for xxxxxxxxxxxxxxxxhttp://m.tkk7.com/sealyu/archive/2009/02/09/253902.htmlsealsealMon, 09 Feb 2009 06:19:00 GMThttp://m.tkk7.com/sealyu/archive/2009/02/09/253902.htmlhttp://m.tkk7.com/sealyu/comments/253902.htmlhttp://m.tkk7.com/sealyu/archive/2009/02/09/253902.html#Feedback1http://m.tkk7.com/sealyu/comments/commentRss/253902.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/253902.html State: FAILED
  Reason: javax.persistence.PersistenceException: org.hibernate.MappingException: Cannot use identity column key generation with <union-subclass> mapping for: com.xxx.xxxx

鍚庢潵鍙戠幇闂鎵鍦細

緇ф壙絳栫暐鎴戦夋嫨浜咢Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)錛?br /> 鍚屾椂鍦ㄧ埗綾諱腑璁劇疆浜咺D鐢熸垚絳栫暐涓猴細
@Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }
鑰屽湪榪欑絳栫暐涓嬩嬌鐢╱nnion-subclass 鍏冪礌鏃?鐖剁被鐨勬爣璇嗗睘鎬х敓鎴愬櫒涓嶈兘浣跨敤“indentity” 錛屽皢絳栫暐鏀逛負 @GeneratedValue(strategy=GenerationType.TABLE)
闂瑙e喅銆?br />

seal 2009-02-09 14:19 鍙戣〃璇勮
]]>
EJB/JPA緇ф壙璇﹁В(杞?http://m.tkk7.com/sealyu/archive/2009/01/05/249911.htmlsealsealMon, 05 Jan 2009 06:18:00 GMThttp://m.tkk7.com/sealyu/archive/2009/01/05/249911.htmlhttp://m.tkk7.com/sealyu/comments/249911.htmlhttp://m.tkk7.com/sealyu/archive/2009/01/05/249911.html#Feedback1http://m.tkk7.com/sealyu/comments/commentRss/249911.htmlhttp://m.tkk7.com/sealyu/services/trackbacks/249911.html 姣忎釜綾誨垎灞傜粨鏋勪竴寮犺〃(table per class hierarchy)
姣忎釜瀛愮被涓寮犺〃(table per subclass)
姣忎釜鍏蜂綋綾諱竴寮犺〃(table per concrete class)

涓銆佹瘡涓被鍒嗗眰緇撴瀯涓寮犺〃(table per class hierarchy)
       榪欑鏄犲皠鏂瑰紡鍙渶涓哄熀綾誨垱寤轟竴涓〃鍗沖彲銆傚湪琛ㄤ腑涓嶄粎鎻愪緵鍩虹被鎵鏈夊睘鎬у搴旂殑瀛楁錛岃繕瑕佹彁渚涙墍鏈夊瓙綾誨睘鎬у搴旂殑瀛楁錛屾澶栬繕闇瑕佷竴涓瓧孌電敤浜庡尯鍒嗗瓙綾葷殑鍏蜂綋綾誨瀷
       瑕佷嬌鐢ㄦ瘡涓被鍒嗗眰緇撴瀯涓寮犺〃(table per class hierarchy) 絳栫暐錛岄渶瑕佹妸@javax.persistence.Inheritance 娉ㄩ噴鐨?font color="#0000ff">strategy
灞炴ц緗負InheritanceType.SINGLE_TABLE銆傞櫎闈炰綘瑕佹敼鍙樺瓙綾葷殑鏄犲皠絳栫暐錛屽惁鍒?strong>@Inheritance 娉ㄩ噴鍙兘鏀懼湪緇ф壙灞傛鐨勫熀綾?/strong>銆傞氳繃閴村埆瀛楁鐨勫鹼紝鎸佷箙鍖栧紩鎺e彲浠ュ尯鍒嗗嚭鍚勪釜綾伙紝騫朵笖鐭ラ亾姣忎釜綾誨搴旈偅浜涘瓧孌點?font color="#cc99ff">閴村埆瀛楁
閫氳繃@javax.persistence.DiscriminatorColumn 娉ㄩ噴榪涜瀹氫箟錛?font color="#0000ff">name
灞炴у畾涔?font color="#cc99ff">閴村埆瀛楁鐨勫垪鍚?/font>錛?/font>discriminatorType 灞炴у畾涔?font color="#cc99ff">閴村埆瀛楁鐨勭被鍨?/font>錛堝彲閫夊兼湁錛歋tring, Char, Integer錛夛紝濡傛灉閴村埆瀛楁鐨勭被鍨嬩負String 鎴朇har錛屽彲浠ョ敤length 灞炴у畾涔夊叾闀垮害銆?font color="#ff0000">@DiscriminatorValue 娉ㄩ噴涓虹戶鎵垮叧緋諱腑鐨?font color="#cc99ff">姣忎釜綾誨畾涔夐壌鍒?/font>錛屽鏋滀笉鎸囧畾閴村埆鍊鹼紝榛樿閲囩敤綾誨悕
渚?
    @SuppressWarnings("serial")
    @Entity
    @Table(name="Vehicle_Hierarchy")
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name="Discriminator",
                                         discriminatorType = DiscriminatorType.STRING,
                                         length=30)
    @DiscriminatorValue("Vehicle")
    public class Vehicle implements Serializable{       //鍩虹被
    private Long id;
    private Short speed;//閫熷害
    @Id
    @GeneratedValue
    @Column(columnDefinition="integer")//鎸囧畾浣跨敤閫傞厤Integer闀垮害鐨勬暟鎹被鍨?br />     public Long getId() {
    return id;
    }
    public void setId(Long id) {
    this.id = id;
    }

    @SuppressWarnings("serial")
    @Entity
    @DiscriminatorValue("Car")
    public class Car extends Vehicle{        //Vehicle鐨勫瓙綾?/font>
    private String engine;//鍙戝姩鏈?br />     @Column(nullable=true,length=30)
    public String getEngine() {
    return engine;
    }
    public void setEngine(String engine) {
    this.engine = engine;
        }
    }


     @SuppressWarnings("serial")
     @Entity
     @DiscriminatorValue("Camion")
     public class Camion extends Car{         //Car鐨勫瓙綾?/font>
     private String container;//闆嗚綆?br />      @Column(nullable=true,length=30)
     public String getContainer() {
     return container;
     }
     public void setContainer(String container) {
     this.container = container;
          }
     }
鍒嗘瀽:
       鍙? 浠ョ湅鍑猴紝姣忎釜瀛愮被娌℃湁鍗曠嫭鐨勬槧灝勶紝鍦ㄦ暟鎹簱涓病鏈夊搴旂殑琛ㄥ瓨鍦ㄣ傝屽彧鏈変竴涓褰曟墍鏈夎嚜韜睘鎬у拰瀛愮被鎵鏈夊睘鎬х殑琛紝鍦ㄥ熀綾諱負Vehicle 鐨勬椂鍊欙紝Discriminator 瀛楁鐨勫煎皢涓篤ehicle,鍦ㄥ瓙綾諱負Car 鐨勬椂鍊欙紝Discriminator 瀛楁鐨勫煎皢涓篊ar錛屽瓙綾諱負Camion 鐨勬椂鍊欙紝Discriminator 瀛楁鐨勫煎皢涓篊amion銆傞偅涔堬紝濡傛灉涓氬姟閫昏緫瑕佹眰Car 瀵硅薄鐨別ngine 灞炴т笉鍏佽涓簄ull錛屾樉鐒舵棤娉曞湪Vehicle_Hierarchy 琛ㄤ腑涓篹ngine 瀛楁瀹氫箟not null 綰︽潫錛屽彲瑙佽繖縐嶆槧灝勬柟寮忔棤娉曚繚璇佸叧緋繪暟鎹ā鍨嬬殑鏁版嵁瀹屾暣鎬с?/font>

浜屻佹瘡涓被鍒嗗眰緇撴瀯涓寮犺〃(table per class hierarchy)
       榪欑鏄犲皠鏂瑰紡涓烘瘡涓被鍒涘緩涓涓〃銆傚湪姣忎釜綾誨搴旂殑琛ㄤ腑鍙渶鍖呭惈鍜岃繖涓被鏈韓鐨勫睘鎬у搴旂殑瀛楁錛屽瓙綾誨搴旂殑琛ㄥ弬鐓х埗綾誨搴旂殑琛?浣跨敤姣忎釜瀛愮被涓寮犺〃 (table per subclass)絳栫暐錛岄渶瑕佹妸@javax.persistence.Inheritance 娉ㄩ噴鐨?strong>strategy 灞炴ц緗負InheritanceType.JOINED

     @SuppressWarnings("serial")
     @Entity
     @Inheritance(strategy=InheritanceType.JOINED)
     @Table(name="Vehicle")
     public class Vehicle implements Serializable{      //鍩虹被
     private Long id;
     private Short speed;//閫熷害
     @Id
     @GeneratedValue
     @Column(columnDefinition="integer")
     public Long getId() {
     return id;
     }
     public void setId(Long id) {
     this.id = id;
     }
     public Short getSpeed() {
     return speed;
     }
     public void setSpeed(Short speed) {
     this.speed = speed;
     }
     }

     @SuppressWarnings("serial")
     @Entity
     @Table(name="Car")
     @PrimaryKeyJoinColumn(name="CarID")     //鎶婁富閿搴旂殑鍒楀悕鏇存敼涓篊arID
     public class Car extends Vehicle{                 //Vehicle鐨勫瓙綾?/font>
     private String engine;//鍙戝姩鏈?br />      @Column(nullable=true,length=30)
     public String getEngine() {
     return engine;
     }
     public void setEngine(String engine) {
     this.engine = engine;
     }
     }

     @SuppressWarnings("serial")
     @Entity
     @Table(name="Camion")
     @PrimaryKeyJoinColumn(name="CamionID")     //鎶婁富閿搴旂殑鍒楀悕鏇存敼涓篊amionID
     public class Camion extends Car{                    //Car鐨勫瓙綾?/font>
     private String container;
     @Column(nullable=true,length=30)
     public String getContainer() {
     return container;
     }
     public void setContainer(String container) {
     this.container = container;
     }
     }
        榪欑鏄犲皠鏂瑰紡鏀寔澶氭佸叧鑱斿拰澶氭佹煡璇紝鑰屼笖絎﹀悎鍏崇郴鏁版嵁妯″瀷鐨勫父瑙勮璁¤鍒欍傚湪榪欑絳栫暐涓綘鍙互瀵瑰瓙綾葷殑灞炴у搴旂殑瀛楁瀹氫箟not null 綰︽潫銆傝絳栫暐鐨勭己鐐癸細
        瀹冪殑鏌ヨ鎬ц兘涓嶅涓婇潰浠嬬粛鐨勬槧灝勭瓥鐣ャ傚湪榪欑鏄犲皠絳栫暐涓嬶紝蹇呴』閫氳繃琛ㄧ殑鍐呰繛鎺ユ垨宸﹀榪炴帴鏉ュ疄鐜板鎬佹煡璇㈠拰澶氭佸叧鑱斻?br /> 閫夋嫨鍘熷垯錛氬瓙綾誨睘鎬ч潪甯稿錛岄渶瑕佸瀛愮被鏌愪簺灞炴у搴旂殑瀛楁榪涜not null 綰︽潫錛屼笖瀵規ц兘瑕佹眰涓嶆槸寰堜弗鏍兼椂錛屼紭鍏堥夋嫨璇ョ瓥鐣?/font>

涓夈佹瘡涓叿浣撶被涓寮犺〃(table per concrete class)
       榪欑鏄犲皠鏂瑰紡涓烘瘡涓被鍒涘緩涓涓〃銆傚湪姣忎釜綾誨搴旂殑琛ㄤ腑鍖呭惈鍜岃繖涓被鎵鏈夊睘鎬э紙鍖呮嫭浠庤秴綾葷戶鎵跨殑灞炴э級瀵瑰簲鐨勫瓧孌?浣跨敤姣忎釜鍏蜂綋綾諱竴寮犺〃(table per concrete class)絳栫暐錛岄渶瑕佹妸@javax.persistence.Inheritance 娉ㄩ噴鐨?strong>strategy 灞炴ц緗負InheritanceType.TABLE_PER_CLASS

        娉ㄦ剰:涓鏃︿嬌鐢ㄨ繖縐嶇瓥鐣ュ氨鎰忓懗鐫浣犱笉鑳戒嬌鐢ˋUTO generator 鍜孖DENTITY generator錛屽嵆涓婚敭鍊間笉鑳介噰鐢ㄦ暟鎹簱鑷姩鐢熸垚.

     @SuppressWarnings("serial")
     @Entity
     @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
     @Table(name="Vehicle")
     public class Vehicle implements Serializable{            //鍩虹被
     private Long id;
     private Short speed;//閫熷害
     @Id
     @Column(columnDefinition="integer")
     public Long getId() {
     return id;
     }
     public void setId(Long id) {
     this.id = id;
     }
     public Short getSpeed() {
     return speed;
     }
     public void setSpeed(Short speed) {
     this.speed = speed;
     }
     }

     @SuppressWarnings("serial")
     @Entity
     @Table(name="Car")
    public class Car extends Vehicle{               //Vehicle鐨勫瓙綾?/font>
     private String engine;//鍙戝姩鏈?br />      @Column(nullable=true,length=30)
     public String getEngine() {
     return engine;
     }
     public void setEngine(String engine) {
     this.engine = engine;
     }
     }

     @SuppressWarnings("serial")
     @Entity
     @Table(name="Camion")
     public class Camion extends Car{              //Car鐨勫瓙綾?/font>
     private String container;//闆嗚綆?br />      @Column(nullable=true,length=30)
     public String getContainer() {
     return container;
     }
     public void setContainer(String container) {
     this.container = container;
     }
     }

娉ㄦ剰:鍦ㄦ煡璇㈡椂,渚嬪: from Vehicle v
         鏌ヨ鎵鏈塚ehicle鏃訛紝鍥犱負浠栨槸鏈緇ф壙鏍戜腑鐨勬牴錛屾煡璇㈢粨鏋滀細寰楀埌鎵鏈夌戶鎵夸簬Vehicle綾葷殑璁板綍
(鏋勯犵殑SQL Where閮ㄥ垎錛?strong>where Discriminator in ('Car', 'Camion'))
         delete from Vehicle v
         鎵ц璇ユ搷浣滀細鍒犻櫎鑷韓瀵瑰簲璁板綍錛岃繕浼氬垹闄ゆ墍鏈夌戶鎵縑ehicle鐨勮褰?鍥犱負浠栨槸鏈緇ф壙鏍戜腑鐨勬牴錛屽氨鐩稿綋浜庢竻闄ゆ暣涓〃鐨勬暟鎹?br />
璇ョ瓥鐣ョ殑浼樼偣錛?br />                      鍦ㄨ繖縐嶇瓥鐣ヤ腑浣犲彲浠ュ瀛愮被鐨勫睘鎬у搴旂殑瀛楁瀹氫箟not null 綰︽潫銆?br /> 璇ョ瓥鐣ョ殑緙虹偣錛?br />                      涓嶇鍚堝叧緋繪暟鎹ā鍨嬬殑甯歌璁捐瑙勫垯錛屾瘡涓〃涓兘瀛樺湪灞炰簬鍩虹被鐨勫浣欑殑瀛楁銆傚悓鏃訛紝涓轟簡鏀寔絳栫暐鐨勬槧灝勶紝鎸佷箙鍖栫鐞嗚呴渶瑕佸喅瀹氫嬌鐢ㄤ粈涔堟柟娉曪紝涓縐嶆柟娉曟槸鍦? entity 杞藉叆鎴栧鎬佸叧鑱旀椂錛屽鍣ㄤ嬌鐢ㄥ嬈℃煡璇㈠幓瀹炵幇錛岃繖縐嶆柟娉曢渶瑕佸鏁版嵁搴撳仛鍑犳鏉ュ線鏌ヨ錛岄潪甯稿獎鍝嶆墽琛屾晥鐜囥傚彟涓縐嶆柟娉曟槸瀹瑰櫒閫氳繃浣跨敤SQLUNIOU 鏌ヨ鏉ュ疄鐜拌繖縐嶇瓥鐣ャ?br /> 閫夋嫨鍘熷垯錛?br />                      闄ら潪浣犵殑鐜板疄鎯呭喌蹇呴』浣跨敤榪欑絳栫暐錛屼竴鑸儏鍐典笅涓嶈閫夋嫨銆?/font>

seal 2009-01-05 14:18 鍙戣〃璇勮
]]>Interceptor 涓璷nFlushDirty()鍑芥暟鎵ц澶氭鐨勯棶棰橈紙Hibernate 鐨勪竴涓皬Bug錛?/title><link>http://m.tkk7.com/sealyu/archive/2008/05/09/199527.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Fri, 09 May 2008 07:59:00 GMT</pubDate><guid>http://m.tkk7.com/sealyu/archive/2008/05/09/199527.html</guid><wfw:comment>http://m.tkk7.com/sealyu/comments/199527.html</wfw:comment><comments>http://m.tkk7.com/sealyu/archive/2008/05/09/199527.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://m.tkk7.com/sealyu/comments/commentRss/199527.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/sealyu/services/trackbacks/199527.html</trackback:ping><description><![CDATA[鏈榪戝湪欏圭洰涓亣鍒頒竴涓鎬殑闂錛屽湪浣跨敤Hibernate鎷︽埅鍣ㄦ崟鑾峰疄浣撳彉鍖栧茍榪涜澶勭悊鏃訛紝鍙戠幇鍏朵腑鐨刼nFlushDirty()鍑芥暟鎵ц浜嗗緢澶氭錛屽鑷磋繘琛屽鐞嗘椂浜х敓寰堝閲嶅鏁版嵁銆傚叿浣撻棶棰樺涓嬶細<br /> 浣跨敤涓涓被緇ф壙Hibernate鐨凟mptyInterceptor綾繪潵瀵圭▼搴忎腑鐨勫疄浣撳彉鍖栬繘琛屾嫤鎴紝騫跺湪鍏朵腑鐨刼nFlushDirty()鍑芥暟涓鎹曡幏鐨勬暟鎹繘琛屽鐞嗭紝浜х敓瀵瑰簲鐨別vent鏁版嵁騫朵繚瀛樺埌鏁版嵁搴撲腑銆?渚嬪錛?br /> public class EventInterceptor extends EmptyInterceptor {<br /> public boolean onFlushDirty(<br />         Object entity,<br />         Serializable id,<br />         Object[] currentState,<br />         Object[] previousState,<br />         String[] propertyNames,<br />         Type[] types ) throws CallbackException {<br /> <br />         if(isAuditable(entity)){//濡傛灉璇ュ疄浣撻渶瑕佽璁板綍錛岀敓鎴愬搴旂殑event銆?br />             //姝ゅ鐢熸垚瀵瑰簲鐨別vent銆?br />         }<br />         return false;<br />     }<br /> <br /> 鍦ㄧ▼搴忔墽琛屽悗錛屽彂鐜板搴斾竴鏉″疄浣撶殑鍙樺寲鐢熸垚浜嗗鏉¢噸澶嶇殑event璁板綍錛岄潪甯鎬笉瑙c?br /> 鍚庢潵google鍙戠幇錛屾湁浜轟篃紕板埌榪囧搴旂殑闂錛屽茍鍦℉ibernage璁哄潧涓彁鍑鴻繃榪欎釜闂錛岄壌瀹氫負Hibernate鐨勪竴涓皬Bug銆?br /> 錛堝師鏂囧湴鍧錛歨ttp://forum.hibernate.org/viewtopic.php?t=940410&highlight=interceptor+onflushdirty錛?br /> 瑙e喅鏂規硶濡備笅錛?br /> 灝咶lushMode鏀逛負錛欶lushMode.COMMIT<br /> 鎴栬呬篃鍙互鎻愬墠榪涜flush()<br /> 閮藉彲浠ヨВ鍐寵繖涓棶棰樸?br /> <br /> <img src ="http://m.tkk7.com/sealyu/aggbug/199527.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/sealyu/" target="_blank">seal</a> 2008-05-09 15:59 <a href="http://m.tkk7.com/sealyu/archive/2008/05/09/199527.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>感谢您访问我们的网站,您可能还对以下资源感兴趣:</p> <a href="http://m.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> 主站蜘蛛池模板: <a href="http://ymiwang.com" target="_blank">香蕉免费在线视频</a>| <a href="http://yw835.com" target="_blank">AAAAA级少妇高潮大片免费看</a>| <a href="http://xy167.com" target="_blank">91福利免费体验区观看区</a>| <a href="http://www91v.com" target="_blank">亚洲精品无码永久中文字幕</a>| <a href="http://qq367.com" target="_blank">一级日本高清视频免费观看</a>| <a href="http://fuzhiwushu.com" target="_blank">亚洲综合色成在线播放</a>| <a href="http://352362.com" target="_blank">2022免费国产精品福利在线</a>| <a href="http://4p5e.com" target="_blank">亚洲色无码一区二区三区</a>| <a href="http://1314c.com" target="_blank">国产午夜无码精品免费看</a>| <a href="http://yu388.com" target="_blank">久久夜色精品国产噜噜亚洲AV</a>| <a href="http://www8x5188x.com" target="_blank">97在线视频免费播放</a>| <a href="http://fns92.com" target="_blank">亚洲人和日本人jizz</a>| <a href="http://gwcyy.com" target="_blank">午夜成人免费视频</a>| <a href="http://www96pg.com" target="_blank">黄色一级毛片免费</a>| <a href="http://linanhotel.com" target="_blank">亚洲国产精品久久久天堂</a>| <a href="http://3688008.com" target="_blank">日本黄网站动漫视频免费</a>| <a href="http://wwwkk5679.com" target="_blank">久久精品亚洲AV久久久无码</a>| <a href="http://zhuanjiao521.com" target="_blank">久久精品国产精品亚洲色婷婷</a>| <a href="http://airou08.com" target="_blank">国产成人精品免费久久久久</a>| <a href="http://zjlxblog.com" target="_blank">精品亚洲aⅴ在线观看</a>| <a href="http://ehiver.com" target="_blank">毛片大全免费观看</a>| <a href="http://321fafa.com" target="_blank">一级毛片在线播放免费</a>| <a href="http://my533.com" target="_blank">亚洲av片劲爆在线观看</a>| <a href="http://yuntao360.com" target="_blank">91情侣在线精品国产免费</a>| <a href="http://500308k.com" target="_blank">国产亚洲综合视频</a>| <a href="http://wwwav888.com" target="_blank">亚洲电影一区二区</a>| <a href="http://wyj4.com" target="_blank">啦啦啦www免费视频</a>| <a href="http://jdwx58.com" target="_blank">国产vA免费精品高清在线观看</a>| <a href="http://yule4.com" target="_blank">久久国产亚洲精品无码</a>| <a href="http://yy468.com" target="_blank">男女交性永久免费视频播放</a>| <a href="http://beidemei.com" target="_blank">v片免费在线观看</a>| <a href="http://catalna.com" target="_blank">亚洲视屏在线观看</a>| <a href="http://wenbye.com" target="_blank">亚洲AV中文无码乱人伦在线视色</a>| <a href="http://www-095666.com" target="_blank">东方aⅴ免费观看久久av</a>| <a href="http://changfafangzhi.com" target="_blank">亚洲一卡2卡4卡5卡6卡在线99 </a>| <a href="http://137499.com" target="_blank">亚洲精品无码成人片久久</a>| <a href="http://by4471.com" target="_blank">1000部羞羞禁止免费观看视频</a>| <a href="http://fsweicheng.com" target="_blank">亚洲kkk4444在线观看</a>| <a href="http://ss8kk.com" target="_blank">亚洲中文字幕无码久久综合网</a>| <a href="http://yy12345.com" target="_blank">99久久久国产精品免费无卡顿</a>| <a href="http://zjpcyh.com" target="_blank">一级黄色毛片免费看</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>