最近学习SSH出现这个情况,也比较奇怪,我用junit测试的没问题,但提交的表单数据就有问题出现org.hibernate.MappingException: Unknown entity:异常: stujiating1 : com.xjgl.server.entity.Stujiatinginfotb@5868d40c Hibernate: select stujiating0_.id as id1_, stujiating0_.sno as sno1_, stujiating0_.jiatingjiegou as jiatingj3_1_, stujiating0_.jiatingfenwei as jiatingf4_1_, stujiating0_.jiatingdizhi as jiatingd5_1_, stujiating0_.jiatingdianhua as jiatingd6_1_, stujiating0_.youbian as youbian1_, stujiating0_.dushengzhinv as dushengz8_1_ from stujiatinginfo stujiating0_ where stujiating0_.sno=? 2014/11/10 12:48:05[DEBUG][com.xjgl.server.dao.StujiatinginfoDao]:144 -- 添加学生101_1的家庭信息成功 ================================================== student.getStujiatinginfo() : com.xjgl.server.entity.Stujiatinginfotb@1fd7855 stujiating2.sno : 101_2 stujiating2.youbian : Hibernate: insert into student (name, sno, id) values (?, ?, ?) Hibernate: insert into stujiatinginfo (sno, jiatingjiegou, jiatingfenwei, jiatingdizhi, jiatingdianhua, youbian, dushengzhinv, id) values (?, ?, ?, ?, ?, ?, ?, ?) Hibernate: select stujiating0_.id as id1_, stujiating0_.sno as sno1_, stujiating0_.jiatingjiegou as jiatingj3_1_, stujiating0_.jiatingfenwei as jiatingf4_1_, stujiating0_.jiatingdizhi as jiatingd5_1_, stujiating0_.jiatingdianhua as jiatingd6_1_, stujiating0_.youbian as youbian1_, stujiating0_.dushengzhinv as dushengz8_1_ from stujiatinginfo stujiating0_ where stujiating0_.sno=? 2014/11/10 12:48:05[ERROR][org.apache.struts2.dispatcher.Dispatcher]:38 -- Exception occurred during processing request: Unknown entity: com.xjgl.server.entity.Stujiatinginfotb$$EnhancerByCGLIB$$5d40c870 org.hibernate.MappingException: Unknown entity: com.xjgl.server.entity.Stujiatinginfotb$$EnhancerByCGLIB$$5d40c870 at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:691) at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1485) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697) at com.xjgl.server.dao.StujiatinginfoDao.saveStujiatinginfo(StujiatinginfoDao.java:143) 程序代码: Stujiatinginfotb stujiating1 = new Stujiatinginfotb(); stujiating1.setId(sid + "_1"); stujiating1.setSno(sno + "_1"); System.out.println(" stujiating1 : " + stujiating1); stujiatinginfoDao.saveStujiatinginfo(stujiating1); System.out.println(" =================================================="); Stujiatinginfotb stujiating2 = new Stujiatinginfotb(); System.out.println(" student.getStujiatinginfo() : " + student.getStujiatinginfo()); stujiating2 = student.getStujiatinginfo(); stujiating2.setId(sid + "_2"); stujiating2.setSno(sno + "_2"); System.out.println(" stujiating2.sno : " + stujiating2.getSno()); System.out.println(" stujiating2.youbian : " + stujiating2.getYoubian()); stujiatinginfoDao.saveStujiatinginfo(stujiating2); log.debug("保存学生家庭信息成功"); 测试代码: @Test public void testPutStudent() throws MyException { StudentAll student = new StudentAll(); Stujiatinginfotb stujiatinginfo = new Stujiatinginfotb(); student.setStujiatinginfo(stujiatinginfo); student.setName("zhangsan"); student.setSno("901"); boolean b = teacherService.putStudent(student); System.out.println("return : " + b); } |
|
stujiating1成功,stujiating2出在异常信息不成功
Stujiatinginfotb stujiating1 = new Stujiatinginfotb(); stujiating1.setId(sid + "_1"); stujiating1.setSno(sno + "_1"); stujiatinginfoDao.saveStujiatinginfo(stujiating1); Stujiatinginfotb stujiating2 = new Stujiatinginfotb(); stujiating2 = student.getStujiatinginfo(); stujiating2.setId(sid + "_2"); stujiating2.setSno(sno + "_2"); stujiatinginfoDao.saveStujiatinginfo(stujiating2); log.debug("保存学生家庭信息成功"); |
|
System.out.println(” student.getStujiatinginfo() : ” + student.getStujiatinginfo());
stujiating2 = student.getStujiatinginfo(); 这2句有问题, 应该是用懒加载 获得student吧, 可能哪里用的不对 暂时没看出来 可以贴出 更详细的 如果仅仅 是为了保存 就去掉 这2句 应该可以 |
|
org.hibernate.MappingException: Unknown
这种的话 可能是因为 你没有 映射文件 student的 可以仔细检查下~ 在hibernate.cfg.xml 里面 是否 配置了 |
|
我没有配置懒加载这一项,默认的。去掉这两句就成stujiating1那样了,那样就可以成功的,就是不知道为什么。 |
|
有映射文件,stujiating1是能保存成功的,如果没有映射文件stujiating1是不能保存成功的吧 |
|
40分 |
加Q。可远程
|
是不是邮编youbian字段的问题呢??
|
|
我的892395619 |
|
没有啊,那个只是我测试打印到控制台的信息,我测试的是能得到那个字段的信息的 |
|
有没有能解决的、远程也可以,谢谢
|
|
应该是懒加载的问题,在映射文件里加lazy=”””false”
|
|
这个解决了,是因为我的这个StudentAll中定义几个实体,但没有new这个几个实体的对象,结果struts2自动对这几个实体new了对象,但new的是子类,和要用的实体不对应,所以出现了org.hibernate.MappingException: Unknown entity: com.xjgl.server.entity.Stujiatinginfotb$$EnhancerByCGLIB$$5d40c870(如果是自己new 的应该是Stujiatinginfotb而不是Stujiatinginfotb$$EnhancerByCGLIB$$5d40c870)
|