我有以下代码:
HashSet hashSet = new HashSet();
hashSet.addAll(partnerRepository.findByVatNumber(partner));
hashSet.addAll(etc...);
我收到以下异常:
org.springframework.dao.InvalidDataAccessApiUsageException:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: enone.domain.PartnerEntity; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: domain.PartnerEntity
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:157)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:111)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy75.findByVatNumberExcludingPartner(Unknown Source)
我怀疑它可能需要对延迟加载做些什么?虽然它有点奇怪,因为我没有调用保存方法,只是将结果添加到哈希集。
说明
答案 0 :(得分:0)
事实证明,问题根本与实际的HashSet无关,而是与存储库有关。
我没有比较合作伙伴的ID,而是将合作伙伴与合作伙伴进行比较。 partnerEntity虽然具有懒惰加载的字段,另外还有来自另一个会话/查询的字段。
我取而代之:
@Query("select p from PartnerEntity p where p.country = ?1 AND NOT p.id = ?2")
List<PartnerEntity> findByCountryNameExcludingPartner(CountryEntity country, PartnerEntity partnerEntity);
有了这个:
@Query("select p from PartnerEntity p where p.country = ?1 AND NOT p.id = ?2")
List<PartnerEntity> findByCountryNameExcludingPartner(CountryEntity country, Integer partnerEntity);
现在它正在发挥作用。我也在通话中发送partnerEntity.getId()
而不是整个实体。