Two useful liks:
          http://jibbering.com/faq/faq_notes/closures.html#clMem
          http://javascript.weblogsinc.com/2005/03/07/javascript-memory-leaks/

          文章來源:http://blueoxygen.dflying.net/3/archive/69_no_more_crap_about_ie_memeory_leak.html

          posted @ 2006-03-16 11:19 BlueO2 閱讀(289) | 評論 (0)編輯 收藏

          Venkman is a Javascript Debugger as a FireFox extenstion.It's at least powerful than IE's default script debugger(not Visual InterDev's).You can watch varaiable,set breakpoint and use "step over" "step into" "step out" "continue" buttons to debug your niffy javascript codes.
          It's ease to use.And tutorial is HERE:http://www.svendtofte.com/code/learning_venkman/index.php

          文章來源:http://blueoxygen.dflying.net/3/archive/75_ajax_tool_box---venkman.html

          posted @ 2006-03-16 11:19 BlueO2 閱讀(391) | 評論 (0)編輯 收藏

          eclipsePOJO used by Hibernate needs to implement hashCode() and equals() method.That's a kind of stuffy work and will be done many many times during development.Some IDEs support automatical generation feature such as IDEA.Eclipse famous plug-in--MyElipse also suppots but it's not free of charge.
          I think nearly all JAVAers know Apache Commons open source project.We can use Commons lib to generate hashCode() and equals() method.I wanna tell you that there is also a plugin for Eclipse called Commons4E which help you generate hasCode() and equals().
          It also can generate toString() and compareTo() method.That's a smart plugin.Enjoy it.Link



          文章來源:http://blueoxygen.dflying.net/3/archive/79_hibernate_pojos_good_assistant-commons_for_eclipse.html

          posted @ 2006-03-16 11:19 BlueO2 閱讀(436) | 評論 (0)編輯 收藏

          Hibernate and Lazy Initialization

          Hibernate object relational mapping offers both lazy and non-lazy modes of object initialization. Non-lazy initialization retrieves an object and all of its related objects at load time. This can result in hundreds if not thousands of select statements when retrieving one entity. The problem is compounded when bi-directional relationships are used, often causing entire databases to be loaded during the initial request. Of course one could tediously examine each object relationship and manually remove those most costly, but in the end, we may be losing the ease of use benefit sought in using the ORM tool.

          The obvious solution is to employ the lazy loading mechanism provided by hibernate. This initialization strategy only loads an object's one-to-many and many-to-many relationships when these fields are accessed. The scenario is practically transparent to the developer and a minimum amount of database requests are made, resulting in major performance gains. One drawback to this technique is that lazy loading requires the Hibernate session to remain open while the data object is in use. This causes a major problem when trying to abstract the persistence layer via the Data Access Object pattern. In order to fully abstract the persistence mechanism, all database logic, including opening and closing sessions, must not be performed in the application layer. Most often, this logic is concealed behind the DAO implementation classes which implement interface stubs. The quick and dirty solution is to forget the DAO pattern and include database connection logic in the application layer. This works for small applications but in large systems this can prove to be a major design flaw, hindering application extensibility.


          Being Lazy in the Web Layer

          Fortunately for us, the Spring Framework has developed an out of box web solution for using the DAO pattern in combination with Hibernate lazy loading. For anyone not familiar with using the Spring Framework in combination with Hibernate, I will not go into the details here, but I encourage you to read Hibernate Data Access with the Spring Framework. In the case of a web application, Spring comes with both the OpenSessionInViewFilter and the OpenSessionInViewInterceptor. One can use either one interchangeably as both serve the same function. The only difference between the two is the interceptor runs within the Spring container and is configured within the web application context while the Filter runs in front of Spring and is configured within the web.xml. Regardless of which one is used, they both open the hibernate session during the request binding this session to the current thread. Once bound to the thread, the open hibernate session can transparently be used within the DAO implementation classes. The session will remain open for the view allowing lazy access the database value objects. Once the view logic is complete, the hibernate session is closed either in the Filter doFilter method or the Interceptor postHandle method. Below is an example of the configuration of each component:

          Interceptor Configuration

          <beans> 
            <bean id="urlMapping"     
               class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">    
                 <property name="interceptors">
                   <list>
                        <ref bean="openSessionInViewInterceptor"/>
                   </list>
                 </property>
                 <property name="mappings">
            ...
            </bean>
            ...
            <bean name="openSessionInViewInterceptor"  
              class="org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor">
                 <property name="sessionFactory"><ref bean="sessionFactory"/></property>
            </bean>
          </beans>
           

          Filter Configuration

          <web-app>
           ...      
            <filter>
              <filter-name>hibernateFilter</filter-name>
              <filter-class>
                org.springframework.orm.hibernate.support.OpenSessionInViewFilter
              </filter-class>
             </filter>
            ...      
            <filter-mapping>
              <filter-name>hibernateFilter</filter-name>
               <url-pattern>*.spring</url-pattern>
            </filter-mapping>
            ...
          </web-app>

          Implementing the Hibernate DAO's to use the open session is simple. In fact, if you are already using the Spring Framework to implement your Hibernate DAO's, most likely you will not have to change a thing. The DAO's must access Hibernate through the convenient HibernateTemplate utility, which makes database access a piece of cake. Below is an example DAO.

          Example DAO

          public class HibernateProductDAO extends HibernateDaoSupport implements ProductDAO  {      
           
                 public Product getProduct(Integer productId) {
                        return (Product)getHibernateTemplate().load(Product.class, productId);
                 }
           
                 public Integer saveProduct(Product product) {
                        return (Integer) getHibernateTemplate().save(product);
                 }       
           
                 public void updateProduct(Product product) {
                        getHibernateTemplate().update(product);
                 }
           }

          Being Lazy in the Business Layer

          Even outside the view, the Spring Framework makes it easy to use lazy load initialization, through the AOP interceptor HibernateInterceptor. The hibernate interceptor transparently intercepts calls to any business object configured in the Spring application context, opening a hibernate session before the call, and closing the session afterward. Let's run through a quick example. Suppose we have an interface BusinessObject:

          public interface BusinessObject { 
               public void doSomethingThatInvolvesDaos(); 
          }</pre><p><font size="2">The class BusinessObjectImpl implements BusinessObject:</font></p>
          <p />
          <pre>public class BusinessObjectImpl implements BusinessObject {
              public void doSomethingThatInvolvesDaos() {
                  // lots of logic that calls
                  // DAO classes Which access 
                  // data objects lazily
              }
          }

          Through some configurations in the Spring application context, we can instruct the HibernateInterceptor to intercept calls to the BusinessObjectImpl allowing it's methods to lazily access data objects. Take a look at the fragment below:

          <beans>
              <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
                   <property name="sessionFactory">
                     <ref bean="sessionFactory"/>
                   </property>
              </bean>
              <bean id="businessObjectTarget" class="com.acompany.BusinessObjectImpl">
                 <property name="someDAO"><ref bean="someDAO"/></property>
              </bean>
              <bean id="businessObject" class="org.springframework.aop.framework.ProxyFactoryBean">
                   <property name="target"><ref bean="businessObjectTarget"/></property>
                   <property name="proxyInterfaces">
                     <value>com.acompany.BusinessObject</value>
                   </property>
                   <property name="interceptorNames">
                     <list>
                        <value>hibernateInterceptor</value>
                     </list>
                   </property>
               </bean>            
          </beans>
           
           

          When the businessObject bean is referenced, the HibernateInterceptor opens a hibernate session and passes the call onto the BusinessObjectImpl. When the BusinessObjectImpl has finished executing, the HibernateInterceptor transparently closes the session. The application code has no knowledge of any persistence logic, yet it is still able to lazily access data objects.

          Being Lazy in your Unit Tests

          Last but not least, we'll need the ability to test our lazy application from J-Unit. This is easily done by overriding the setUp and tearDown methods of the TestCase class. I prefer to keep this code in a convenient abstract TestCase class for all of my tests to extend.

          public abstract class MyLazyTestCase extends TestCase {
           
                  private SessionFactory sessionFactory;
                  private Session session;
          	
                  public void setUp() throws Exception {
          	    super.setUp();
          	    SessionFactory sessionFactory = (SessionFactory) getBean(\"sessionFactory&quot<img alt=";)" src="http://www.dflying.net/plugins/smileys/icons/default/wink_smile.gif" />;
          	    session = SessionFactoryUtils.getSession(sessionFactory, true);
          	    Session s = sessionFactory.openSession();
          	    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
           
                  }
           
                  protected Object getBean(String beanName) {
                      //Code to get objects from Spring application context
                  }
          	
                  public void tearDown() throws Exception {
          	    super.tearDown();
          	    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
          	    Session s = holder.getSession(); 
          	    s.flush();
          	    TransactionSynchronizationManager.unbindResource(sessionFactory);
          	    SessionFactoryUtils.closeSessionIfNecessary(s, sessionFactory);
                  }
          }


          文章來源:http://blueoxygen.dflying.net/3/archive/84_hibernate_performance_tuning.html

          posted @ 2006-03-16 11:19 BlueO2 閱讀(657) | 評論 (0)編輯 收藏

          寫了一些sample在我的Blog上面。還沒入門的朋友可以看看
          http://blueoxygen.dflying.net/3/archive/20_prototype_samples.html

          posted @ 2006-03-16 11:18 BlueO2 閱讀(276) | 評論 (0)編輯 收藏

          為AJAX貼貼臉系列文章。第一篇為AJAX貼貼臉之入門篇。本示例部分操作利用了prototype類庫,相關知識請查閱
          示例為我們以前經常遇到過的動態列表問題,當選擇一個下拉框的時候,另外一個的數據將動態產生。
          result.jpg
          這是個極其簡單的應用。不過我們講由簡入繁,最后的示例會展現Google Suggest類型的自動提示。而且我們會不停的對整個示例重構,標題的1-1也表明有1-2等。
          1-1是完全自己處理AJAX的各種問題,1-2預計引入其他類庫來操作XML,1-3預計用buffalo完成此示例。之后每個示例如果有必要都會有此類對比。由于代碼很簡單并且有注釋,所以文章以代碼即可展現應用。

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
          <html>
          <head>
          <title> Dynamic List </title>
          <meta name="Generator" content="EditPlus">
          <meta name="Author" content="">
          <meta name="Keywords" content="">
          <meta name="Description" content="">
          <script src="./script/prototype.js"></script>
          <script>
          var xmlHttp;    //XHR Object
          function refreshBookList() {

             var bookCate = $F('bookCate');
             
          //clear Books select box
             if(bookCate == ""){
              clearBookList();
              
          return;
             }
             
          //we use XML file directly for demonstrating.
             //In real application,you should generate *.xml file by server side
             var url;
             
          if(bookCate == "SAP"){
              url 
          = "./SAP.xml";
             }
          else {
              url 
          = "./SIEBEL.xml"
             }
             
          var pars = "";
             
          //Create a new XHR object
             xmlHttp = new Ajax.Request(url,{method: 'get',onComplete: handleListChange});
          }
          //remove list values of select box
          function clearBookList() {
              
          var books = $('bookList');
              
          while(books.childNodes.length >0){
                  books.removeChild(books.childNodes[
          0]);
              }
          }
          //callback function
          function handleListChange(originalRequest){
              clearBookList();
              
          var books = $('bookList');
              
          var results = originalRequest.responseXML;
              
          var option = null;
              
          var booksXML = results.getElementsByTagName("books")[0];
              
          for(var i = 0; i < booksXML.childNodes.length;i++){
                  
          //get book tag
                  var listItem = booksXML.childNodes[i];
                  option 
          = document.createElement('option');
                  option.appendChild(document.createTextNode(listItem.firstChild.nodeValue));
                  books.appendChild(option);
              }
          }
          </script>
          </head>

          <body>
          <form  action="#">
              Book Categroies:
              
          <select name="bookCate" id="bookCate" onchange="refreshBookList();">
                  
          <option>Select One</option>
                  
          <option value="SAP">SAP Books</option>
                  
          <option value="SIEBLE">SIEBEL Books</option>
              
          </select>
              
          <br/><br/>
              
          <select name="bookList" id="bookList" size="6" style="width:300px"></select>
              
          <br/>
          </form>
          </body>
          </html>
          SAP.xml
          <?xml version="1.0" encoding="ISO-8859-1"?>
          <books>
          <book>
          ABAP
          </book>
          <book>
          BW
          </book>
          <book>
          FI module
          </book>
          </books>
          SIEBEL.xml
          <?xml version="1.0" encoding="ISO-8859-1"?>
          <books>
          <book>
          SIEBEL
          </book>
          <book>
          CRM
          </book>
          </books>

          posted @ 2006-02-23 14:52 BlueO2 閱讀(338) | 評論 (0)編輯 收藏

          AJAX與服務器端通訊,雖然XHR對象的介入使得我們可以異步處理用戶請求,但是另外一個細節也暴露給我,我們如何與服務器統一通信的契約?

          目前比較公認的有三種數據傳輸交互方式:XHTML Fragment,JSON,XML。當然也有不同的聲音,這里也會介紹。

          • XHTML片斷
          這種方式也被稱為AHAH, 目前很多遺留系統想沾AJAX的光改善一下交互,節省一下帶寬,大多數采用了此種方式。我們以 Struts為例子,比如在頁面上有一個按鈕,點擊后得到現在我收藏的圖書列表,則此圖書列表(Table元素組成)便可以以XHTML片斷的形式返回客戶端,然后利用XHR對象的responseText屬性得到,設知道某DIV中。
          for (Iterator it = sortedPresidentsList.iterator(); it.hasNext();) {
          HashMap hm 
          = (HashMap)it.next();
          html 
          += "<tr>";
          html 
          += "<td>" + (String)hm.get("firstName"+ "</td>";
          html 
          += "<td>" + (String)hm.get("middleName"+ "</td>";
          html 
          += "<td>" + (String)hm.get("lastName"+ "</td>";
          html 
          += "<td>" + (String)hm.get("firstYearInOffice"+ "</td>";
          html 
          += "<td>" + (String)hm.get("lastYearInOffice"+ "</td>";
          html 
          += "</tr>";
          }

          html 
          += "</table>";

          // Write the HTML to response
          response.setContentType("text/html");
          PrintWriter out 
          = response.getWriter();
          out.println(html);
          out.flush();

            這是一個Struts的例子,Action中response直接flush輸出。當然也可以讓Struts導向到結果jsp頁面,請求的XHR可以得到jsp生成的HTML內容。
            缺點是:當返回有Form的內容的時候會崩潰。CSS樣式設置復雜。

            • XML

            XML在很多AJAX示例當中作為數據傳遞的標準。而XML具有很好的數據語義描述性,服務器端生成方式眾多,而幾乎所有瀏覽器都支持對XML 的解析。以JavaScript解析服務器端返回的XML為例子,只需要XHR的responseXML屬性即可獲得XML由javascript解析 (請看我在BlogJAVA寫的操作示例) 而XML的產生方式似乎所有人都認準了

            <channel>

            <title>Dan's Data</title>
            <description>New and interesting computer hardware, gadgets and toys reviewed. And letters. Lots of letters.</description>
            <link>http://www.dansdata.com/</link>

            <item>
            <title>Ultrasone HFI-650 headphones</title>
            <description>They look like the 550s, they feel like the 550s, they've got marketing buzzwords like the 550s - are they worth the extra money?</description>
            <link>http://www.dansdata.com/quickshot025.htm</link>
            </item>..


            一類,似乎這樣子語義描述更清楚。但是我從michael那里得到啟示,確實,用此類方式傳輸數據幾乎無法在客戶端統一處理。從我的showcase中大家也可以看到,Javascript解析XML后更新DOM簡直就是體力活,枯燥且容易出錯,而此種XML返回的數據則會千差萬別,以尋找特定tag的節點的方式怎么能統一處理呢?于是,第二種XML傳輸方式應運而生。

            <list>
            <type>java.util.List</type>
            <map>
            <type>yourapp.domain.Book</type>
            <string>title</string>
            <string>JavaScript, the Definitive Guide</string>
            <string>publisher</string>
            <string>O'Reilly</string>
            <string>author</string>
            <string>David Flanagan</string>
            <string>cover</string>
            <string>/images/cover_defguide.jpg</string>
            <string>blurb</string>
            <string>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</string>
            </map>
            <map>
            <type>yourapp.domain.Book</type>
            <string>title</string>
            <string>DOM Scripting</string>
            <string>publisher</string>
            <string>Friends of Ed</string>
            <string>author</string>
            <string>Jeremy Keith</string>
            <string>cover</string>
            <string>/images/cover_domscripting.jpg</string>
            <string>blurb</string>
            <string>Praesent et diam a ligula facilisis venenatis.</string>
            </map>
            </list>

            這樣子只要客戶端和服務器端達成共識string long int等代表的數據類型就可以在兩端還原成自己可用的對象。而這也是micheal認為真正的AJAX應該有的形態,其中AJAX引擎便承擔起了這份工作。
            缺點:XML的構造和解析在沒有類庫輔助的情形下是一個災難,而XML文件的傳輸效率似乎也不如簡單的文本高。我講會在后面的blog中提及XML文件的產生方法,以及客戶端一些解析的框架。

            • JSON

            JSON是JavaScript Object Notation的縮寫,是一份規范,定義了極其簡單的數據描述形式。幾乎在所有主流語言上都有實現。JSON對象是名/值對的集合,所以一個簡單的JSON對象為:
            var testObj={
            "name" : david,
            "sex" : Male,
            "work" : SE
            }
            客戶端javascript調用eval()就可以解析由服務器端產生的JSON串。var jsonExpression =
            ( + req.responseText + );
            var customer = eval(jsonExpression);
            而服務器端(JAVA)也有相應的lib來操作:JSONObject 的toString()
            缺點:雖然JSON簡潔高效,但是轉化為Javascript對象通過eval()也就是說,AJAX的傳輸邦定在了JSON上面,這樣,總會讓人不很放心。并且JSON對象難于閱讀與理解,不如XML直觀。

            除了會在下一篇AJAX的blog介紹JAVA對象序列化到XML以外,會一些從頭開始構建應用的例子,其中不會使用buffalo DWR一類的框架,我會嘗試選用不同的方案作為數據傳輸方式供大家參考。

            posted @ 2006-02-23 11:15 BlueO2 閱讀(502) | 評論 (0)編輯 收藏

             總結了Hibernate中實體對象與數據的映射方式……圖文并茂,配置實例代碼,為david吃飽了撐著之大作。

            1 Entity's relation

            1.1 One to One

            一個對象的一個實例對應另一個對象的一個實例從數據庫角度描述,一個表中的一條記錄對應另外一個表中唯一一條記錄

            public class Image{
            private Long id;
            private String imgName;
            private Blog value; //to store binary of image
            ......
            }
            Image.hbm.xml
            <class name="Image" dynamic-update="true">
            <id...
            <property...
            </class>

            public class Product{
            private Long id;
            private String name;
            private double price;
            private Image photo;
            }
            <class name=...dynamic-update="true" lazy="true"
            <id...
            <property...
            <one-to-one name="photo" class="package.Image" cascade="all" outer-join="auto" constrained="false"/>
            </class>

            1.2 Many to One

            一個對象對應另一個對象的多個實例,從數據庫角度描述,是在一個數據表中的一條記錄對應另外一個數據表中對條記錄.

            public class PersonalCompute{
            private Long computerId;
            private String cpu;
            ......
            private Programmer owner;
            }
            <class name="Programmer" table="Programmer">
            <id...
            <property...
            </class>

            <class name="PersonalCompute" table="PCS">
            <id....
            <property...
            <many-to-one name="owner" column="programmerId" class="Programmer">
            </many-to-one>

            1.3 One to Many

            The same example as Many to One.But we stand at Programmer class's view point.
            public class Programmer{
            ...
            private Set computers = new HashSet();
            }

            <class name="Programmer" table="Programmer">
            <id...
            <property...
            <set name="computers" inverse="true"
            cascade="all">
            <key column="programmerId" />
            <one-to-many class="PersonalComputers">
            </one-to-many>
            </set>

            2 Collection

            2.1 Map

            <class name="Team">
            ...
            <map name="members">
            <key foreign-key="fk">
            <column name="teamNumber">
            </key>
            <index column="teamRole" type="string"/>
            <element column="name" ...
            </map>


            showcase:
            Team team1 = new Team();
            team1.setName(xx);
            team1.getMembers.put("index","content");
            ......
            sess.save...

            2.1.1 many-to-any

            2.1.2 One to many

            單單使用Map做存儲往往無法表達復雜的對象,如果要講對象 map進行映射:
            One Team,multi-members
            Public class Member{
            ...
            private Team team;
            }
            <class name="Team">
            ...
            <map name="members" inverse="false">
            <key column="team" foreign-key="fk"/>
            <index type="string" column="teamRole"/>
            <one-to-many class="Member"/>
            </map>
            <class name="member">
            <id...
            <property...
            <many-to-one name="team" />

            showcase:
            Team team = new Team();
            team.setName("xx");
            Member mem1 = new Member();
            mem1.setXX
            mem1.setXX
            Member mem2 = new Member();
            mem2.setXX
            mem2.setXX
            team.getMembers().put("xx",mem1);
            team.getMembers().put("xx2",mem2);

            2.1.3 many to many

            <class name="Team">
            <id
            <propery
            <map name="members" inverse="false" table="teamHasMembers">
            <key column="team" foreign-key="fk"/>
            <index type="string" column="teamRole"/>
            <many-to-many class="member" outer-join="auto">
            <column name="member"/>
            </many-to-many>
            </map>

            <class name="Member">
            <id
            <property
            <map name="teams" table="memberAtTeams">
            <key....

            teamRole is their common key of Map.
            Team

            id

            name

            2

            team2

            Member

            di

            name

            age

            1

            davy

            23

            memberAtTeams

            member

            team

            teamRole

            1

            2

            coach

            teamHasMembers

            team

            member

            teamRole

            2

            1

            coach

            2.1.4 Composite-index

            Index 可以是一個類。
            public class Position{
            private String role;
            private String scene;
            public Position()....
            getter...
            setter...
            }
            <class name="Team">
            <id...
            <property...
            <map name="members" inverse="false">
            <key column="team" foreign-key="fk"/>
            <composite-index class="Position">
            <key-property..
            <key-property...
            </composite-index>
            <one-to-many class="Member"/>
            </map>
            ...

            <class name="Member">
            <id...
            <property...
            <many-to-one name="team"/>
            </class>

            index-many-to-one

            index-many-to-many

            index-many-to-any

            2.1.5 composite-element

            <import class="Member"/>
            <class name="Team">
            <id...
            <property...
            <map name="members" table="teamMembers">
            <key column="teamId"/>
            <index...
            <composite-element class="Member">
            <parent name="parent"/>
            <property...
            <property...
            </composite-element>
            </map>

            2.2 Set

            <class name="Team">
            <..
            <set name="members" inverse="false">
            <key column="team" foreign-key="fk"/>
            <elemnet column="name" type="string"/>
            </set>

            2.2.1 one-to-many

            <set name="xx" column="">
            <key column="" foreign-key="fk"/>
            <one-to-many class="BB"/>
            </set>

            2.2.2 many-to-many

            <class name="Team">
            <id..
            <property...
            <set name="members" inverse="false" table="teamHasMembers">
            <key column="team" foreign-key="fk">
            <many-to-many class="Member" outer-join="auto">
            <column name="member"/>
            </set>
            </class>

            <class name="Member">
            <id..
            <property...
            <set name="members" inverse="true" table="memberAtTeams">
            <key column="member" foreign-key="fk">
            <many-to-many class="Member" outer-join="auto">
            <column name="team"/>
            </set>
            </class>

            2.2.3 many-to-any

            2.2.4 composite-element

            2.3 List

            <list name="members" inverse="false">
            <key column="team" foreign-key="fk"/>
            <index column="teamMember"/>
            <element column="name" type="string"/>
            </list>

            2.3.1 one-to-many

            2.3.2 many-to-many

            2.3.3 many-to-many

            2.3.4 composite-element

            2.4 Bag

            It can contains JDK's Collection and List interface type's object.

            2.5 idBag

            bag相比多出了collection-id屬性

            <ibdbag name="">
            <collection-id column="mbid" type="long">
            <generator class="hilo"/>
            </collection-id>
            <key..
            <element..
            </idbag>

            2.6 array

            public class Team{
            //...
            private String[] members;
            }

            <array name="members" inverse="false">
            <key column="team" foreign-key="fk"/>
            <index column="teamNumber"/>
            <element column="name" type="string"/>
            </array>

            3 Component

            <class name="Country">
            <id..
            <property...
            <component name="position">
            <property...
            </component>
            Public class Country{
            //..
            private Position position;
            }

            3.1 One-to-one

            3.2 many-to-one

            3.3 dynamic-component

            4 Dynamic class

            <dynamic-class entity-name="Country">
            <id name="id" type="long">
            <generator class="hilo"/>
            </id>
            <property name="name" column="NAME" type="string"/>
            </..

            4.1 One-to-one

            4.2 many-to-one

            5 Mapping type

            5.1 Java type to SQL type

            5.2 3.0 enmu

            5.3 user defined data type

            need implements org.hibernate.usertype.CompositeUserType or org.hibernate.usertype.UserType interface.

            6 Class Hierarchy's mapping

            6.1 Subclasses are saved in on Table

            6.1.1 discriminator

            Sapmle:

            Class AbstractParent{
            String id;
            /*important*/
            String discriminator
            }
            Class ChildA{
            String childA;
            }
            Class ChildB{
            String childB;
            }

            =========DB=========

            colum name

            data type

            memo

            id

            varchar

            childA

            varchar

            childB

            varchar

            class_type

            varchar

            used to identify subclass:A B

            ========mapping config file====
            <class name="AbstractParent" table="PARENT" abstract="true">
            <id...
            <discriminator column="CLASS_TYPE" type="string"/>
            <property......
            <subclass name="ChildA" discriminator-value="A">
            <property name="childA"/>
            </subclass>
            <subclass......

            id generator

            · increment

            · identity

            · sequence

            · hilo

            · seqhilo

            · uuid

            · guid

            · native

            · assigned

            · foreign

            composite id

            <composite-id>
            <key-property name=""/>
            <key-property name=""/>
            </composite-id>

            6.2 Subclasses are saved in separated Table

            不為父類Container建立映射文件 BoxBottle的映射文件也如沒有關系一樣普通的建立 但是對Container取得操作

            List pcs = sess.createQuery("from com.be.david.Container").list();
            if(!cs.isEmpty()){
            ...
            }
            會將BoxContainer數據都取出來 這是Hibernate默認隱式完成的

            6.3 Parent Class and Subclasses are saved in separated Table

            <class name="Container" table="CONTAINER">
            <id...
            <property...
            <joined-subclass name="Box" table="CONTAINER_BOX">
            <key column="ContainerId"/>
            <property...
            </joined-subclass>
            <joined-subclass name="Bottle" table="CONTAINER_BOTTLE">
            <key...
            <property...
            </joined-subclass>
            </class>

            6.4 One PO multi-table

            public Class Person{
            牋牋燬tring id;
            牋牋燬tring name;
            牋牋燬tring sex;
            牋牋燬tring address;
            牋牋燬tring city;
            牋牋燬tring zipcode;
            }
            we wanna save address related attributes(blue ones).Use join
            <class name="Person" table="PERSON">
            <id...
            <property ...
            <join table="ADDRESS">
            <key column="addressID"/>
            <property name="address"/>
            ...
            </join>
            </class>

            6.5 Class A Class B not inherited from Parent Class

            6.5.1 subselect

            We can use subselct to make ChildA and ChildB standalone.So how can we get all data including ChindA and ChildB?

            <class name="ChildA" table="parent">
            <id...
            <property...
            </class>
            <class name="ChildB" table="parent">
            <id...
            <property...
            </class>
            <class name="Parent" mutable="false">
            <subselect>
            select * from ChildA
            union
            select * from ChildB
            </subselect>
            <sychronize table="ChildA"/>
            <sychronize table="ChildB"/>
            <id...
            <property...
            </class>

            posted @ 2006-02-22 12:45 BlueO2 閱讀(982) | 評論 (0)編輯 收藏

                 摘要: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> New Document </TITLE><META NAME="Ge...  閱讀全文

            posted @ 2006-02-15 22:58 BlueO2 閱讀(936) | 評論 (0)編輯 收藏

            雖然struts上傳下載文件很簡單,但是封裝一下還是好地。
            /*
            * $Id: DownloadAction.java 164530 2005-04-25 03:11:07Z niallp $
            *
            * Copyright 2004-2005 The Apache Software Foundation.
            *
            * Licensed under the Apache License, Version 2.0 (the "License");
            * you may not use this file except in compliance with the License.
            * You may obtain a copy of the License at
            *
            *      http://www.apache.org/licenses/LICENSE-2.0
            *
            * Unless required by applicable law or agreed to in writing, software
            * distributed under the License is distributed on an "AS IS" BASIS,
            * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
            * See the License for the specific language governing permissions and
            * limitations under the License.
            */



            package org.apache.struts.actions;

            import java.io.BufferedInputStream;
            import java.io.File;
            import java.io.FileInputStream;
            import java.io.IOException;
            import java.io.InputStream;
            import java.io.OutputStream;

            import javax.servlet.ServletContext;
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;

            import org.apache.struts.action.Action;
            import org.apache.struts.action.ActionForm;
            import org.apache.struts.action.ActionForward;
            import org.apache.struts.action.ActionMapping;


            /**
            * This is an abstract base class that minimizes the amount of special coding
            * that needs to be written to download a file. All that is required to use
            * this class is to extend it and implement the <code>getStreamInfo()</code>
            * method so that it returns the relevant information for the file (or other
            * stream) to be downloaded. Optionally, the <code>getBufferSize()</code>
            * method may be overridden to customize the size of the buffer used to
            * transfer the file.
            *
            * @since Struts 1.2.6
            */

            public abstract class DownloadAction extends Action {

                /**
                 * If the <code>getBufferSize()</code> method is not overridden, this is
                 * the buffer size that will be used to transfer the data to the servlet
                 * output stream.
                 */

                protected static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

                /**
                 * Returns the information on the file, or other stream, to be downloaded
                 * by this action. This method must be implemented by an extending class.
                 *
                 * @param mapping  The ActionMapping used to select this instance.
                 * @param form     The optional ActionForm bean for this request (if any).
                 * @param request  The HTTP request we are processing.
                 * @param response The HTTP response we are creating.
                 *
                 * @return The information for the file to be downloaded.
                 *
                 * @throws Exception if an exception occurs.
                 */

                protected abstract StreamInfo getStreamInfo(ActionMapping mapping,
                        ActionForm form, HttpServletRequest request,
                        HttpServletResponse response)
                        throws Exception;

                /**
                 * Returns the size of the buffer to be used in transferring the data to
                 * the servlet output stream. This method may be overridden by an extending
                 * class in order to customize the buffer size.
                 *
                 * @return The size of the transfer buffer, in bytes.
                 */

                protected int getBufferSize() {
                    return DEFAULT_BUFFER_SIZE;
                }

                /**
                 * Process the specified HTTP request, and create the corresponding HTTP
                 * response (or forward to another web component that will create it).
                 * Return an <code>ActionForward</code> instance describing where and how
                 * control should be forwarded, or <code>null</code> if the response has
                 * already been completed.
                 *
                 * @param mapping  The ActionMapping used to select this instance.
                 * @param form     The optional ActionForm bean for this request (if any).
                 * @param request  The HTTP request we are processing.
                 * @param response The HTTP response we are creating.
                 *
                 * @throws Exception if an exception occurs.
                 */

                public ActionForward execute(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                        throws Exception {

                    StreamInfo info = getStreamInfo(mapping, form, request, response);
                    String contentType = info.getContentType();
                    InputStream stream = info.getInputStream();

                    try {
                        response.setContentType(contentType);
                        copy(stream, response.getOutputStream());
                    } finally {
                        if (stream != null) {
                            stream.close();
                        }
                    }

                    // Tell Struts that we are done with the response.
                    return null;
                }

                /**
                 * Copy bytes from an <code>InputStream</code> to an
                 * <code>OutputStream</code>.
                 *
                 * @param input  The <code>InputStream</code> to read from.
                 * @param output The <code>OutputStream</code> to write to.
                 *
                 * @return the number of bytes copied
                 *
                 * @throws IOException In case of an I/O problem
                 */

                public int copy(InputStream input, OutputStream output)
                        throws IOException {
                    byte[] buffer = new byte[getBufferSize()];
                    int count = 0;
                    int n = 0;
                    while (-1 != (n = input.read(buffer))) {
                        output.write(buffer, 0, n);
                        count += n;
                    }
                    return count;
                }

                /**
                 * The information on a file, or other stream, to be downloaded by the
                 * <code>DownloadAction</code>.
                 */

                public static interface StreamInfo {

                    /**
                     * Returns the content type of the stream to be downloaded.
                     *
                     * @return The content type of the stream.
                     */

                    public abstract String getContentType();

                    /**
                     * Returns an input stream on the content to be downloaded. This stream
                     * will be closed by the <code>DownloadAction</code>.
                     *
                     * @return The input stream for the content to be downloaded.
                     */

                    public abstract InputStream getInputStream() throws IOException;
                }

                /**
                 * A concrete implementation of the <code>StreamInfo</code> interface which
                 * simplifies the downloading of a file from the disk.
                 */

                public static class FileStreamInfo implements StreamInfo {

                    /**
                     * The content type for this stream.
                     */

                    private String contentType;

                    /**
                     * The file to be downloaded.
                     */

                    private File file;

                    /**
                     * Constructs an instance of this class, based on the supplied
                     * parameters.
                     *
                     * @param contentType The content type of the file.
                     * @param file        The file to be downloaded.
                     */

                    public FileStreamInfo(String contentType, File file) {
                        this.contentType = contentType;
                        this.file = file;
                    }

                    /**
                     * Returns the content type of the stream to be downloaded.
                     *
                     * @return The content type of the stream.
                     */

                    public String getContentType() {
                        return this.contentType;
                    }

                    /**
                     * Returns an input stream on the file to be downloaded. This stream
                     * will be closed by the <code>DownloadAction</code>.
                     *
                     * @return The input stream for the file to be downloaded.
                     */

                    public InputStream getInputStream() throws IOException {
                        FileInputStream fis = new FileInputStream(file);
                        BufferedInputStream bis = new BufferedInputStream(fis);
                        return bis;
                    }
                }

                /**
                 * A concrete implementation of the <code>StreamInfo</code> interface which
                 * simplifies the downloading of a web application resource.
                 */

                public static class ResourceStreamInfo implements StreamInfo {

                    /**
                     * The content type for this stream.
                     */

                    private String contentType;

                    /**
                     * The servlet context for the resource to be downloaded.
                     */

                    private ServletContext context;

                    /**
                     * The path to the resource to be downloaded.
                     */

                    private String path;

                    /**
                     * Constructs an instance of this class, based on the supplied
                     * parameters.
                     *
                     * @param contentType The content type of the file.
                     * @param context     The servlet context for the resource.
                     * @param path        The path to the resource to be downloaded.
                     */

                    public ResourceStreamInfo(String contentType, ServletContext context,
                            String path) {
                        this.contentType = contentType;
                        this.context = context;
                        this.path = path;
                    }

                    /**
                     * Returns the content type of the stream to be downloaded.
                     *
                     * @return The content type of the stream.
                     */

                    public String getContentType() {
                        return this.contentType;
                    }

                    /**
                     * Returns an input stream on the resource to be downloaded. This stream
                     * will be closed by the <code>DownloadAction</code>.
                     *
                     * @return The input stream for the resource to be downloaded.
                     */

                    public InputStream getInputStream() throws IOException {
                        return context.getResourceAsStream(path);
                    }
                }
            }

            posted @ 2005-12-07 17:21 BlueO2 閱讀(720) | 評論 (1)編輯 收藏

            僅列出標題
            共3頁: 上一頁 1 2 3 下一頁 

            posts - 29, comments - 3, trackbacks - 0, articles - 0

            Copyright © BlueO2

            主站蜘蛛池模板: 洪雅县| 阿瓦提县| 大余县| 潍坊市| 遂宁市| 商洛市| 宁晋县| 滦南县| 应城市| 鹤峰县| 湖州市| 包头市| 佛冈县| 上杭县| 阿坝| 长岛县| 安康市| 崇礼县| 宾阳县| 乌拉特前旗| 肃宁县| 长沙县| 富宁县| 龙井市| 南召县| 河源市| 镇坪县| 凤山市| 宽甸| 晋中市| 库伦旗| 松桃| 高碑店市| 奉新县| 普兰店市| 常山县| 大名县| 会泽县| 淅川县| 久治县| 霍山县|