问题
I have gone through numerous posts, questions listing the additional steps to ensure that save method from JpaRepository commits the record in the Database. The usage of a proxy over the transactionManager/using Transactional with REQUIRES_NEW as propagation etc. But still I am not able to get the exact resolution for my use case. It seems a basic use-case, but something is not right here.
ApplicationContext.xml
<context:component-scan base-package="com.oracle.blog" />
<jpa:repositories base-package="com.oracle.blog.repo" />
<tx:annotation-driven/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url"
value="jdbc:derby://localhost:1527/MyDerby;create=true" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="com.oracle.blog"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<!-- <prop key="hbm2ddl.auto">update</prop> -->
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="authorDAO" class="com.oracle.blog.repo.AuthorDAO">
</bean>
AuthorDAO.java
@Transactional
public class AuthorDAO {
@PersistenceContext
private EntityManager em;
@Autowired
private AuthorRepository authorRep;
@Transactional(propagation=Propagation.REQUIRES_NEW)
public Author save(Author auth){
//Author persistedAuth= authorRep.save(auth);
Author persistedAuth = authorRep.saveAndFlush(auth);
//authorRep.flush();
return persistedAuth;
}
}
I am calling it from main, by calling the save method from an instance of AuthorDAO.
Please point out to any annotations which I can use in the main method/class.
I tried using @Commit
(at class level), @Transactional
(at main method level)
UPDATE I am getting a bean instance of AuthorDAO in main via application context:
AuthorDAO authDAO = (AuthorDAO)context.getBean("authorDAO");
//authDAO.save(authObj);
System.out.println(authDAO.save(authObj).getAuthorId());
Find the EntityManager logs below. There is nothing wrong in the logs. Infact, it is properly listing the entity to be merged. The save method returns the entity fine. Just that it is not getting committed to the DB.
INFO: Initialized JPA EntityManagerFactory for persistence unit 'default'
23:54:39.012 [main] DEBUG o.h.s.internal.StatisticsInitiator - Statistics initialized [enabled=false]
Author003 null
23:54:39.355 [main] DEBUG o.h.e.t.internal.TransactionImpl - begin
23:54:39.377 [main] DEBUG o.s.d.r.c.s.TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource - Adding transactional method 'saveAndFlush' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
23:54:39.405 [main] DEBUG org.hibernate.engine.spi.ActionQueue - Executing identity-insert immediately
23:54:39.408 [main] DEBUG org.hibernate.SQL - insert into Author (AUTHOR_ID, bio, email, name, PROFILE_LINK) values (default, ?, ?, ?, ?)
Hibernate: insert into Author (AUTHOR_ID, bio, email, name, PROFILE_LINK) values (default, ?, ?, ?, ?)
23:54:39.418 [main] DEBUG org.hibernate.SQL - values identity_val_local()
Hibernate: values identity_val_local()
23:54:39.422 [main] DEBUG o.h.id.IdentifierGeneratorHelper - Natively generated identity: 1
23:54:39.423 [main] DEBUG o.h.r.j.i.ResourceRegistryStandardImpl - HHH000387: ResultSet's statement was not registered
23:54:39.427 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Processing flush-time cascades
23:54:39.428 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Dirty checking collections
23:54:39.430 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
23:54:39.430 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
23:54:39.431 [main] DEBUG o.h.internal.util.EntityPrinter - Listing entities:
23:54:39.431 [main] DEBUG o.h.internal.util.EntityPrinter - com.oracle.blog.src.Author{profileLink=http://changesagnblog.blogspot.in, name=Author003, bio=My third blog to be published on blogspot., authorId=1, email=yetagnakashdotm@gmail.com}
23:54:39.433 [main] DEBUG o.h.e.t.internal.TransactionImpl - committing
23:54:39.433 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Processing flush-time cascades
23:54:39.433 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Dirty checking collections
23:54:39.433 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
23:54:39.433 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
23:54:39.433 [main] DEBUG o.h.internal.util.EntityPrinter - Listing entities:
23:54:39.433 [main] DEBUG o.h.internal.util.EntityPrinter - com.oracle.blog.src.Author{profileLink=http://changesagnblog.blogspot.in, name=Author003, bio=My third blog to be published on blogspot., authorId=1, email=yetagnakashdotm@gmail.com}
Solved
Make sure we mention correct table name and schema name in @Table
annotation while listing Java Bean as JPA Entities
Find more details of the correct implementation here
来源:https://stackoverflow.com/questions/39679766/jparepository-save-method-does-not-commit-using-derby-db