I am having a strange issue while inserting a new entry.
Problem:
entityManager.persist(myEntiy)
: line is executed but no entry is created in the db.
I know many had this issue before and i have read all of those posts but none of it answers my problem. I am not inserting an entry for the first time. So i have did the configuration and other basic things right. That means I do use <tx:annotation-driven transaction-manager="jpaTransactionManager"/>
in my spring-core.xml and I do use an @Transactional
on top of the methods in my dao.
Solutions tried: Adding entityManager.flush()
after persist, but this resulted in
javax.persistence.TransactionRequiredException: no transaction is in progress.
Also tried:
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
which resulted in
Not allowed to create transaction on shared EntityManager
Then saw someone saying the problem may be the
AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
but i didnt have anything in log saying that this may be the cause. And i dont have a clue what exactly is the solution if that is the issue. My hibernate log ends with the lines.
DEBUG 0412185908 [80-exec-2 SqlStatementLogger.java: 104] select mySchema.sequence_for_my_entity_PK.nextval from dual
DEBUG 0412185908 [80-exec-2 SequenceGenerator.java: 127] Sequence identifier generated: BasicHolder[java.lang.Long[20]]
DEBUG 0412185908 [80-exec-2 AbstractSaveEventListener.java: 131] Generated identifier: 20, using strategy: org.hibernate.id.SequenceHiLoGenerator
DEBUG 0412185908 [80-exec-2 LogicalConnectionImpl.java: 314] Releasing JDBC connection
DEBUG 0412185909 [80-exec-2 LogicalConnectionImpl.java: 332] Released JDBC connection
DEBUG 0412185909 [80-exec-2 ConnectionProxyHandler.java: 219] HHH000163: Logical connection releasing its physical connection
no errors whatsoever there. Any help is appreciated.
My spring-core.xml have:
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref= "jpaVendorAdapter"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="punit" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<tx:annotation-driven transaction-manager="jpaTransactionManager"/>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${local.datasource.database.platform}" />
<property name="generateDdl" value="true" />
<property name="showSql" value="true" />
</bean>
<bean id="dataSource" class="${local.datasource.database.datasource}">
<property name="dataSourceName" value="${local.datasource.database.name}" />
<property name="URL" value="${local.datasource.database.url}" />
<property name="user" value="${local.datasource.database.user}" />
<property name="password" value="${local.datasource.database.password}" />
</bean>
And the DaoImpl is:
@Repository
public class MyDaoImpl implements MyDao {
protected EntityManager entityManager;
@Autowired
AnotherDao anotherDao;
public EntityManager getEntityManager() {
return entityManager;
}
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
@Transactional
public String saveNewInfo(Data dataObject, String scheduleType){
String uniqueSessionId = null;
History history = new History();
SessionIdentifierGenerator sessionIdentifierGenerator = new SessionIdentifierGenerator();
uniqueSessionId = sessionIdentifierGenerator.nextSessionId();
Type type = anotherDao
.getAppObject(dataObject.getApplication());
history.setType(type.getType());
history.setSessionId(uniqueSessionId);
history.setSchedulerType(scheduleType);
history.setStatusCode("1");
history.setVersionNumber(dataObject.getVersionNumber());
entityManager.persist(history);
return uniqueSessionId;
}
}
So the problem is solved. I added these two lines to my Spring-Core.xml
<bean id="myDao" class="com.myPackage.dao.myDaoImpl"/>
<bean id="myAnotherDao" class="com.myPackage.dao.myAnotherDaoImpl" />
I never used to specify by dao beans like this before in the spring-core and still it used to work for update ,delete and even for persisting a new entry(Everybody is welcome to clarify this part).But this is the solution that fixed my issue.