`

org.hibernate.PropertyValueException: not-null property references a null or tra

阅读更多
org.hibernate.PropertyValueException: not-null property references a null or transient value

at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:103)
at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:132)
at org.hibernate.action.internal.AbstractEntityInsertAction.makeEntityManaged(AbstractEntityInsertAction.java:141)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:220)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:142)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:329)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:135)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:735)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:727)
at org.hibernate.engine.spi.CascadingAction$5.cascade(CascadingAction.java:258)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:388)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:331)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209)
at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:418)
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:358)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:334)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:166)
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:449)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:292)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:135)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
at com.lj.zhang.Student.HibernateTeam_1insert.main(HibernateTeam_1insert.java:36)



	Session session=HibernateUtil.openSession();
		
		Team team=new Team(null,"impl_team",null);
		
		 
		
		
		Student s1=new Student("alleni", null);
		Student s2=new Student("eline",null);
		
		IdCard card1=new IdCard(001, s1);
		
		IdCard card2=new IdCard(002,s2);
		
	 	
		team.setStudents(ArraysHelper.asSet(s1,s2));
		
		
		Transaction tx=session.beginTransaction();
		
		session.save(team);
		
		tx.commit();


package org.hibernate.engine.internal
public final class Nullability

/** Check nullability of the class persister properties
/* @param values entity properties
public void checkNullability {
...
//这里会对所有transient状态的对象进行检查,没有异常则进入下一步,最终持久化。
for ( int i = 0; i < values.length; i++ ) {

				if ( checkability[i] && values[i]!= LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
					final Object value = values[i];
					if ( !nullability[i] && value == null ) {

						//check basic level one nullablilty
						throw new PropertyValueException(
								"not-null property references a null or transient value",
								persister.getEntityName(),
								persister.getPropertyNames()[i]
							);

					}
					else if ( value != null ) {

						//values is not null and is checkable, we'll look deeper
						String breakProperties = checkSubElementsNullability( propertyTypes[i], value );
						if ( breakProperties != null ) {
							throw new PropertyValueException(
								"not-null property references a null or transient value",
								persister.getEntityName(),
								buildPropertyPath( persister.getPropertyNames()[i], breakProperties )
							);
						}

					}
				}
...
			}




出现这个问题的原因是没有把Student应有的IdCard加进去。
Hibernate会从我们执行session.save()中的对象开始,依次将关联的对象加进session缓存对象中。 比如这里的代码是save(team),于是会先加一个student,然后这个student对应的idcard,如此循环递归。

但是当代码运行到Student的循环时,根据映射配置文件, 该student应该对应一个idcard,且不能为null,于是就报错了。

如下图所示,当加入了代码:
s1.setIdCard(card1);
	 	s2.setIdCard(card2);


values[1]那里便有了该有的idcard对象地址。
之前那里为null,抛出异常。


  • 大小: 117.8 KB
分享到:
评论

相关推荐

    解决SpringDataJPA报错:org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null w

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/...

    Dart.Snmp.Dll Dart.Snmp.45.dll v4.9.4.9.0.1 Released: 2016-05-10 Fixed

    Then add/update references to Dart.Snmp.Dll in your project. No limits, no nagging messages. Enjoy. And if you like the product, then buy it. Support the software companies and developers!

    C#程序单词注释汇总

    - 表示数据库中的NULL值,在.NET框架中用于区分普通语言中的null。 18. **Profile**: - 配置文件。 - 通常包含特定于用户或应用程序的设置。 19. **Constraint**: - 约束。 - 数据库中用于确保数据完整性的...

    Hibernate映射导致的几个异常

    Not-null property references a null or transient value 当Hibernate尝试保存一个实体时,如果该实体中的某个被标记为`@NotNull`或在映射文件中定义为`not-null="true"`的属性值为null或者指向一个尚未持久化的...

    Hibernate,hibernate笔记

    在描述中提到的`org.hibernate.PropertyValueException: not-null property references a null or transient value`是Hibernate抛出的一个常见异常,表示尝试保存一个实体时,其某个非空属性为null或未持久化。...

    10_传智播客JPA详解_JPA中的一对多双向关联与级联操作

    在Java世界中,Java Persistence API(JPA)是Oracle公司推出的用于对象关系映射(ORM)的标准框架,它简化了数据库操作,使开发者能够用Java对象来操作数据库记录。本教程将深入探讨JPA中的一对多双向关联及级联...

    JLink_Windows_V648.zip

    Version V6.48 (2019-07-26) Added flash programming support for AmbiqMicro's AMA2B1KK ...Firmware: J-Link EDU Mini: RISC-V: On implementations that do not populate a "program buffer" CSRs could not ...

    JPA 开发中遇到的错误

    org.hibernate.PropertyValueException: not-null property references a null or transient value 当你尝试保存一个实体时,如果该实体中某个被标记为`@NotNull`或`@Column(nullable = false)`的属性值为null,则...

    10.1038_d41586-022-00985-9-references.ris (1)

    10.1038_d41586-022-00985-9-references.ris (1)

    10.1007_s11704-022-1389-x-references (2).ris

    10.1007_s11704-022-1389-x-references (2).ris

    编译好的x265,带y4m文件

    --level-idc |float&gt; Force a minumum required decoder level (as '5.0' or '50') --[no-]high-tier If a decoder level is specified, this modifier selects High tier of that level Threading, performance:...

    DB2数据库错误代码

    - **说明**:表示尝试为允许NULL的列设置NOT NULL约束。 45. **+65001538** - **SQLSTATE**: 538 - **说明**:表示索引的锁状态被非法更改。 46. **+65301551** - **SQLSTATE**: 551 - **说明**:表示指定...

    hibernate annotations

    **标题:“Hibernate Annotations”** **概述** Hibernate是一个流行的Java对象关系映射(ORM)框架,它简化了在Java应用程序中处理数据库的过程。Hibernate Annotations是Hibernate框架的一个重要组成部分,它...

    Lewis, J. M., & Looney, J. G. (1988). The long struggle: Well-functioning workingclass Black families. New York: Brunner/Mazel, 193 pp., no price listed

    REFERENCES GOUGH, H., (1975). Manual for the California Psychological Inventory. Palo Alto, CA: Consulting GOUGH, H. (1987). Administrator’s guidefor the California Psychological Inventory. Palo ...

    hibernate常见错误解决方案

    **PropertyValueException: not-null property references a null or transient value** **异常描述:** 此异常表明一个标记为不可为空的属性引用了一个null或瞬态值。 **解决方法:** - 确认所有标有`not-null`...

    Johnson转换系统,非高斯转换高斯,高斯转换非高斯

    % -----References:----- % Hill, I. D. 1976. Algorithm AS 100: Normal-Johnson and Johnson-Normal % Transformations. Journal of the Royal Statistical Society. Series C (Applied % Statistics) 25(2): 190-...

    db2 错误码 大全

    - **描述**: 在尝试向不允许NULL值的列插入NULL值时出现问题。 ##### 45. 锁定失败 - **SQLCODE**: +65001 - **SQLSTATE**: 538 - **描述**: 尝试锁定某行数据时失败。 ##### 46. 缓冲池问题 - **SQLCODE**: +...

    Senfore_DragDrop_v4.1

    E.g.: if (DragDetectPlus(THandle(MyControl-&gt;Handle), Point(X, Y))) { ... } * Virtual File Stream formats can only be pasted from the clipboard with live data (i.e. FlushClipboard/...

    Machine Learning: Step-by-Step Guide To Implement Algorithms with Python

    Benefits of reading this book that you’re not going to find anywhere else: Introduction to Machine Learning Classification How to train a Model Different Models Combinations CHAPTER1 INTRODUCTION TO ...

    cmake-3.18.4-Linux-x86_64.tar.gz

    CMake is a cross-platform, open-source build system generator. For full documentation visit the CMake Home Page and the CMake Documentation Page. The CMake Community Wiki also references useful guides...

Global site tag (gtag.js) - Google Analytics