`

異常處理---01

阅读更多

公司原有的項目我負責的那部份oneToOne oneToMany以及ManyToMany等關係 今天進行聯合查詢時 爆出異常

異常一:failed to lazily initialize a collection of role

原因:

解決方法:

 

網上搜尋了資料  主要是延迟加载搞的鬼,对于一对多的时候最好是

異常二: org.hibernate.LazyInitializationException: could not initialize proxy - no Session

原因是:

 

异常三:a different object with the same identifier value was already associated with the sessionyuang原因: 在session中存在两个对象 关联的同一个标志位,其实这是同一个对象,但自己在做业务操作的时候可能保存了两次,

解决方法

      一是:最笨的方法 是手写HQL 

      二是:调用this.hibernateTemplate.merge();替代his.hibernateTemplate.update()  

 

 

 

 

異常四:Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value

原因: 一個不能為空的屬性引用了一個為空或為無效的值.

       解決方法: 打印將進行操作的對象的各項屬性和值 并檢查各個值是否為空或為無效的值,尤其是對有級聯操作的數據時更要仔細檢查

 

異常五:java.util.NoSuchElementException

原因:  一個迭代器被執行兩次,最好一個為空.

官方的解釋為:

An object of this class is thrown when a collection or iterator is empty. For example, when the next method of an Iterator instance is called when hasNext of that instance returns false as in the code below.

  ArrayList list = new ArrayList();
  list.add(new String("hello"));
  Iterator it = list.iterator();
  System.out.println(it.next());   // ok, list has one element
  System.out.println(it.next());   // NOT ok, iterator is "empty"
 

 修改為:

			for(Ability o: cacheList){
				Iterator<Ability> oldIt = OLDLIST.iterator();
				while(oldIt.hasNext()){
					Ability ability_1 = oldIt.next();
					if(ability_1.getAbilityCode().equals(o.getAbilityCode())){
						System.out.println("----移除OLDLIST共有對象---"+o.getAbilityName());
						oldIt.remove();//先將Iterator移除掉 然後進行數據各項操作
						OLDLIST.remove(ability_1);//移除對象
					}
				}
			}

  又報異常java.util.ConcurrentModificationException

原因為:因為Iterator 有一個独立的线程,并且拥有一个 mutex 锁。當 對Iterator進行迭代式 不能對其內的數據進行刪除和添加操作.否則會報該異常,解決方法是 先把迭代器移除 然後進行數據操作.

官方解解為:

public class ConcurrentModificationException extends RuntimeException

This exception may be thrown by methods that have detected concurrent modification of an object
 when such modification is not permissible.

For example, it is not generally permssible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these 
circumstances. Some Iterator implementations (including those of all the collection implementations 
provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that 
do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, 
non-deterministic behavior at an undetermined time in the future.

Note that this exception does not always indicate that an object has been concurrently modified by 
a different thread. If a single thread issues a sequence of method invocations that violates the 
contract of an object, the object may throw this exception. 
For example, if a thread modifies a collection directly while it is iterating over the collection 
with a fail-fast iterator, the iterator will thow this exception.

Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, 
impossible to make any hard guarantees in the presence of unsynchronized concurrent modification.
 Fail-fast operations throw ConcurrentModificationException on a best-effort basis. Therefore, it 
would be wrong to write a program that depended on this exception for its correctness: 
ConcurrentModificationException should be used only to detect bugs. 

  繼續修改代碼改為:

			for(Ability o: OLDLIST){
				System.out.println("---OLDLIST--"+o.getAbilityName());
				for(Iterator<Ability> newIt= NEWLIST.iterator(); newIt.hasNext();){
					Ability ability_1 = newIt.next();
					if(ability_1.getAbilityCode().equals(o.getAbilityCode())){
						System.out.println("----移除NEWLIST共有對象---"+o.getAbilityName());
						newIt.remove();
						NEWLIST.remove(ability_1);
						cacheList.add(o);
					}
				}
			}

 

  異常五: deleted object would be re-saved by cascade (remove deleted object from associations)

	/**
	 * 所處小組,需要包括管理組
	 * targetEntity 指明相關聯的類為Group.class
	 * FetchType.EAGER 指明預先獲取數據
	 * FetchType.LAZY  延時加載數據
	 * cascade=CascadeType.ALL 表示支持所有的級聯操作
	 * optional = false 表示可允許子集為空
	 * @return group
	 */
	@JsonIgnore
	@ManyToOne(targetEntity=Group.class,cascade=CascadeType.ALL,fetch = FetchType.EAGER)
	@JoinColumn(name="groupID",referencedColumnName="id")
	public Group getGroup() {
		return group;
	}

 異常原因是:

@ManyToOne(targetEntity=Group.class,cascade=CascadeType.ALL,




fetch = FetchType.EAGER)

將其改為:

cascade=CascadeType.REFRESH




就ok了

心得:對於注解掌握的的知識不強!

 

SQL 修改列名和表名

        一: EXEC sp_rename '表名.[列名]' , '新列名' ,'COLUMN'

        二: EXEC sp_rename '表名 ','新表名'.

 

異常六: javax.xml.transform.TransformerConfigurationException: 无法编译样式表

原因有兩種: 一是沒有解析包 可以導入xmlparserv21.jar 或者crimson-1.1.3.jar 等解析.xml和.xsl的jar包

                二是: 文件的路徑或者文件名不正確

 

異常七:javaEE1.5發送郵件時報出異常 Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/QEncoderStream

原因是:jar包之間有衝突,將mail包刪除 或者把換成java1.4EE 的開發環境 就可以在本地測試了.

 

異常八:Eclipse  No embedded stylesheet instruction for file

原因是: 當打開一個xml文件時,單擊Eclipse上的綠色運行按鈕時 拋出異常 并生成一個新的xml文件,解決方法把窗口顯示為你將要運行的java文件,重新運行 就好了! 這可能是Eclipse的一個bug!

 

異常九 spring BeanDefinitionStoreException Caused by: java.net.ConnectExceptio

貌似是網絡原因,其實是 xml文件DTD解析錯誤. 

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.5.dtd">
 

改為:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 

異常十:javax .mail .MessagingExceptionCould not connect to SMTP hostsmtp .163.comport25

原因 一:本地計算機把端口號25 關閉了,比如一些防護軟件爲了阻止惡意軟件攻擊,會自動關閉端口25

原因二: 163郵箱服務器已關閉該端口

原因三:本地網絡不通.


異常十一:發佈項目是出現異常

 

java.lang.NoClassDefFoundError: 


javax


/el


/


ExpressionFactor









   解決方案: 將 jstl.jar;jsf-impl.jar;javaee.jar;jsf-api.jar 放在tomcat .xx\common\lib文件夾下

 

異常十二:                                   Caused

by: java.lang.IllegalStateException: Neither BindingResult nor plain

target object for bean name 'registrationForm' available as request

attribute


	at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
	at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
	at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
	at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
	at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:147)
	at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:138)
	at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:122)
	at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:408)
	at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
	at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
	at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
	at org.apache.jsp.WEB_002dINF.views.registrationform_jsp._jspx_meth_form_005finput_005f0(registrationform_jsp.java:295)
	at org.apache.jsp.WEB_002dINF.views.registrationform_jsp._jspService(registrationform_jsp.java:122)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)

 

異常十三:

java.lang.AbstractMethodError:javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext;

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.AbstractMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext;
	at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:498)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

  原因是Tomcat5.x不支持 jstl1.2 一些語法,它只實現了servlet2.4 和jsp2.0的相應規範,tomcat6.x實現了servlet2.5  和jsp2.1相應規範.

兩種解決方法將:

  一:將服務器tomcat5.x 升級為tomcat 6.x

  二:將jstl1.2jar包換成 jstl1.1jar

 

 

 

 

分享到:
评论

相关推荐

    实验五 Java异常处理实验.docx

    Java 异常处理实验 Java 异常处理是 Java 语言中的一种错误处理机制,用于捕获和处理程序执行过程中出现的异常情况。异常是指程序执行过程中出现的非正常情况,例如除零错误、数组下标越界等。 实验目的 本实验的...

    精品课件-Python常见异常处理.pptx

    01 异常简介 02 异常类 03 异常处理 目录页 04 抛出异常 05 自定义异常 06 with和as环境安装器 精品课件----Python常见异常处理全文共71页,当前为第3页。 异常介绍 在Python中,程序在执行的过程中产生的错误称为...

    Lab01-异常处理-调试

    Lab01a异常处理和调试 关于本计划 代码研究员401 ASP.NET实验1的实践与异常处理和调试,根据用户的输入执行简单的数学方程式。 设置 克隆回购 在Visual Studio或其他选择的IDE中打开并运行文件 视觉的 该程序是一个...

    计算机后端-Java-Java核心基础-第17章 异常处理 01. 每天一考.avi

    计算机后端-Java-Java核心基础-第17章 异常处理 01. 每天一考.avi

    老男孩python 网盘下载

    课时28 04-11-异常及异常处理 课时29 04-辅助-代码调试 课时3 04-01-辅助-wj01-pythoner.cn-os 课时30 05-04-测试驱动开发 课时31 05-03-继承及模块化开发 课时32 05-02-重载 课时33 05-01-面向对象 课时34 06-01-子...

    SpringBoot 服务调用异常重试补偿处理

    1. 启动Demo01和Demo02项目 2. 请求 地址:127.0.0.1:8080/demo01/send/post 报文:{"sleepNum":5} 3. 测试目的 ... 停止demo02, 请求demo01,出现网络异常, demo01会执行三次重试后在进行补偿处理

    《Java程序设计案例教程》教学课件07异常处理.pptx

    模块7 异常处理 《Java程序设计案例教程》教学课件07异常处理全文共31页,当前为第1页。 学习目标 01 了解异常产生的原因。 02 了解Java异常体系结构,以及受检异常和未受检异常的区别。 03 掌握使用try-catch-...

    Python语言程序设计教程 北理工Python课程W02V01-Python程序元素分析 共19页.pdf

    4-1-4-异常处理设计 4-1-5-三者最大实例分析 4-2-1-基本循环结构 4-2-2-通用循环构造方法 4-2-3-死循环半路循环 4-2-4-布尔表达式 6-1-1-文件的基础 6-1-2-文件的基本处理 6-1-3-文件实例一 6-1-4-文件实例二 6-2-1-...

    Java编程实训 Java开发基础入门教程1-09 Java异常处理机制和调试(共32页).pptx

    【完整课程列表】 ...09 Java异常处理机制和调试(共32页).ppt 10 java集合框架(共27页).ppt 11 JAVA UI swing编程基础(共39页).ppt 12 AWT布局管理器(共14页).ppt 13 AWT事件处理模型(共29页).ppt

    HCIP-Big Data培训视频教程【共43集】.rar

    26 java-异常处理 27-29 大数据离线批量场景化解决方案 30-34 大数据实时检索场景化解决方案 35-37 大数据实时流处理场景化解决方案 38 Readis.1 39-42 大数据实时流处理场景化解决方案 43 ReadisB

    Python语言程序设计教程 北理工Python课程W01V06-理解问题的计算部分 共9页.pdf

    4-1-4-异常处理设计 4-1-5-三者最大实例分析 4-2-1-基本循环结构 4-2-2-通用循环构造方法 4-2-3-死循环半路循环 4-2-4-布尔表达式 6-1-1-文件的基础 6-1-2-文件的基本处理 6-1-3-文件实例一 6-1-4-文件实例二 6-2-1-...

    Visual C# 开发技术全集

    第01章 C#与.NET Framework简介--- 第02章 C#程序设计基础------ 第03章 与用户交互------- ...第07章 处理异常和错误----- 第08章 XML编程-------- 第09章 应用程序部署------- 第10章 时间跟踪系统-----

    实验室程序文件.docx

    29 HHRBDC/CX01-29-2010 检测工作程序 30 HHRBDC/CX01-30-2010 现场检测管理程序 31 HHRBDC/CX01-31-2006 应急检测工作程序 32 HHRBDC/CX01-32-2010 检测过程中发生异常情况处理程序 33 HHRBDC/CX01-33-2010 事故...

    Golang mk教程-Go语言视频零基础入门到精通项目实战web编程

    第07天-进阶-接口实例、终端文件读写、异常处理 第08天-进阶-goroute详解、定时器与单元测试 第09天-高级-网络协议TCP、Redis与聊天室 第10天-高级-Web开发与Mysql数据库 第11天-高级-项目实战-日志收集系统kafka库...

    SynEdit Studio V2005.09.05

    ---------- ---------- v1.2(2005.05.29) ---------- - 01.TSynEdit,稍稍修改使用缓冲技术后可能出现的异常; - 02.SynEditActions,一个包含各种synedit动作的单元,目前只提供测试,没有注册单元; ---------- -------...

    SynEdit Studio V2005.07.29

    ---------- ---------- v1.2(2005.05.29) ---------- - 01.TSynEdit,稍稍修改使用缓冲技术后可能出现的异常; - 02.SynEditActions,一个包含各种synedit动作的单元,目前只提供测试,没有注册单元; ---------- -------...

    SynEdit Studio V2005.09.18 beta1.00

    ---------- ---------- v1.2(2005.05.29) ---------- - 01.TSynEdit,稍稍修改使用缓冲技术后可能出现的异常; - 02.SynEditActions,一个包含各种synedit动作的单元,目前只提供测试,没有注册单元; ---------- -------...

    SynEdit Studio V2005.08.29

    ---------- ---------- v1.2(2005.05.29) ---------- - 01.TSynEdit,稍稍修改使用缓冲技术后可能出现的异常; - 02.SynEditActions,一个包含各种synedit动作的单元,目前只提供测试,没有注册单元; ---------- -------...

    spring mvc统一处理异常

    spring mvc统一处理异常,通过@ControllerAdvice+@ExceptionHandler

    程序文件------模具管理程序.doc

    " "4.5在生产过程中发现模具异常,必须马上停止生产,退回模房返修。 " "4.6为防模具在使用过程中变形,收水。品质部每天必须对所做出的产品进行检 " "检查和尺寸测量。 " "4.7在生产中因变形或收水,老化等问题要...

Global site tag (gtag.js) - Google Analytics