如何在外键列中插入值,引用是使用JPA在同一个表的主键上

时间:2016-04-07 18:22:28

标签: java mysql spring jpa

我需要在外键列中插入一个空值,但是我收到此错误: 有可能吗?

异常

  

org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.TransientPropertyValueException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.company.project.model.ConceptoGasto.conceptoGasto - > com.company.project.model.ConceptoGasto;嵌套异常是java.lang.IllegalStateException:org.hibernate.TransientPropertyValueException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.company.project.model.ConceptoGasto.conceptoGasto - > com.company.project.model.ConceptoGasto

实体

//----------------------------------------------------------------------
// ENTITY PRIMARY KEY ( BASED ON A SINGLE FIELD )
//----------------------------------------------------------------------
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable=false)
private Integer id;

....

@ManyToOne (optional = true)
@JoinColumn(name="idPadre", referencedColumnName="id", nullable=true)
private ConceptoGasto conceptoGasto;

.....

方式

JpaOperation operation = new JpaOperation() {
 @Override
 public Object execute(EntityManager em) throws PersistenceException {
   T managedEntity = em.merge(entityToSave);
   //em.refresh(managedEntity);
   return managedEntity;
 }
 } ;

0 个答案:

没有答案