java - Spring Data JPA save new Entity and get its all dependencies -
im saving entity database via spring data jpa save() method. method returns saved entity object, if im saving new entity, returned object isn't filled example @manytomany full entities.
as far know, when spring data jpa save new entity, uses entitymanager.persist() method, , if entity exist in database, save() method uses entitymanager.merge() method.
my question is, how save new entity in database , full dependencies in 1 transaction?
i trying add helper class, bean services extends, like:
public abstract class helperpersistenceservice<e extends abstractentity<i>, extends serializable> { @persistencecontext private entitymanager entitymanager; @transactional public e savewithmerge(e e) { if (e.getid() == null) { entitymanager.persist(e); } return entitymanager.merge(e); } }
and works without @transactional method. got excepton:
javax.persistence.transactionrequiredexception: no entitymanager actual transaction available current thread - cannot reliably process 'persist' call @ org.springframework.orm.jpa.sharedentitymanagercreator$sharedentitymanagerinvocationhandler.invoke(sharedentitymanagercreator.java:278) @ com.sun.proxy.$proxy778.persist(unknown source)
how solve problem?
have added cascade=persist,merge on many many relationship?
the behaviour you're after requires cascade present.
Comments
Post a Comment