Java JpaRepository保存方法不使用Derby DB提交
我已经阅读了许多帖子,这些问题列出了确保来自JpaRepository的save方法在数据库中提交记录的附加步骤。在transactionManager上使用代理/使用Transactional with需要_NEW作为传播等,但我仍然无法获得我的用例的确切解决方案。这似乎是一个基本的用例,但这里有些不对劲 ApplicationContext.xmlJava JpaRepository保存方法不使用Derby DB提交,java,spring,jpa,repository,spring-data-jpa,Java,Spring,Jpa,Repository,Spring Data Jpa,我已经阅读了许多帖子,这些问题列出了确保来自JpaRepository的save方法在数据库中提交记录的附加步骤。在transactionManager上使用代理/使用Transactional with需要_NEW作为传播等,但我仍然无法获得我的用例的确切解决方案。这似乎是一个基本的用例,但这里有些不对劲 ApplicationContext.xml <context:component-scan base-package="com.oracle.blog" /> <jpa
<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>
我从main调用它,从AuthorDAO实例调用save方法。
请指出我可以在主方法/类中使用的任何注释。
我尝试使用@Commit
(在类级别上)、@Transactional
(在主方法级别上)
更新
我通过应用程序上下文在main中获得AuthorDAO的bean实例:
AuthorDAO authDAO = (AuthorDAO)context.getBean("authorDAO");
//authDAO.save(authObj);
System.out.println(authDAO.save(authObj).getAuthorId());
在下面找到EntityManager日志。日志中没有任何错误。事实上,它正确地列出了要合并的实体。save方法返回实体fine。只是它没有被提交给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}
已解决 确保在将JavaBean列为JPA实体时,在
@table
注释中提到了正确的表名和模式名
查找正确实现的更多详细信息是否关闭了数据库?是否可以包含所有代码?我假设您正在主类中创建一个新的AuthorDAO实例。我从main
AuthorDAO authDAO=(AuthorDAO)context.getBean(“AuthorDAO”)中的应用程序上下文获取AuthorDAO的bean实例//save(authObj);System.out.println(authDAO.save(authObj.getAuthorId())代码>你关闭数据库了吗?你能包括你所有的代码吗?我假设您正在主类中创建一个新的AuthorDAO实例。我从mainAuthorDAO authDAO=(AuthorDAO)context.getBean(“AuthorDAO”)中的应用程序上下文获取AuthorDAO的bean实例//save(authObj);System.out.println(authDAO.save(authObj.getAuthorId())代码>
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}