- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Spring Boot 并希望提高性能。我必须在数据库中下载 50000 字段的文件。使用 hibernate 。我在 Batch inserts 中找到了解决方案.但我不知道如何从 crudrepository 获取 entitymanager
public interface MyRepository extends CrudRepository<OTA, Long>
application.yaml
jdbc.batch_size: 50
order_inserts: true
order_updates: true
cache.use_second_level_cache: true
@Service @Repository @Transactional public class MyStorageService {
private MyRepository myRepository;
private void insertAll(final List<MyFile> file) {
myRepository.save(file.getListLine());
}
private void insert(final List<OTA> ota) {
Session session = (Session) entityManager.getDelegate();
Transaction tx = session.beginTransaction();
for (int i = 0; i < ota.size(); i++) {
session.save(ota.get(i));
if (i % 50 == 0) {
session.flush();
session.clear();
}
}
tx.commit();
session.close();
}
@PersistenceContext
EntityManager entityManager;
ERROR [http-nio-18842-exec-1] JpaTransactionManager: Commit exception overridden by rollback exception java.lang.IllegalStateException: Transaction not active at org.hibernate.jpa.internal.TransactionImpl.getRollbackOnly(TransactionImpl.java:131) at org.springframework.orm.jpa.JpaTransactionManager$JpaTransactionObject.isRollbackOnly(JpaTransactionManager.java:665)
ERROR [http-nio-18842-exec-1] JpaTransactionManager: Commit exception overridden by rollback exception java.lang.IllegalStateException: Transaction not active at org.hibernate.jpa.internal.TransactionImpl.getRollbackOnly(TransactionImpl.java:131) at org.springframework.orm.jpa.JpaTransactionManager$JpaTransactionObject.isRollbackOnly(JpaTransactionManager.java:665)
ERROR [http-nio-18842-exec-5] JpaTransactionManager: Commit exception overridden by rollback exception java.lang.IllegalStateException: Transaction not active at org.hibernate.jpa.internal.TransactionImpl.getRollbackOnly(TransactionImpl.java:131) at org.springframework.orm.jpa.JpaTransactionManager$JpaTransactionObject.isRollbackOnly(JpaTransactionManager.java:665)
java.lang.IllegalStateException: EntityManager is closed at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:97) at org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:88) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.clear(AbstractEntityManagerImpl.java:1382) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347) at com.sun.proxy.$Proxy91.clear(Unknown Source) at org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:554)
save
来自 crud 存储库,但想要提高性能并使用插入。请告诉我如何从 crudrepository 获取 entitymanager。
最佳答案
我不确定您的 Spring 配置是如何设置的。希望这可能会有所帮助,给你:
如果你已经设置了org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
的bean在您的上下文 xml 或 @Configuration java 文件中,在这种情况下,您可以通过以下方式获取实体管理器。
假设您按如下方式声明了上述内容:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceRefToBeMentioned"/>
<property name="packagesToScan" value="package.to.be.mentioned"/>
<property name="jpaVendorAdapter" ref="hibernateJPAVendorAdapter"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
@Autowired
LocalContainerEntityManagerFactoryBean entityManagerFactory;
EntityManager entityManager = entityManagerFactory.getObject().createEntityManager();
Session session = (Session) entityManager.getDelegate();
Transaction tx = session.beginTransaction();
....
...
tx.commit();
session.close();
entityManager.close();
关于hibernate - 我如何从 crudrepository 获取 entitymanager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44331164/
我是一名优秀的程序员,十分优秀!