JpaRepository save method does not commit using Derby DB
<p>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.</p>

<p>ApplicationContext.xml</p>

<pre><code>&lt;context:component-scan base-package="com.oracle.blog" /&gt;
 &lt;jpa:repositories base-package="com.oracle.blog.repo" /&gt;
 &lt;tx:annotation-driven/&gt;

 &lt;bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
    &lt;property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" /&gt;
    &lt;property name="url"
        value="jdbc:derby://localhost:1527/MyDerby;create=true" /&gt;
    &lt;property name="username" value="root" /&gt;
    &lt;property name="password" value="root" /&gt;
 &lt;/bean&gt;

 &lt;bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
    &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
    &lt;property name="packagesToScan" value="com.oracle.blog"/&gt;
    &lt;property name="jpaVendorAdapter"&gt;
        &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
            &lt;property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" /&gt;
        &lt;/bean&gt;
    &lt;/property&gt;
    &lt;property name="jpaProperties"&gt;
        &lt;props&gt;
            &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.DerbyDialect&lt;/prop&gt;
            &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
            &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
            &lt;prop key="hibernate.use_sql_comments"&gt;false&lt;/prop&gt;
            &lt;!-- &lt;prop key="hbm2ddl.auto"&gt;update&lt;/prop&gt; --&gt;
        &lt;/props&gt;
    &lt;/property&gt;

 &lt;/bean&gt;
 &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
    &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
 &lt;/bean&gt;

&lt;bean id="authorDAO" class="com.oracle.blog.repo.AuthorDAO"&gt;
&lt;/bean&gt;
</code></pre>

<p>AuthorDAO.java</p>

<pre><code>@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;
    }
}
</code></pre>

<p>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 <code>@Commit</code> (at class level), <code>@Transactional</code> (at main method level)  </p>

<p><strong>UPDATE</strong>
I am getting a bean instance of AuthorDAO in main via application context:</p>

<pre><code>AuthorDAO authDAO = (AuthorDAO)context.getBean("authorDAO");
        //authDAO.save(authObj);
        System.out.println(authDAO.save(authObj).getAuthorId());
</code></pre>

<p>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.</p>

<pre><code>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}
</code></pre>

<hr>

<p><strong>Solved</strong>
Make sure we mention correct table name and schema name in <code>@Table</code> annotation while listing Java Bean as JPA Entities
Find more details of the correct implementation <a href="http://lebanesemushroom.blogspot.in/2016/11/debugging-java-with-springjpa-api-and.html" rel="nofollow noreferrer">here</a></p>