異常處理---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

 

 

 

 

你可能感兴趣的:(thread,sql,Hibernate)