我已经阅读了很多帖子,列出了其他步骤,以确保JpaRepository中的save方法提交数据库中的记录。在transactionManager上使用代理/使用Transactional和REQUIRES_NEW作为传播等。但是我仍然无法获得我的用例的确切解决方案。这似乎是一个基本的用例,但有些东西不在这里。
的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;
}
}
我通过从AuthorDAO实例调用save方法从main调用它。
请指出我可以在main方法/类中使用的任何注释。
我尝试使用@Commit
(在班级),@Transactional
(在主要方法级别)
更新 我通过应用程序上下文获取了AuthorDAO的bean实例:
AuthorDAO authDAO = (AuthorDAO)context.getBean("authorDAO");
//authDAO.save(authObj);
System.out.println(authDAO.save(authObj).getAuthorId());
在下面找到EntityManager日志。日志中没有任何问题。事实上,它正确地列出了要合并的实体。 save方法返回实体罚款。只是它没有被提交给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}
解决
确保在将Java Bean列为JPA实体时,在@Table
注释中提及正确的表名和模式名称
详细了解正确实施here