异常1:not-null property references a null or transient value
解决方法:将“一对多”关系中的“一”方,not-null设置为false
(参考资料:http://www.thearcmind.com/confluence/pages/viewpage.action?pageId=212)
异常2:org.hibernate.TransientObjectException: object references an unsaved transient instance
解决方法一:cascade="save-update,persist"
(参考资料:http://www.laliluna.de/254.html)
解决方法二:对于允许为空的外键实体类先判断再装载可避免异常抛出。
例:
/**
* 保存
* @return
* @throws Exception
*/
public String save() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
String root = request.getRealPath("/");
if(fileName==null||fileName.trim().equals("")){
fileName="/images/nopic.jpg";
this.getBrand().setLogopic(fileName);
}
else
this.getBrand().setLogopic(Upload.toFile(myFile,fileName, root,"/brand/pic/"));
if(request.getParameter("activityId")==null||"".equals(request.getParameter("activityId")))
this.getBrand().setActivity(null);
else
{
Activity activity = new Activity();
activity.setId(Integer.parseInt(request.getParameter("activityId")));
this.getBrand().setActivity(activity);
}
brandService.save(brand);
return SUCCESS;
}
异常3:org.hibernate.QueryException: could not resolve property
解决方法:"from Category category where category.userID = :userID"修改为"from Category category whereuserID = :userID"或者"from Category category where category.user.id = :userID"
(参考资料:http://www.laliluna.de/277.html)
异常4:could not initialize proxy - the owning Session was closed
解决方法:设置lazy为false
(参考资料:http://forum.springframework.org/showthread.php?t=27993)
异常2我在应用中碰到了这样的问题:
有三个表:userInfo deptmentInfo role
userInfo 与deptmentInfo为many to one
userInfo 与role为many to one
下面为保存UserInfo对象时的代码:
DeptmentInfo dept = new DeptmentInfo();
dept.setDeptName(deptName);
Role role = new Role();
role.setRoleName(roleName);
UserInfo user = new UserInfo();
user.setUserName(userName);
user.setUserSex(userSex);
user.setDuty(duty);
user.setPhone(phone);
user.setMobileNum(mobileNum);
user.setEmail(email);
user.setQq(qq);
user.setMsn(msn);
user.setAdress(adress);
user.setDeptmentInfo(dept);
user.setRole(role);
dao.save(user);
执行时有错:org.hibernate.TransientObjectException: object references an unsaved transient instance
于是改了UserInfo.hbm.xml的一些地方
如下:
<many-to-one name="role" class="com.oa.domain.Role"
cascade="save-update,persist" fetch="select">
<column name="ROLE_ID" precision="22" scale="0" />
</many-to-one>
<many-to-one name="deptmentInfo"
class="com.oa.domain.DeptmentInfo" cascade="save-update,persist"
fetch="select">
<column name="DEPT_ID" precision="22" scale="0" />
</many-to-one>
加了上面红色部分的,就OK了,能保存了。
相关推荐
在Java世界中,Java Persistence API(JPA)是Oracle公司推出的用于对象关系映射(ORM)的标准框架,它简化了数据库操作,使开发者能够用Java对象来操作数据库记录。本教程将深入探讨JPA中的一对多双向关联及级联...
3.Caused by:org.hibernate.TransientObjectException:object references an unsaved transient instance 原因:代码中关联的对象没有set值进去。查看代码,发现是enterPriseStrength和enterprise为多对一
标题中的"PyPI 官网下载 | autolink-references-mkdocs-plugin-0.2.0.tar.gz"指的是在Python Package Index (PyPI) 官网上可以找到的一个开源项目,名为`autolink-references-mkdocs-plugin`,其版本为0.2.0,打包...
《PyPI官网下载参考资料解析器:references_parser-0.0.1-py3-none-any.whl》 在Python编程环境中,PyPI(Python Package Index)是官方的第三方库仓库,为开发者提供了丰富的软件包和模块资源。PyPI上的每一个发布...
### AN12900: Secure Over-the-Air Prototype for Linux Using CAAM and Mender #### 1. Introduction Over-the-Air (OTA) updates represent an efficient method for Original Equipment Manufacturers (OEMs) ...
iPhone应用开发中的Object-C内存管理是开发者必须掌握的关键技能,特别是在资源有限的移动设备上,如iPhone OS(现称为iOS)。内存管理不当可能导致程序崩溃,严重影响用户体验。Object-C的内存管理基于引用计数机制...
在调试过程中,可能会遇到`Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance`这样的异常。这个异常通常是因为在持久化操作时,关联的对象没有被正确设置。例如...
更新查询语句:LY=计算机学报 OR LY=软件学报 OR LY=中国科学信息科学 OR LY=中国科学通报 OR LY=电子学报 OR LY=通信学报 OR
C++ For Artists: The Art, Philosophy, and Science of Object-Oriented Programming by Rick Miller ISBN:1932504028 Biblio Distribution ? 2003 (590 pages) Intended as both a classroom and reference ...
微分进化算法的一些参考文献,仅供学习和科研使用,祝愉快。
Feature 1 - Project Cleaner: 1、Actions for unused files 2、Enhanced Project View Feature 2 - Find References: 1、Find references, usages, relationships easily in ...2、Explore usages & references
TransientObjectException: object references an unsaved transient instance 这个异常通常发生在级联操作中,例如当配置了`cascade="save-update,persist"`。如果在保存一个对象时,该对象引用了一个未保存的...
pelican_references-0.2.3-py3-none-any.whl是一个Python的轮子文件(wheel file),这种文件格式是Python的二进制包分发格式,可以方便地安装到Python环境中,无需编译源代码。"py3-none-any"表示该轮子文件适用于...
PHYSICAL-DEFAULT-VALUE shall only be used, if INPUT-PARAM references a DATA-OBJECT-PROP. Output parameters are returned by the job as physical values. The physical values returned by the job shall ...
`org.hibernate.TransientObjectException`: object references an unsaved transient instance** - **异常描述**:当尝试保存一个对象时,该对象引用了一个尚未被持久化的对象。 - **原因分析**:通常发生在尝试...
org.hibernate.TransientObjectException: object references an unsaved transient instance 当一个实体引用了另一个尚未保存的实体时,会抛出此类异常。这通常发生在级联保存的场景中,如果父实体尝试保存时,子...
This article describes an introductory object-oriented finite element program for static and dynamic nonlinear applications. This work can be considered as an extension of the original FEM_Object ...
资源来自pypi官网。 资源全名:pelican_references-0.2.3-py3-none-any.whl