gdufo

           

          (轉)我的APPFUSE2.0.1開發過程的問題記錄和解決辦法

          http://www.pben.cn/read.htm?action=topic&id=8a8181871a519f9c011a54b337e9055d&bid=33&fcpage=1&fcaction=index&tagId=0

          1。怎樣使每次測試時不刪除數據庫中表結構?
          解答:將pom.xml配置中的如下代碼屏蔽掉。
          <!--plugin> 
          <groupId>org.codehaus.mojo</groupId> 
          <artifactId>hibernate3-maven-plugin</artifactId> 
          <version>2.0-alpha-2</version> 
          <configuration> 
          <components> 
          <component> 
          <name>hbm2ddl</name> 
          <implementation> 
          annotationconfiguration 
          </implementation> 
          </component> 
          </components> 
          <componentProperties> 
          <drop>true</drop> 
          <jdk5>true</jdk5> 
          <propertyfile> 
          target/classes/jdbc.properties 
          </propertyfile> 
          <skip>${maven.test.skip}</skip> 
          </componentProperties> 
          </configuration> 
          <executions> 
          <execution> 
          <phase>process-test-resources</phase> 
          <goals> 
          <goal>hbm2ddl</goal> 
          </goals> 
          </execution> 
          </executions> 
          <dependencies> 
          <dependency> 
          <groupId>${jdbc.groupId}</groupId> 
          <artifactId>${jdbc.artifactId}</artifactId> 
          <version>${jdbc.version}</version> 
          </dependency> 
          </dependencies> 
          </plugin
          --> 

          2。怎樣使用外部配置變量?
          解答:a。在pom.xml配置文件中加入占位符
          如: <!-- FTP settings -->
          <ftp.url>135.224.82.4</ftp.url> 
          <ftp.username>ls</ftp.username> 
          <ftp.password>FdbEgT+7</ftp.password> 
          <ftp.path>/data/jyf/0000/SRC/</ftp.path> 

          b。在resources目錄下新建文本文件ftp.properties
          ftp.url=${ftp.url} 
          ftp.username
          =${ftp.username} 
          ftp.password
          =${ftp.password} 
          ftp.path
          =${ftp.path} 

          c。在引用的applicationContext文件節點propertyConfigurer中添加<value>classpath:ftp.properties</value>
          d。這時就可以直接使用占位符了
          如: <!--FTP Server-START-->
          <bean id="myFTP" class="com.xjgzinfo.app.util.MyFTP"
          lazy-init="true" destroy-method="destroy">
          <property name="server" value="${ftp.url}" />
          <property name="user" value="${ftp.username}" />
          <property name="password" value="${ftp.password}" />
          <property name="path" value="${ftp.path}" />
          </bean>
          <!--FTP Server-END-->

          3。測試Dao時報如下錯誤該怎么解決?
          Unsatisfied dependency expressed through bean property 'interfaceDataDefineDao': Set this property value or disable dependency checking for this bean
          解答:以下列出3種解決方法,優先選擇方法1。
          解決方法1:測試初始化沒有自動加載webapp/WEB-INF/applicationContext.xml文件,把該文件復制到target\classes\目錄下即可。
          解決方法2:修改build path將src/main/webapp的out put folder修改成default output folder(實際是target/classes),此方法實際是將webapp里的文件都復制到target/classes里,使得測試過程可以訪問到applicationContext.xml文件。
          解決方法3:復制target/test-classes/的WEB-INF文件夾到target/classes目錄下,使得集成測試可以訪問該目錄下的文件。

          4。怎樣解決測試報“Embedded error: java.sql.SQLException: ORA-01401: 插入的值對于列過大”錯誤?
          解答:檢查hmb2ddl生成的數據表字段的實際類型及大小,再跟test/resources/samples-data.xml里對應的表的測試數據比較,檢查是否跟實際數據庫表中數據類型不匹配。有時appfuse自動產生的測試數據過大造成的,修改測試數據即可。

          5。怎樣使用本地mvean_proxy?
          解答:本地運行java -jar maven-proxy-standalone-0.2-app.jar maven-proxy.properties,然后在pom.xml庫中修改配置

          <repositories> 
          <repository> 
          <id>central</id> 
          <name>xjgzinfo Central Repository</name> 
          <url>http://135.224.80.12:9999/repository</url> 
          <snapshots> 
          <enabled>true</enabled> 
          </snapshots> 
          <releases> 
          <enabled>true</enabled> 
          </releases> 
          </repository> 
          <repository> 
          <id>appfuse</id> 
          <url>http://static.appfuse.org/repository</url> 
          </repository> 
          <repository> 
          <id>Maven2</id> 
          <url>http://repo1.maven.org/maven2</url> 
          </repository> 
          <!-- Needed for Facelets --> 
          <repository> 
          <id>java.net</id> 
          <url>http://download.java.net/maven/1/</url> 
          <layout>legacy</layout> 
          </repository> 
          </repositories> 

          <pluginRepositories> 
          <pluginRepository> 
          <id>central</id> 
          <name>xjgzinfo Central Repository</name> 
          <url>http://135.224.80.12:9999/repository</url> 
          </pluginRepository> 
          <pluginRepository> 
          <id>appfuse</id> 
          <url>http://static.appfuse.org/repository</url> 
          </pluginRepository> 
          </pluginRepositories> 

          6。使用JSF時怎樣向菜單上添加子菜單,及關聯事件處理代碼?
          解答:a.首先在ApplicationResources.properties資源配置文件中添加變量
          例如:# -- auditReport day tast page --
          auditReportDayTask.execute=AuditReport Task Run
          auditReportDayTask.executed=AuditReport has been runned successfully.
          # -- AuditReport-END
          b.向AuditReportForm.java文件中添加事件響應代碼
          例如: public String auditDay() {
          auditReportManager.autoAuditDay();
          addMessage("auditReportDayTask.executed");

          return "mainMenu";
          }
          c.在mainMenu.xhtml文件中添加commandLink
          例如:<h:commandLink value="#{text['auditReportDayTask.execute']}" action="#{auditReportForm.auditDay}"/>

          7。怎樣向Mvean中installing 3rd party JARs?
          解答:To install a JAR in the local repository use the following command:

          mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
          -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

          例如:mvn install:install-file -Dfile=quartz-1.6.0.jar -DgroupId=quartz -DartifactId=quartz -Dversion=1.6.0 -Dpackaging=jar

          8。怎樣在pom.xml中配置3rd party JARs,使得mvn integration-test集成測試后打包成war文件?
          解答:向dependency中添加依賴,通過第7步的安裝就可以從本地庫中引用第三方jar文件了。
          例如:
           <dependency> 
          <groupId>quartz</groupId> 
          <artifactId>quartz</artifactId> 
          <version>${quartz.version}</version> 
          <scope>compile</scope> 
          </dependency> 
           
          <quartz.version>1.6.0</quartz.version> 


          9。怎樣使用Ioc在Spring框架中使用bean自動管理對象?
          解答:a.編寫需要被Spring管理的類,如MyFTP.java;
          b.在applicationContext.xml或applicationContext-resources.xml中配置bean;
          如:
          <!--FTP Server-START--> 
          <bean id="myFTP" class="com.xjgzinfo.app.util.MyFTP" 
          lazy-init
          ="true" destroy-method="destroy"> 
          <property name="server" value="${ftp.url}" /> 
          <property name="user" value="${ftp.username}" /> 
          <property name="password" value="${ftp.password}" /> 
          <property name="localResource" value="${ftp.localPath}" /> 
          <property name="remotePath" value="${ftp.remotePath}" /> 
          </bean> 
          <!--FTP Server-END--> 

          10。怎樣使用由Spring自動管理的bean對象? 
          解答:a.在需要使用該bean的類中添加該屬性對象,如NamedSQLParser xmlParser = null; 
          并添加相應的get和set方法; 
          b.在applicationContext.xml中配置該bean; 
          <!--AuditReportManager-START--> 
          <bean id="auditReportManager" 
          class
          ="com.xjgzinfo.app.service.impl.AuditReportManagerImpl"> 
          <constructor-arg ref="auditReportDao" /> 
          <property name="myftp" ref="myFTP" /> 
          <property name="xmlParser" ref="namedSQLParser" /> 
          </bean> 
          <!--AuditReportManager-END--> 

          c.在AuditReportManagerImpl.java類中直接使用xmlParser屬性,該對象已經在auditReportManager由Spring裝載時實例化了。

          11。在使用Spring框架時怎樣通過application context訪問資源?
          解答:a.通過虛擬路徑來存取.如果我們的資源文件位于CLASSPATH下,我們就可以通過這種方法來獲取資 源文件,獲取代碼如下:
          ApplicationContext context = new FileSystemXmlApplicationContext("src/org/spring/test/applicationContext.xml");
          Resource resource = context.getResource("classpath:message.properties");
          這里需要注意的是classpath:是spring約定的URL虛擬路徑.
          b.或通過實際路徑來存取:
          獲取代碼如下:
          ApplicationContext context = new FileSystemXmlApplicationContext("src/org/spring/test/applicationContext.xml");
          Resource resource = context.getResource("file:D:/JProject/SpringTest/src/message.properties");
          c.或通過相對路徑來存取:
          獲取方法:
          ApplicationContext context = new FileSystemXmlApplicationContext("src/org/spring/test/applicationContext.xml");
          Resource resource = context.getResource("src/message.properties");

          12。怎樣把Resource作為屬性來配置?
          解答:a.首先創建待配置bean的java源文件AuditReportManagerImpl.java,該文件中要有Resource類型屬性,如UrlResource,ClassPathResource,FileSystemResource,ServletContextResource,InputStreamResource,ByteArrayResource其中一種資源變量。例如private ClassPathResource xmlResource;
          b.然后在applicationContext.xml或applicationContext-resources.xml配置文件中配置該bean;
          如:
          <!--NamedSQLParser Server-START-->
          <bean id="namedSQLParser" class="com.xjgzinfo.app.util.NamedSQLParser">
          <property name="xmlResource" value="classpath:nativeSQL.hbm.xml"/>
          </bean>
          <!--NamedSQLParser Server-END-->
          c.通過web.xml中配置的監聽,如
          <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          該監聽會在web容器啟動時對該web應用實例化ApplicationContext對象,通過a,b兩步的配置ApplicationContext對象會實例化該資源,并獲取classpath下的nativeSQL.hbm.xml資源文件返回給AuditReportManagerImpl.java中的Resource類型屬性,在本例中我的xmlResource屬性就可以在AuditReportManagerImpl中直接使用了。

          13。怎樣使用命名查詢?
          解答:a.在src/main/resources/目錄下建立命名查詢的配置文件,如:nativeSQL.hbm.xml
          <?xml version="1.0" encoding="utf-8"?> 
          <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
          > 
          <!-- 
          This is the native SQL entity queries config document. 
          You must list all columns of the entity.The primary key must unique. 
          You will get a list with entity object. 
          --> 
          <hibernate-mapping> 
          <sql-query name="1000PARA01"> 
          <return alias="auditReport" 
          class
          ="com.xjgzinfo.app.model.AuditReport"> 
          </return> 
          <![CDATA[ 
          select min(rownum) as {auditReport.id}, 
          NULL as {auditReport.acct_date}, 
          NULL as {auditReport.acct_month}, 
          NULL as {auditReport.audit_name}, 
          product_type as {auditReport.field1}, 
          state as {auditReport.field2}, 
          count(1) as {auditReport.field3}, 
          NULL as {auditReport.field4}, 
          NULL as {auditReport.field5}, 
          NULL as {auditReport.field6}, 
          NULL as {auditReport.field7}, 
          NULL as {auditReport.field8}, 
          NULL as {auditReport.field9}, 
          NULL as {auditReport.field10}, 
          NULL as {auditReport.field11}, 
          NULL as {auditReport.field12}, 
          NULL as {auditReport.field13}, 
          NULL as {auditReport.field14}, 
          NULL as {auditReport.field15}, 
          NULL as {auditReport.field16}, 
          NULL as {auditReport.field17}, 
          NULL as {auditReport.field18}, 
          NULL as {auditReport.region_name}, 
          NULL as {auditReport.partition_id_region} 
          from ls6_para.product_t@ysdb 
          group by product_type, state 
          ]]> 
          </sql-query> 
          </hibernate-mapping> 

          b.在src/main/resources/hibernate.cfg.xml文件中添加資源文件
          <mapping resource="nativeSQL.hbm.xml" />
          c.在DaoHibernate實現java文件類中就可以通過調用super.getHibernateTemplate().findByNamedQuery(queryName);方法執行命名查詢了。而且super.getHibernateTemplate().findByNamedQueryAndNamedParam()方法支持帶參數的sql語句。
          d.注意!命名查詢語句必須給出完整的對應與AuditReport對象屬性的字段,否則會報“nested exception is java.sql.SQLException: 列名無效”的錯誤。當AuditReport對象設定關鍵字時,{auditReport.id}的值不能為NULL,否則會報“java.lang.NullPointerException”的錯誤;{auditReport.id}的值要唯一才能正確返回結果,否則返回的List<AuditReport>中的AuditReport對象都一樣。

          14。怎樣使用Dom4j讀取XML文件?
          解答:a。導入如下包:
          import org.dom4j.Document;
          import org.dom4j.DocumentException;
          import org.dom4j.Element;
          b。采用如下語句片段讀取XML節點:
          private List namedSQL;// 命名查詢列表,用于存放解析后得到的命名查詢

          private ClassPathResource xmlResource;// 待解析的XML文件名 
          try 
          // 采用Dom4j解析nativeSQL.hbm.xml文件 
          List errors = new ArrayList(); 
          Document doc; 
          XMLHelper xmlHelper 
          = new XMLHelper(); 
          doc 
          = xmlHelper.createSAXReader(this.xmlResource.getFilename(), 
          errors, XMLHelper.DEFAULT_DTD_RESOLVER).read( 
          this.xmlResource.getInputStream()); 
          final Element root = doc.getRootElement();// 取得root跟節點 
          // 遍歷XML樹 
          for (Iterator i = root.elementIterator("sql-query"); i.hasNext();) 
          Element sqlQuery 
          = (Element) i.next(); 
          this.namedSQL.add(sqlQuery.attributeValue("name"));// 獲取該節點的name屬性 
          }
           
          if (errors.size() != 0
          throw new MappingException("invalid configuration"
          (Throwable) errors.get(
          0)); 
          }
           
          }
           catch (DocumentException ex) 
          ex.getStackTrace(); 
          }
           catch (IOException ex) 
          ex.getStackTrace(); 
          }
           

          c。對以上語句的解釋,xmlResource是資源文件對應的變量,對應的XML文件就是以上13問答用到的nativeSQL.hbm.xml文件。該資源使用方法見以上12解答。XMLHelper.DEFAULT_DTD_RESOLVER是對XML文件中DOCTYPE標記進行解析的解析器,該解析器碰到http://hibernate.sourceforge.net描述資源時首先會到hibernate-3.2.5.ga.jar文件的org/hibernate路徑下尋找指定的dtd文件,如果找不到才會到http://hibernate.sourceforge.net網站讀取該dtd文件。具體使用方法可以參照hibernate-3.2.5.ga.jar/org/hibernate/util/下的XMLHelper.class、ConfigHelper.class和DTDEntityResolver.class

          15。測試時報“ Error executing database operation: CLEAN_INSERT
          Embedded error: Listener refused the connection with the following error:
          ORA-12518, TNS:listener could not hand off client connection
          The Connection des.c.r.i.p.tor used by the client was:
          localhost:1521:Ora9i”怎么處理?
          解答:提示這個錯誤的原因是由于當前配置的Ora9i數據庫不能連接。檢查網絡或開啟數據庫實例。

          16。使用Oracle作數據庫時,PL/SQL Developer工具連接數據庫正常,但是測試時報“[INFO] Error executing database operation: CLEAN_INSERT
          Embedded error: my_table_name_t”,是怎么回事?

          解答:這是因為在appfuse中數據庫配置方案名要大寫,如下配置:
          <!-- Database settings --> 
          <dbunit.dataTypeFactoryName> 
          org.dbunit.ext.oracle.OracleDataTypeFactory 
          </dbunit.dataTypeFactoryName> 
          <dbunit.schema>HR</dbunit.schema><!-- Make sure to capitalize the schema name 這里已經清楚地告訴我們方案名要大寫--> 
          <dbunit.operation.type>CLEAN_INSERT</dbunit.operation.type> 
          <hibernate.dialect> 
          org.hibernate.dialect.Oracle9iDialect 
          </hibernate.dialect> 
          <hibernate.show_sql>true</hibernate.show_sql> 
          <jdbc.groupId>com.oracle</jdbc.groupId> 
          <jdbc.artifactId>ojdbc14</jdbc.artifactId> 
          <jdbc.version>10.2.0.2.0</jdbc.version> 
          <jdbc.driverClassName> 
          oracle.jdbc.OracleDriver 
          </jdbc.driverClassName> 
          <jdbc.url> 
          <![CDATA[jdbc:oracle:thin:@localhost:1521:Ora9i]]> 
          </jdbc.url> 
          <jdbc.username>hr</jdbc.username> 
          <jdbc.password>hr</jdbc.password> 

          17。采用Hibernate3時對于大批量保存操作該怎么處理?
          解答:對于大批量的數據入庫操作,要定量刷新緩存,采用super.getSession().flush(),Hibernate會自動生成原始SQL語句,將內存中的數據持久化到數據庫中。否則會導致內存溢出。采用如下語句:
          /** 
          * 批量插入VO 

          @param objs 
          * 批量插入transient態的VO結果集 
          */
           
          public List batchInsert(List<AuditReport> objs) 
          log.debug(
          "Inserting into table by List<AuditReport>."); 

          try 
          for (int i = 0; i < objs.size(); i++
          super.getHibernateTemplate().save(objs.get(i)); 
          if (i % 20 == 0
          super.getSession().flush(); 
          super.getSession().clear(); 
          }
           
          }
           
          return objs; 
          }
           catch (DataAccessException dae) 
          log.error(
          "insert into table by collection fail.", dae); 
          throw dae; 
          }
           catch (RuntimeException re) 
          log.error(
          "insert into table by collection fail.", re); 
          throw re; 
          }
           
          }
           

          posted on 2008-06-25 22:10 gdufo 閱讀(1823) 評論(0)  編輯  收藏 所屬分類: Appfuse

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          Hibernate

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 林州市| 株洲市| 吴旗县| 濉溪县| 宜都市| 铁岭市| 郑州市| 安化县| 长武县| 井研县| 崇明县| 沽源县| 双桥区| 望城县| 元氏县| 沛县| 江城| 浪卡子县| 博罗县| 南宫市| 九龙坡区| 盖州市| 习水县| 东城区| 苍山县| 油尖旺区| 新泰市| 门头沟区| 海城市| 德安县| 建宁县| 屯昌县| 成都市| 万宁市| 惠来县| 文登市| 丹阳市| 康保县| 定结县| 石台县| 池州市|