What does "org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation" mean?
Issue
- Following exception is logged when trying to persist entity:
org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: model.Merchant.defaultPOS -> model.PointOfSale. What is the root cause of this exception and how can it be avoided?
The code:
Merchant:
...
@OneToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "defaultPOS_id")
private PointOfSale defaultPOS;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "merchant")
private List<PointOfSale> POSList = new ArrayList<PointOfSale>();
...
and
PointOfSale:
...
@ManyToOne(cascade = CascadeType.ALL, optional=false)
@JoinColumn(name = "merchant_id")
@ForeignKey(name = "FK_POS_TO_MERCHANT")
private Merchant merchant;
Test:
@RunWith(Arquillian.class)
public class MerchantPersistenceTestCase {
@PersistenceContext
EntityManager em;
@Inject
UserTransaction utx;
@Before
public void preparePersistenceTest() throws Exception {
utx.begin();
em.joinTransaction();
}
@After
public void commitTransaction() throws Exception {
utx.commit();
}
@Test
@TargetsContainer("maximilien:DigicashPayment")
public void create_merchant() {
PointOfSale pos=new PointOfSale();
Merchant merchant = new Merchant();
pos.setMerchant(merchant);
merchant.setDefaultPOS(pos);
em.persist(merchant);
}
}
Environment
- Red Hat JBoss Enterprise Application Platform(EAP)
- 6.x
- Hibernate
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
