I need some help resolving a Hibernate TransientPropertyValueException
We have two entities:
@Entity
@Table(name = "TABLE_A")
public class TableA {
@Id
@Column(name = "EXT_ID", nullable = false, unique = true, updatable = false)
private String extId;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EXT_ID", updatable = false, insertable = false)
private TableB tableB;
(...)
}
@Entity
@Table(name = "TABLE_B")
public class TableB {
@Id
@Column(name = "EXT_ID", nullable = false)
private String extId;
@OneToOne
@JoinColumn(name = "EXT_ID")
private TableA tableA;
}
Now we delete a TableA
using a CrudRepository
:
@Override
@Transactional
public boolean cancel(String extId) {
Optional<TableA> maybeTableA = repository.findTableA(extId, DELETE_STATUS_SET);
return maybeTableA.map(tableA -> {
repository.delete(tableA);
return true;
}).orElse(false);
}
So far everything works fine. But now whenever I want to query for the TableB
which had a reference to the TableA
I am getting a TransientPropertyValueException
It is obvious, that the reference from TableB
to TableA
has not been deleted before commiting the transaction.
Is there a nice way to resolve this problem?
I have tried sessionFactory.getCurrentSession().flush()
directly after the delete but it also does not work
The only alternative that worked was manually deleting the reference within the transaction:
tableB.setTableA(null);
tableBRepository.save(tableB);
But this is a little bit hacky imo