Exception in thread "main" java.lang.StackOverflowError异常问题

问题描述

今天在学习Hibernate一对一关联的时候,跟平常一样,写POJO,然后测试时,报了Exceptioninthread"main"java.lang.StackOverflowError异常,百度得知是程序陷入了无限循环,但就是一个简单的JavaBean,也没有哪段程序会导致错误,然后经过测试,给Person和IdCard加入了无参构造,程序就能正常运行了,但不知道原理是什么,求大神解答packagecom.wss.dao;publicclassPerson{privateIntegerid;privateStringname;privateIdCardidCard;publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicIdCardgetIdCard(){returnidCard;}publicvoidsetIdCard(IdCardidCard){this.idCard=idCard;}publicPerson(){//TODOAuto-generatedconstructorstub}}

packagecom.wss.daoUtil;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.hibernate.cfg.Configuration;importcom.wss.dao.IdCard;importcom.wss.dao.Person;publicclassTest{publicstaticvoidmain(String[]args){Configurationcfg=newConfiguration().configure();SessionFactorysf=cfg.buildSessionFactory();Sessionsession=sf.openSession();Transactiontr=session.beginTransaction();System.out.println("a");//新建对象Personperson=newPerson();person.setName("张三");IdCardidCard=newIdCard();idCard.setNumber("341218575464");//建立联系tr.begin();person.setIdCard(idCard);idCard.setOwner(person);//保存session.save(person);session.save(idCard);//提交事件tr.commit();session.close();}}

解决方案

解决方案二:
你把SQL语句打印出来就知道了,然后你这个应该是双向一对一吧,你配置正确了嘛?把idCard.setOwner(person);删了试一试
解决方案三:
删掉idCard.setOwner(person);和session.save(idCard);试试
解决方案四:
好像有时候对象相互引用死循环会出这个异常

时间: 2024-10-28 06:50:35

Exception in thread "main" java.lang.StackOverflowError异常问题的相关文章

jdk-Exception in thread "main" java.lang.

问题描述 Exception in thread "main" java.lang. 从svn上down下来的源码,在搭建本机环境的时候报错 Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apdplat/word/analysis/TextSimilarity : Unsupported major.minor version 52.0 2 at java.lang.Cl

求解答-最好运行结果说是Exception in thread "main" java.NoSuchMethordError:main

问题描述 最好运行结果说是Exception in thread "main" java.NoSuchMethordError:main import java.applet.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; //import java.Math.*; public class cal extends Applet { //定义所有需要使用的全局变量 String str="

hashmap-Java中遇到的一个异常Exception in thread "main" java.lang.NullPointerException

问题描述 Java中遇到的一个异常Exception in thread "main" java.lang.NullPointerException import java.util.*; public class NewHashTest{ public static void main(String[] args) { HashMap hash = new HashMap(); Emp emp1 = new Emp(""laoda""111&q

java sql 错误-Exception in thread "main"

问题描述 Exception in thread "main" 5C Exception in thread ""main"" java.lang.NullPointerException 老是出现这个错误,是为什么呢 解决方案 线程里面出现了空指针异常 解决方案二: AxisFault faultCode: {http://xml.apache.org/axis/}HTTP faultSubcode: faultString: (400)Bad

List操作错误Exception in thread "main" java.lang.IllegalStateException

问题描述 Iterator<Teacher> iter = teacherList.iterator();while(iter.hasNext()){Teacher teacher =iter.next();//①查找老师的最大上限值,如果该老师已经达到上限了,则将该老师从List中拿出,不予安排if(teacher.getMaxnum()<=teacher.getRealnum()){iter.remove();teacherList.remove(teacher);}//②找到该考试

求解答-运行结果说是Exception in thread &amp;amp;quot;main&amp;amp;quot; java.NoClassDeFoundError:Example

问题描述 运行结果说是Exception in thread "main" java.NoClassDeFoundError:Example import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; import j

Exception in thread &amp;amp;quot;main&amp;amp;quot; java.lang.NoClassDefFoundError:

问题描述 <?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by

Exception in thread &amp;amp;quot;main&amp;amp;quot; java.lang.Exception: 网页内容获取异常!Http Status Code:403

问题描述 packagecom.mobile.util;importjava.util.regex.Matcher;importjava.util.regex.Pattern;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.NameValuePair;importorg.apache.commons.httpclient.methods.PostMethod;importorg.

java程序编译成功但运行不了(Exception in thread &amp;amp;quot;main&amp;amp;quot; java.lang.IllegalArgumentException

问题描述 D:work>javac JComboBoxDemo.javaD:work>java JComboBoxDemoException in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself at java.awt.Container.checkAddToSelf(Unknown Source) at java.awt.Container.addI