<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    hk2000c技術專欄

    技術源于哲學,哲學來源于生活 關心生活,關注健康,關心他人

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      111 隨筆 :: 1 文章 :: 28 評論 :: 0 Trackbacks

    #

    class ThreadA
    {
      public static void main(String[] args)
      {
        ThreadB b=new ThreadB();
        b.start();
        System.out.println("b is start....");
        synchronized(b)//括號里的b是什么意思,起什么作用?
        {
          try
          {
     System.out.println("Waiting for b to complete...");
     b.wait();//這一句是什么意思,究竟讓誰wait?
            System.out.println("Completed.Now back to main thread");
          }catch (InterruptedException e){}
        }
        System.out.println("Total is :"+b.total);
       }
    }


    class ThreadB extends Thread
    {
      int total;
      public void run()
      {
        synchronized(this)
        {
          System.out.println("ThreadB is running..");
          for (int i=0;i<100;i++ )
          {
            total +=i;
            System.out.println("total is "+total);
          }
          notify();
        }
      }
    }

    要分析這個程序,首先要理解notify()和wait(),為什么在前幾天紀錄線程的時候沒有紀錄這兩個方法呢,因為這兩個方法本來就不屬于Thread類,而是屬于最底層的object基礎類的,也就是說不光是Thread,每個對象都有notify和wait的功能,為什么?因為他們是用來操縱鎖的,而每個對象都有鎖,鎖是每個對象的基礎,既然鎖是基礎的,那么操縱鎖的方法當然也是最基礎了.

     再往下看之前呢,首先最好復習一下Think in Java的14.3.1中第3部分內容:等待和通知,也就是wait()和notify了.

    按照Think in Java中的解釋:"wait()允許我們將線程置入“睡眠”狀態,同時又“積極”地等待條件發生改變.而且只有在一個notify()或notifyAll()發生變化的時候,線程才會被喚醒,并檢查條件是否有變."

      我們來解釋一下這句話.
      "wait()允許我們將線程置入“睡眠”狀態",也就是說,wait也是讓當前線程阻塞的,這一點和sleep或者suspend是相同的.那和sleep,suspend有什么區別呢?

       區別在于"(wait)同時又“積極”地等待條件發生改變",這一點很關鍵,sleep和suspend無法做到.因為我們有時候需要通過同步(synchronized)的幫助來防止線程之間的沖突,而一旦使用同步,就要鎖定對象,也就是獲取對象鎖,其它要使用該對象鎖的線程都只能排隊等著,等到同步方法或者同步塊里的程序全部運行完才有機會.在同步方法和同步塊中,無論sleep()還是suspend()都不可能自己被調用的時候解除鎖定,他們都霸占著正在使用的對象鎖不放.
       而wait卻可以,它可以讓同步方法或者同步塊暫時放棄對象鎖,而將它暫時讓給其它需要對象鎖的人(這里應該是程序塊,或線程)用,這意味著可在執行wait()期間調用線程對象中的其他同步方法!在其它情況下(sleep啊,suspend啊),這是不可能的.
       但是注意我前面說的,只是暫時放棄對象鎖,暫時給其它線程使用,我wait所在的線程還是要把這個對象鎖收回來的呀.wait什么?就是wait別人用完了還給我啊!
       好,那怎么把對象鎖收回來呢?
       第一種方法,限定借出去的時間.在wait()中設置參數,比如wait(1000),以毫秒為單位,就表明我只借出去1秒中,一秒鐘之后,我自動收回.
       第二種方法,讓借出去的人通知我,他用完了,要還給我了.這時,我馬上就收回來.哎,假如我設了1小時之后收回,別人只用了半小時就完了,那怎么辦呢?靠!當然用完了就收回了,還管我設的是多長時間啊.

       那么別人怎么通知我呢?相信大家都可以想到了,notify(),這就是最后一句話"而且只有在一個notify()或notifyAll()發生變化的時候,線程才會被喚醒"的意思了.
       因此,我們可將一個wait()和notify()置入任何同步方法或同步塊內部,無論在那個類里是否準備進行涉及線程的處理。而且實際上,我們也只能在同步方法或者同步塊里面調用wait()和notify().

       這個時候我們來解釋上面的程序,簡直是易如反掌了.

       synchronized(b){...};的意思是定義一個同步塊,使用b作為資源鎖。b.wait();的意思是臨時釋放鎖,并阻塞當前線程,好讓其他使用同一把鎖的線程有機會執行,在這里要用同一把鎖的就是b線程本身.這個線程在執行到一定地方后用notify()通知wait的線程,鎖已經用完,待notify()所在的同步塊運行完之后,wait所在的線程就可以繼續執行.

    posted @ 2007-12-19 15:20 hk2000c 閱讀(7840) | 評論 (9)編輯 收藏

    public class Foo {
        // 似有靜態內部類, 只有當有引用時, 該類才會被裝載
        private static class LazyFoo {
           public static Foo foo = new Foo();
        }

        public static Foo getInstance() {
           return LazyFoo.foo;
        }
    }

    posted @ 2007-12-18 23:58 hk2000c 閱讀(351) | 評論 (0)編輯 收藏

    一、 金 絲 燕 的 特 征 與 特 性

    金絲燕種類大致有15種,會制造”燕窩”的燕子在生物學上被歸類為:動物界,脊椎動物門,脊椎動物亞門,鳥綱,雨燕目,雨燕科,金絲燕屬,種有:褐腰金絲燕,灰腰金絲燕,爪哇金絲燕,方尾金絲燕,短嘴金絲燕,白腰金絲燕,小白腰金絲燕,白喉針尾
    金絲燕,白腹金絲燕,小白腹金絲燕,戈式金絲燕等。
    根據八年來長期在印度尼西亞蘇門達臘島的觀察與實際建筑燕屋供金絲燕棲息,在燕屋內外裝設紅外線監視器,并于金絲燕入口處加設紅外線計數器,經長期觀察的結果,金絲燕晝出晚歸,成年金絲燕平均的身厚(含毛)約4cm,身長約10cm,雙翅展開約22cm,重約20g,腳短且軟,四個腳趾都朝前方無法抓握,所以不像一般的鳥類三個腳趾朝前一趾朝后,能棲息在電線上或在地上行走,金絲燕只能用四趾鉤住掛在垂直的墻壁或巖壁上,金絲燕對回音有很好的定位感,能在漆黑的環境中自由飛行,并找到原來的棲息點,
    成年金絲燕每年平均會有三次的產卵,每年4月8月12月是金絲燕的產卵高峰期,每次都產2個白色的卵,長約2.0cm,寬約1.2cm,重約3.5g,
    每次產卵前約30天開始筑巢,每次筑巢都是雌雄燕一起筑需約30天,卵的孵化期約20天,小金絲燕孵化出后雌雄金絲燕會一起哺育,約40天后小金絲燕即能自行飛翔覓食,且另行尋找棲息點,不再使用此巢,從筑巢到小金絲燕飛離需約90天,

    二、 金 絲 燕 的 繁 殖 周 期
    金絲燕會在小金絲燕飛離后(空巢期)約30天后,就會再進行第二次的筑巢,金絲燕每次的筑巢都會筑新的巢,若前次筑的巢尚在,金絲燕會在原有的窩上面再筑一次巢,并不會因前次筑的巢還在而不再筑新巢,所以采收燕窩并不會造成金絲燕無家可歸.


    三、 金 絲 燕 與 家 燕 的 區 別
    金絲燕覓食都是在飛行中進行的,而且只專吃會飛的昆蟲或小生物,而喝水除了喝雨水外,會低飛將嘴巴貼在水池的水面上,邊飛邊喝水.常見停留在電線上或棲息于屋檐下的燕子是雀形目,燕科,的家燕種,是用泥土來筑巢的,所以是不會制造”燕窩”的。

    四、 金 絲 燕 的 俗 稱
    金絲燕的俗稱”白燕(又稱官燕)”和”黃燕”,其實這兩種名稱,都是金絲燕的另一種稱呼,金絲燕所筑的”燕窩”其顏色,在剛筑好時,一般都是珍珠白的顏色,但時間一久會因金絲燕筑巢的所處的環境而有所不同,雖是筑在同一個環境上,也會因所筑的時間長短不同而有不同的顏色,一般上”燕窩”存在的時間久了其顏色會由珍珠白色慢慢轉變成灰白(即俗稱”白燕,又稱官燕”)或珍珠黃(即俗稱”黃燕”)的顏色,這是因受氧化的結果,屬自然現象,類似萷了皮的水黎放久了因接觸空氣中的氧,產生氧化作用而變色一樣;大多數的金絲燕從筑巢,生蛋,孵化,成長到小金絲燕離巢,大約需90天的時間,采收”燕窩”的時間,都是在小金絲燕已離巢時再進行采收,所以”燕窩”的顏色是很自然的灰白或珍珠黃。

    五、 屋 燕 的 習 性
    金絲燕筑巢于人工建筑的屋內,我們稱之為”屋燕”,因燕窩有其特殊的價值性,讓人們想盡辦法引金絲燕進屋來筑巢,一般通用的方法會用音響播放金絲燕的叫聲,引其入屋,并保持屋中無光線,溫度在28度C左右,濕度控制在90%以上,來營造一個金絲燕喜歡棲息與居住的環境。

    六、 燕 屋 的 生 態 環 境
    屋中有”燕窩”的屋主都會視金絲燕如珍寶,都會竭盡所能,盡心盡力的保護它,所以不可能讓其它的動物進出或棲息,尤其是貓頭鷹,貓頭鷹是金絲燕最大的天敵,屋主會將也金絲燕的入口做成----長60-90CM.高20-30CM.并在洞口四周加裝電燈.來防止貓頭鷹的進入外.更會將燕屋的環境保持的很好。

    七、 采 收 燕 窩 的 準 備
    所有的燕屋屋主好不容易才將金絲燕引進屋內棲息筑窩,絕對不會以殺雞取卵的方式來采收燕窩,所以我們都是在小金絲燕飛離巢后的空巢期,也就是在每年的2月6月10月才開始采收,且在采收每個燕窩之前都要先用鏡子以反射的方式撿查小金絲燕是否已飛離,在確定小金絲燕已飛離。

    八、 如 何 采 收 燕 窩
    所以我們都是在小金絲燕飛離巢后的空巢期,也就是在每年的2月6月10月才開始采收,且在采收每個燕窩之前都要先用在確定小金絲燕已飛離,采收工人才架設樓梯爬上采收,采收工人會先以冷開水將整個燕窩噴濕,再以小鏟子將燕窩取下.

    九、 堅 持 100% 人 工 精 挑 燕 窩
    剛采收的燕窩,因先前有噴水,需先分類晾干,以利保存,待日后分批慢慢清理,清理燕窩時須在恒溫恒濕無塵的環境中工作,將燕窩以冷開水浸濕清洗幾次,再用布蓋上約30分鐘使其軟化,再用不銹鋼的小夾子仔細的夾除異物,待異物處理完成后,盡量保持其原有的形狀,置放于恒溫,恒濕無塵的干燥室中干燥殺菌,經過檢驗之后再進包裝。

    十、 燕 窩 的 種 類 (一)
    以來源區分為:洞燕,屋燕,草燕,毛燕.。
    洞燕—較為灰黑或黃色,與毛及雜質較多,口感較為爽實.;屋燕—色較白,質松,毛少,口感較滑軟;草燕—平均枯草重量占整個窩90%以上;毛燕—平均羽毛重量占整個窩50%以上,一般”毛燕”都喜歡筑巢于山洞中。

    十一、 燕 窩 的 種 類 (二)
    以顏色區分為:白燕(又稱官燕),黃燕,紅燕(俗稱血燕);以形狀區分為:盞,塊,條,絲,碎。

    十二、 二 次 結 巢 的 燕 窩
    金絲燕每次的筑巢都會筑新的巢,若前次筑的巢尚在,金絲燕會在原有的窩上面再筑一次巢,并不會因前次筑的巢還在而不再筑新巢,所以采收燕窩并不會造成金絲燕無家可歸。

    posted @ 2007-12-08 09:27 hk2000c 閱讀(2190) | 評論 (0)編輯 收藏

    美國有超過1000家院校提供碩士及博士學位課程,而這類課程主要注重專科學系,以協助學生吸取不同的專門技能或進行深入研究。 


    美國研究生學位不同之處

    研究生學位分為專業學位及研究學位,而這兩類學位都有碩士程度及博士程度之分。

    在碩士程度,專業學位(如:工商管理碩士、教育碩士、藝術碩士),提供一些實用的專門技能及直接就業機會。以研究為主的碩士學位(如:文學碩士及理學碩士),則提供一般進行研究的經驗、獎學金以及修讀博士的機會。有些大學更有雙學位課程供選擇。舉例說,一般收讀商科及工業工程的學生便可同時獲得工商管理和理學的碩士學位。碩士學位通常可于1至2年時間完成。其中專業學位的學生需進行實習或工作;研究學位的學生則需撰寫論文或考試。碩士學位與博士學位課程內容的主要分別在于前者比較注重課堂功課,較少研討會及考試。雖然很多文學及理學碩士課程都要求學生呈交論文,但學生亦可能選擇豁免此要求。

      美國博士學位不好拿

    博士學位的目的在于訓練學者在一個特定學科內進行研究,而這些學位通常都包括課堂作業以及一個主要研究計劃。博士學位通常需4至6年時間全職才能完成。前兩年包括課堂、研討會、及有關閱讀,令學生能學到有關學科的全面知識。之后會有筆試或口試,測試學生的程度。成功完成考試及研究計劃并獲得認可后,便可得到候選資格。研究計劃一般需時1至2年時間,視乎學科而定。該學科教師會對學生的研究計劃作出指導及評核成績,不過學生需要獨立地進行其研究的工作。

      如何寫好推薦信

    選擇推薦人的過程中,應從自己以往在課堂、課外活動及工作上所認識的人著手。一個理想的推薦人應具備以下條件:他(她)對你有很高的評價;擁有對你的多方面認知;對你所選的院校及學科有所認識;曾教導過為數不少的學生和可以將你與其他學生作正面的比較;入學辦事處小組對其有所認識,而且會認真衡量其所作出的判斷;擁有上佳的書寫溝通技巧。當然,沒有人可以完全符合以上條件,只需選擇最接近自己理想中的人選便行了。

    如果你是工作了數年后回歸校園,你未必可以找回你大學時的教授。如果這樣,便應聯絡你申請的院校了解有關的政策。

    他們或可讓你以雇主或上司的推薦信代替。當你決定了替你寫推薦信的人選后,可以詢問他們是否覺得自己對你有足夠的認識去寫封有意義的推薦信。向他們提供所需資料,協助他們替你寫封詳盡且有效的推薦信。

    譬如:成績單、履歷表、申請入學時寫的文章副本及你的研究論文副本,都是有幫助的資料。推薦信必須由推薦人親自書寫及簽署。如果能將一個貼有空郵郵票及已寫有地址的信封給你的推薦人便更好。

      個人自述

    個人自述亦是申請入學過程中的重要一環。從你呈交的文章中,入學處小組可更清晰地認識你的自發能力和你對所選學科的投入程度;你對該課程的期望;你的寫作能力;你主要的學習興趣;你的研究或工作經驗;你的學術背景;你目前和長遠的目標;你申請入讀的原因;或你所擁有的其它特長。文章的主題可能很明確或很遼闊,而不同的院校對文章長短的要求亦有所不同。最重要是要仔細閱讀問題及作答。切忌將同一篇文章放進不同的申請信內。有關撰寫文章的其它詳情和建議可參考Peterson''sGuidestoGraduatePrograms及其它有關資料。

      選擇研究院課程

    比較美國各種研究院課程之時,除卻地點、學校大小及收費之外,另外還有些更重要的因素應注意。

    以下的幾項重點便能幫助決定哪家院校最適合你:

      學習科目

    ●認清自己喜愛的學科及研究的興趣。你或需請教教授或學習指導,找出哪家院校有提供你所選擇的學科。

    ●向自己的教授們請教有關你所選擇學科的課程。他們或可介紹你找到其他教授或所需資料。

    ●查看有列明院校各部門、師資及教師研究專長的參考書籍。找些與你有近似研究興趣的教授。

    ●參考有關自己學科的學術刊物,了解誰活躍于該學系、編輯部門的人、誰撰寫的文章最能吸引你,找出這些教授及研究專員所屬的院校。

    ●找出哪所大學有提供你心目中學系的研究院。

    切記如果你的研究喜好與該校的教師類似的話,你獲錄取和得到獎學金的機會便會相應提高。

    課程及論文要求

    ●找出完成該學科所需時間多久。

    ●查看學校章程內的課程是否合乎你的要求和興趣。

    ●找出完成學位的要求,是否包含考試、口試或論文。

    posted @ 2007-12-05 23:13 hk2000c 閱讀(548) | 評論 (0)編輯 收藏

      在過去幾年里,Hibernate不斷發展,幾乎成為Java數據庫持久性的事實標準。它非常強大、靈活,而且具備了優異的性能。在本文中,我們將了解如何使用Java 5 注釋來簡化Hibernate代碼,并使持久層的編碼過程變得更為輕松。

      傳統上,Hibernate的配置依賴于外部 XML 文件:數據庫映射被定義為一組 XML 映射文件,并且在啟動時進行加載。創建這些映射有很多方法,可以從已有數據庫模式或Java類模型中自動創建,也可以手工創建。無論如何,您最終將獲得大量的 Hibernate 映射文件。此外,還可以使用工具,通過javadoc樣式的注釋生成映射文件,盡管這樣會給您的構建過程增加一個步驟。

      在最近發布的幾個Hibernate版本中,出現了一種基于 Java 5 注釋的更為巧妙的新方法。借助新的 Hibernate Annotation 庫,即可一次性地分配所有舊映射文件——一切都會按照您的想法來定義——注釋直接嵌入到您的 Java 類中,并提供一種強大及靈活的方法來聲明持久性映射。籍由自動代碼完成和語法突出顯示功能,最近發布的Java IDE也為其提供了有力的支持。

      Hibernate Annotation還支持新的 EJB 3 持久性規范。這些規范旨在提供一種標準化的 Java 持久性機制。由于 Hibernate 3 還提供了一些擴展,因此您可以十分輕松地遵從這些標準,并使用 EJB 3 編程模型來對 Hibernate 持久層進行編碼。

      現在,讓我們來動手使用Hibernate Annotation。

    安裝 Hibernate Annotation

      要使用 Hibernate Annotation,您至少需要具備 Hibernate 3.2和Java 5。可以從 Hibernate 站點 下載 Hibernate 3.2 和 Hibernate Annotation庫。除了標準的 Hibernate JAR 和依賴項之外,您還需要 Hibernate Annotations .jar 文件(hibernate-annotations.jar)、Java 持久性 API (lib/ejb3-persistence.jar)。如果您正在使用 Maven,只需要向 POM 文件添加相應的依賴項即可,如下所示:

        ...
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>3.2.1.ga</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.2.0.ga</version>
    </dependency>
    <dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0</version>
    </dependency>
    ...
    

      下一步就是獲取 Hibernate 會話工廠。盡管無需驚天的修改,但這一工作與使用 Hibernate Annotations有所不同。您需要使用 AnnotationConfiguration 類來建立會話工廠:

    sessionFactory = new
    AnnotationConfiguration().buildSessionFactory();

      盡管通常使用 <mapping> 元素來聲明持久性類,您還是需要在 Hibernate 配置文件(通常是 hibernate.cfg.xml)中聲明持久性類:

    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    <session-factory>
    <mapping class="com.onjava.modelplanes.domain.PlaneType"/>
    <mapping class="com.onjava.modelplanes.domain.ModelPlane"/>
    </session-factory>
    </hibernate-configuration>
    

      近期的許多 Java 項目都使用了輕量級的應用框架,例如 Spring。如果您正在使用 Spring 框架,可以使用 AnnotationSessionFactoryBean 類輕松建立一個基于注釋的 Hibernate 會話工廠,如下所示:

    <!-- Hibernate session factory -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">create</prop>
    ...
    </props>
    </property>
    <property name="annotatedClasses">
    <list>
    <value>com.onjava.modelplanes.domain.PlaneType</value>
    <value>com.onjava.modelplanes.domain.ModelPlane</value>
    ...
    </list>
    </property>
    </bean>
    

    第一個持久性類

      既然已經知道了如何獲得注釋所支持的 Hibernate 會話,下面讓我們來了解一下帶注釋的持久性類的情況:

      像在其他任何 Hibernate應用程序中一樣,帶注釋的持久性類也是普通 POJO。差不多可以說是。您需要向 Java 持久性 API (javax.persistence.*)添加依賴項,如果您正在使用任何特定于 Hibernate的擴展,那很可能就是 Hibernate Annotation 程序包(org.hibernate.annotations.*),但除此之外,它們只是具備了持久性注釋的普通 POJO 。下面是一個簡單的例子:

    @Entity
    public class ModelPlane {
    private Long id;
    private String name;
    @Id
    public Long getId() {
    return id;
    }
    public void setId(Long id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }
    

      正像我們所提到的,這非常簡單。@Entity 注釋聲明該類為持久類。@Id 注釋可以表明哪種屬性是該類中的獨特標識符。事實上,您既可以保持字段(注釋成員變量),也可以保持屬性(注釋getter方法)的持久性。后文中將使用基于屬性的注釋。基于注釋的持久性的優點之一在于大量使用了默認值(最大的優點就是 “慣例優先原則(convention over configuration)”)。例如,您無需說明每個屬性的持久性——任何屬性都被假定為持久的,除非您使用 @Transient 注釋來說明其他情況。這簡化了代碼,相對使用老的 XML 映射文件而言也大幅地減少了輸入工作量。

    生成主鍵

      Hibernate 能夠出色地自動生成主鍵。Hibernate/EBJ 3 注釋也可以為主鍵的自動生成提供豐富的支持,允許實現各種策略。下面的示例說明了一種常用的方法,其中 Hibernate 將會根據底層數據庫來確定一種恰當的鍵生成策略:

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
    return id;
    }
    

    定制表和字段映射

      默認情況下,Hibernate 會將持久類以匹配的名稱映射到表和字段中。例如,前一個類可以與映射到以如下代碼創建的表中:

    CREATE TABLE MODELPLANE
    (
    ID long,
    NAME varchar
    )
    

      如果您是自己生成并維護數據庫,那么這種方法很有效,通過省略代碼可以大大簡化代碼維護。然而,這并不能滿足所有人的需求。有些應用程序需要訪問外部數據庫,而另一些可能需要遵從公司的數據庫命名慣例。如果有必要,您可以使用 @Table 和 @Column 注釋來定制您自己的持久性映射,如下所示:

    @Entity
    @Table(name="T_MODEL_PLANE")
    public class ModelPlane {
    private Long id;
    private String name;
    @Id
    @Column(name="PLANE_ID")
    public Long getId() {
    return id;
    }
    public void setId(Long id) {
    this.id = id;
    }
    @Column(name="PLANE_NAME")
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }
    

      該內容將映射到下表中:

    CREATE TABLE T_MODEL_PLANE
    (
    PLANE_ID long,
    PLANE_NAME varchar
    )
    

      也可以使用其他圖和列的屬性來定制映射。這使您可以指定諸如列長度、非空約束等詳細內容。Hibernate支持大量針對這些注釋的屬性。下例中就包含了幾種屬性:

        ...
    @Column(name="PLANE_ID", length=80, nullable=true)
    public String getName() {
    return name;
    }
    ...
    

    映射關系

      Java 持久性映射過程中最重要和最復雜的一環就是確定如何映射表間的關系。像其他產品一樣, Hibernate 在該領域中提供了高度的靈活性,但卻是以復雜度的增加為代價。我們將通過研究幾個常見案例來了解如何使用注釋來處理這一問題。

      其中一種最常用的關系就是多對一的關系。假定在以上示例中每個 ModelPlane 通過多對一的關系(也就是說,每個飛機模型只與一種飛機類型建立聯系,盡管指定的飛機類型可以與七種飛機模型建立聯系)來與 PlaneType 建立聯系。可如下進行映射:

        @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    public PlaneType getPlaneType() {
    return planeType;
    }
    

      CascadeType 值表明 Hibernate 應如何處理級聯操作。

      另一種常用的關系與上述關系相反:一對多再對一關系,也稱為集合。在老式的 Hibernate 版本中進行映射或使用注釋時,集合令人頭疼,這里我們將簡要加以探討,以使您了解如何處理集合,例如,在以上示例中每個 PlaneType 對象都可能會包含一個 ModelPlanes 集合。可映射如下:

     @OneToMany(mappedBy="planeType",
    cascade=CascadeType.ALL,
    fetch=FetchType.EAGER)
    @OrderBy("name")
    public List<ModelPlane> getModelPlanes() {
    return modelPlanes;
    }
    

    命名查詢

      Hibernate 最優秀的功能之一就在于它能夠在您的映射文件中聲明命名查詢。隨后即可通過代碼中的名稱調用此類查詢,這使您可以專注于查詢,而避免了 SQL 或者 HQL 代碼分散于整個應用程序中的情況。

      也可以使用注釋來實現命名查詢,可以使用 @NamedQueries 和 @NamedQuery 注釋,如下所示:

    @NamedQueries(
    {
    @NamedQuery(
    name="planeType.findById",
    query="select p from PlaneType p left join fetch p.modelPlanes where id=:id"
    ),
    @NamedQuery(
    name="planeType.findAll",
    query="select p from PlaneType p"
    ),
    @NamedQuery(
    name="planeType.delete",
    query="delete from PlaneType where id=:id"
    )
    }
    )
    

      一旦完成了定義,您就可以像調用其他任何其他命名查詢一樣來調用它們。

    結束語

      Hibernate 3 注釋提供了強大而精致的 API,簡化了 Java 數據庫中的持久性代碼,本文中只進行了簡單的討論。您可以選擇遵從標準并使用 Java 持久性 API,也可以利用特定于 Hibernate的擴展,這些功能以損失可移植性為代價提供了更為強大的功能和更高的靈活性。無論如何,通過消除對 XML 映射文件的需求,Hibernate 注釋將簡化應用程序的維護,同時也可以使您對EJB 3 有初步認識。來試試吧!

    posted @ 2007-12-02 01:13 hk2000c 閱讀(295) | 評論 (0)編輯 收藏

    通常是封裝在某一級的接口或者類沒有事務聲明所致

      <aop:config>
            <aop:pointcut id="systemDaoMethods" expression="execution(* com.unitedbiz.system.dao.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="systemDaoMethods"/>
        </aop:config>

    申明一下就可以了

    posted @ 2007-11-30 14:01 hk2000c 閱讀(282) | 評論 (0)編輯 收藏

    需要改裝的是
    圖片,文件上傳
    服務器圖片瀏覽
    這兩樣工作應該需要3天左右


    posted @ 2007-11-30 00:50 hk2000c 閱讀(251) | 評論 (0)編輯 收藏

    How to configure remote JSP debugging
    under WebSphere Application Servers

    Introduction

    This article describes how you can create a run and debug configuration in IntelliJ IDEA 5.1 (or higher) to allow your Web modules with JSP files to be remotely debugged under WebSphere 5.1 and WebSphere 6.0 application servers. It also describes how to configure these application servers to enable integration with IntelliJ IDEA, and provides you with step-by-step instructions for application deployment.

    Requirements

    Before making any settings, make sure that the following requirements are met:

    1. WebSphere 5.1 or 6.0 application server is installed (full or express version). IntelliJ IDEA does not have any bundled servers, so you need to install them yourself.
    2. JDK (Sun Java2TM Standard Edition SDK) 1.4 or less is installed. Although IntelliJ IDEA supports JDK 5.0, WebSphere application servers require JDK 1.4 or less.
    3. WebSphere application server is started.

    You can find the full documentation on how to install, start, and initially configure the server at http://www-306.ibm.com/software/websphere/.

    Configuring WebSphere Application Server

    To enable remote debugging on the server, perform the following steps:

    1. Start the server's Administrative Console.
    2. Expand Servers > Application Servers.
      screenshot


      Note:

      WebSphere Application Server V6.0: Administrative Console

    3. Click the server name and clic the Configuration tab
    4. Navigate to the Additional Properties group and click the Debugging Service link.
    5. Make sure that the debugging service is enabled at server startup.
      • For the WebSphere 5.1 application server, select the Startup check box.
        screenshot


        Note:

        WebSphere Application Server - Express 5.1: enabling debugging

      • For the WebSphere 6.0 application server, select the Enable service at server startup check box.

        To the JVM Debug arguments field add the following line before the existing set of arguments: -Dwas.debug.mode=true

        screenshot


        Note:

        WebSphere Application Server 6.0: enabling debugging

    6. Click the Apply button. Then save the settings (for example, using the notification message)
      screenshot


      Note:

      WebSphere Application Server 6.0: notification message

    7. Restart the server using WebSphere's or operating system's administration tools.

    Generating the Application Archive File

    IntelliJ IDEA can generate the WAR, EAR, or JAR file for your application automatically. For this purpose, you need to configure your module (Web, J2EE Application, or EJB).

    1. In IntelliJ IDEA, click File > Settings (Ctrl + Alt + S), and then select Modules.
    2. In the Modules list, click the module for which you need to generate the archive file.
    3. Click the J2EE Build Settings tab.
    4. Select the Create web module war/ear/jar file check box, and specify the location of the file. This path will be required later, on the application deployment stage.

    Apply the changes, and make the project (click the Build > Make Project menu or press Ctrl + F9).

    The application is now ready for deployment.

    Deploying Application

    You deploy the application using the WebSphere application server's tools.

    1. Start the server's Administrative Console.
    2. Click Applications > Install New Application, and then click your server.
    3. Select Local file system or Remote file system depending on your server installation, and then specify the path to your WAR, EAR, or JAR file (usually, it corresponds to the path you specified when generating the application archive file in IntelliJ IDEA).
    4. In the Context Root text box, specify the context (e.g. "myContext").
      screenshot


      Note:

      WebSphere Application Server 6.0: enabling debugging

    5. Click Next. Skip the next screen and click Next again. Note:

      The Security Warning may appear. It displays the content of the was.policy file. You can simply click Continue.

    6. On the first step of the Install New Application wizard, select the Enable class reloading check box, and then click Next. This allows you to redeploy the application without reinstalling it
      screenshot


      Note:

      WebSphere Application Server 6.0: selecting installation options.

    7. On the second step, select the check box next to your Web module to map it to the server. Then click Next.
    8. On the third step, select the check box next to your Web module to map the virtual host to it, and then click Next.
    9. On the last step, just click Finish. The deployment will start. Note:

      The server may not always notify you when the deployment is complete. If the operation takes too long, click Enterprise Applications. If your application is present in the enterprise applications list with the ?Stopped? status (red cross icon), the deployment is complete, and you can move to the next step.

    10. When the deployment is finished, the server console will show you the installation results page. Save the configuration, for example using the Save to Master Configuration link on this page.
    11. Click Applications > Enterprise Applications. In the list of applications your application should be displayed with the ?Stopped? status (red cross icon).
    12. Select the check box next to your application, and then click Start.
    screenshot


    Note:

    WebSphere Application Server 6.0: starting the application

    Creating Run/Debug Configuration

    WebSphere 5.1/6.0 application servers are supported by means of the general IntelliJ IDEA integration with the JSR compatible servers and do not have a server-specific type of Run/Debug configuration. You need to create a configuration for a JSR45 compatible server and then modify it according to WebSphere application server's needs.

    1. In IntelliJ IDEA, click the Run > Edit Configurations menu, and then click the JSR45 Compatible Server tab.
    2. Click the plus sign or press Insert to add a new configuration, and then click Remote. Specify some configuration name.
    3. Click the Configure button to add an application server.
    4. In the Application Servers dialog box, click Add, and then specify some server name.
    5. Click the Attach Classes button, and find the j2ee.jar file for your version of WebSphere Application server (<installation folder>\lib\j2ee.jar).
      screenshot


      Note:

      IntelliJ IDEA 5.1: configuring application servers

    6. In the Application Servers dialog box, click Apply to save the changes, and then in the configuration, in the Application Server list, select the added server.
    7. In the Startup page text box, specify the home page of your web application in the following format:
      • WebSphere Application Server 5.1
        http://localhost:7080/<ContextRoot>/<home page>
      • WebSphere Application Server 6.0
        http://localhost:9080/<ContextRoot>/<home page>

      "7080" and "9080" are default ports for the WebSphere 5.1 and 6.0 servers, respectively. If you have changed the default port, specify the actual port number instead.

      <ContextRoot> is name of web context used during application deployment, e.g. "myContext".
      <home page> can be any page from your Web module.

    8. Optionally, you can configure the application server log to display its content in the IntelliJ IDEA's Run tool window, in the console.

      Click the Add button, and then in the Edit Log Files Aliases dialog specify some alias name (e.g. "activity"). In the Log File Location text box, specify the location of the activity.log file. By default, this file is located in the <home>\logs folder.

    9. In the JSP package text box, enter one of the following:
      • WebSphere Application Server 5.1
        org.apache.jsp
      • WebSphere Application Server 6.0
        com.ibm._jsp
    10. In the VM options variable text box, type DEBUG.
    11. If you use WebSphere Application Server 5.1, select to use the specific JSP's line mapping model. For the WebSphere Application Server 6.0 this check box is ignored, but it is recommended to clear it.
    12. In the Port text box, specify the port number of the corresponding application server (it should be the same as specified for the startup page).
      screenshot


      Note:

      IntelliJ IDEA 5.0: Run/Debug configuration for WebSphere Application Server 6.0

    13. Click the Startup/Connection tab, and then click Debug. Check that the Socket option is selected, and the Port number is 7777.

    Debugging Application

    To start debugging the application in IntelliJ IDEA, just click Run > Debug or press Shift + F9. You can use all the power of the IntelliJ IDEA debugger for the JSP files running on the WebSphere Application Server, including breakpoints, stepping commands, watches, etc.

    Note:

    To apply any changes in the web module, you will need to redeploy the application as described earlier in this article. The "Build on frame deactivation" feature is not supported for remote debugging.

    Troubleshooting

    The server crashes during the debug session

    Such problems may appear if you use the IBM's virtual machine supplied with the WebSphere server. Possible solutions in this case are:

    • Run IntelliJ IDEA with the command line option:
      -Didea.debugger.keep.temp.objects=false
      In this case IntelliJ IDEA will avoid calling certain methods from debug API.
    • Use Sun's JDK 1.4 or less

    IntelliJ IDEA is unable to open the debugger port

    In most cases, it is an internal WebSphere server error. Try to restart the WebSphere application server.

    Conclusion

    We hope that this article helped you to configure remote debugging successfully. If you have encountered any problems that are not described in the Troubleshooting section, don't hesitate to contact our technical support: support@jetbrains.com.

    posted @ 2007-11-20 21:56 hk2000c 閱讀(947) | 評論 (0)編輯 收藏

    Enter your name: Daniel
    Your key is: 22457-MWPIW-INDLX-2PU2X-WBHSX-C4YK0

    posted @ 2007-11-20 19:07 hk2000c 閱讀(240) | 評論 (0)編輯 收藏

    Hibernate 1.2.3 has built-in support for blobs. Hibernate natively maps blob columns to java.sql.Blob. However, it's sometimes useful to read the whole blob into memory and deal with it as a byte array.

    One approach for doing this to create a new UserType as follows.

    package mypackage;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
    import java.sql.Blob;
    import cirrus.hibernate.Hibernate;
    import cirrus.hibernate.HibernateException;
    import cirrus.hibernate.UserType;
    public class BinaryBlobType implements UserType
    {
    public int[] sqlTypes()
    {
    return new int[] { Types.BLOB };
    }
    public Class returnedClass()
    {
    return byte[].class;
    }
    public boolean equals(Object x, Object y)
    {
    return (x == y)
    || (x != null
    && y != null
    && java.util.Arrays.equals((byte[]) x, (byte[]) y));
    }
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
    throws HibernateException, SQLException
    {
    Blob blob = rs.getBlob(names[0]);
    return blob.getBytes(1, (int) blob.length());
    }
    public void nullSafeSet(PreparedStatement st, Object value, int index)
    throws HibernateException, SQLException
    {
    st.setBlob(index, Hibernate.createBlob((byte[]) value));
    }
    public Object deepCopy(Object value)
    {
    if (value == null) return null;
    byte[] bytes = (byte[]) value;
    byte[] result = new byte[bytes.length];
    System.arraycopy(bytes, 0, result, 0, bytes.length);
    return result;
    }
    public boolean isMutable()
    {
    return true;
    }
    }
    

    The BinaryBlobType will convert a blob into a byte array and back again.

    Here's how to use it. First, define an entity that contains a byte[] property:

    public class ImageValue
    {
    private long id;
    private image byte[];
    public long getId() { return id; }
    public void setId(long id) { this.id = id; }
    public byte[] getImage() { return image; }
    public void setImage(byte[] image) { this.image = image; }
    }
    

    Then map a blob column onto the byte[] property:

    <class name="ImageValue" table="IMAGE_VALUE">
    <id name="id/>
    <property name="image" column="IMAGE" type="mypackage.BinaryBlobType"/>
    </class>
    

    Notes:

    1) Blobs aren't cachable. By converting the blob into a byte array, you can now cache the entity.

    2) This approach reads the whole blob into memory at once.

    3) The above type is known to work for reading blobs out of the db. Other usage patterns might also work.

    Comments (GK)

    I changed isMutable() to return true, since an array is a mutable object.

    The use of setBlob() will work on some drivers, but not all. I think its more portable to use setBytes() or even setBinaryStream().

    comments (db)

    db's comment above was right, setBlob() didn't work on Oracle, I used setBytes().

    comments (Chad Woolley)

    Below is a modified nullsafeset() that i needed to use to get it to work with tomcat 4.1.27 & oracle 8/9i - the normal calls don't work through the tomcat/dbcp connection pool wrapper objects... (this caused me great pain)

    pls note that the setBytes() doesn't seem to work with oracle driver & hibernate

    d.birch@eclipsegroup.com.au

    public void nullSafeSet(PreparedStatement st, Object value, int index)
    throws HibernateException, SQLException
    {
    if(st instanceof org.apache.commons.dbcp.DelegatingPreparedStatement &&
    ((org.apache.commons.dbcp.DelegatingPreparedStatement)st).getDelegate()
    instanceof oracle.jdbc.OraclePreparedStatement)
    {
    oracle.sql.BLOB blob = oracle.sql.BLOB.createTemporary(
    ((org.apache.commons.dbcp.PoolableConnection)st.getConnection()).getDelegate(), false, oracle.sql.BLOB.DURATION_SESSION);
    blob.open(BLOB.MODE_READWRITE);
    OutputStream out = blob.getBinaryOutputStream();
    try
    {
    out.write((byte[])value);
    out.flush();
    out.close();
    }
    catch(IOException e)
    {
    throw new SQLException("failed write to blob" + e.getMessage());
    }
    blob.close();
    ((oracle.jdbc.OraclePreparedStatement)((org.apache.commons.dbcp.DelegatingPreparedStatement)st).getDelegate()).setBLOB(index, blob);
    }
    else
    {
    st.setBlob(index, Hibernate.createBlob((byte[]) value));
    }
    }
    //and.. note the null check, oracle drivers return a null blob...
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
    throws HibernateException, SQLException
    {
    final Blob blob = rs.getBlob(names[0]);
    return blob != null?blob.getBytes(1, (int)blob.length()):null;
    }
    

    / comments Vanitha

    I had to use the user type to save pdfs as oraBLOBs in oracle 91 database. nullsafeSet

    needed a sligh modification , or else ther was a classcastexception. Used oracle Blob instead of Hibernate Blob type and it works.

     public void nullSafeSet(PreparedStatement st, Object value, int index)
    throws HibernateException, SQLException
    {
    oracle.sql.BLOB t_blob = oracle.sql.BLOB.createTemporary(((org.jboss.resource.adapter.jdbc.WrappedConnection) st.getConnection()).getUnderlyingConnection(),
    false, oracle.sql.BLOB.DURATION_SESSION);
    OutputStream t_out = null;
    t_blob.open(BLOB.MODE_READWRITE);
    t_out = t_blob.getBinaryOutputStream();
    try
    {
    t_out.write((byte[]) value);
    t_out.flush();
    t_out.close();
    }
    catch (IOException e)
    {
    throw new SQLException("failed write to blob" + e.getMessage());
    }
    t_blob.close();
    st.setBlob(index, t_blob);
    }
    

    </code>

    posted @ 2007-11-16 17:46 hk2000c 閱讀(1398) | 評論 (0)編輯 收藏

    僅列出標題
    共11頁: 上一頁 1 2 3 4 5 6 7 8 9 下一頁 Last 
    主站蜘蛛池模板: 亚洲伊人久久成综合人影院| 天天摸天天操免费播放小视频| 亚洲成人影院在线观看| 亚洲国产欧美国产综合一区 | 亚洲毛片基地日韩毛片基地| 日韩精品无码免费专区午夜| 中文字幕在线观看亚洲| 亚洲av永久无码精品天堂久久| 最近中文字幕电影大全免费版| 亚洲嫩模在线观看| 免费观看国产网址你懂的| 激情五月亚洲色图| 性做久久久久免费观看| 一级午夜免费视频| 久久亚洲AV午夜福利精品一区 | 大学生高清一级毛片免费| 亚洲精品无码久久| 亚洲国产a级视频| 成av免费大片黄在线观看| 亚洲AV无码国产丝袜在线观看 | 丁香六月婷婷精品免费观看| 国产亚洲精品a在线观看| 嫩草成人永久免费观看| 亚洲一区二区三区久久| 免费吃奶摸下激烈视频| 国产在线观看免费视频软件| 亚洲无圣光一区二区| 国产成人免费网站在线观看| 三年片免费高清版| 亚洲国产片在线观看| 在线视频免费国产成人| 国产免费一区二区三区在线观看 | 国产亚洲人成网站在线观看| 久久午夜伦鲁片免费无码| 亚洲愉拍一区二区三区| 中文字幕专区在线亚洲| 最近免费中文字幕mv电影| 女bbbbxxxx另类亚洲| 亚洲国产一区在线| 免费在线观看中文字幕| 3d动漫精品啪啪一区二区免费 |