我发现实际上是Spring,我可以设置一些工作.现在,我想使用Hibernate/JPA将我导入的数据保存在数据库中,并且我不断收到此错误:
14:46:43.500 [main] ERROR o.s.b.core.step.AbstractStep - Encountered an error executing the step javax.persistence.TransactionRequiredException: no transaction is in progress
Run Code Online (Sandbox Code Playgroud)
我看到问题出在交易上.这是我的spring java配置entityManager和transactionManager:
@Configuration
public class PersistenceSpringConfig implements EnvironmentAware
{
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception
{
// Initializes the entity manager
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factoryBean.setDataSource(dataSource());
// Scans the database model
factoryBean.setPackagesToScan(EntiteJuridiqueJPA.class.getPackage().getName());
// Defines the Hibernate properties
Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.show_sql", "false");
jpaProperties.setProperty("hibernate.format_sql", "false");
String connectionURL = "jdbc:h2:file:" + getDatabaseLocation();
jpaProperties.setProperty("hibernate.connection.url", connectionURL); …Run Code Online (Sandbox Code Playgroud) 当我尝试在 spring 批处理中使用我的作业处理器将我从 csv 文件恢复的一些数据保留在数据库中时,此错误出现在控制台中,对于我的 dao,我正在使用 hibernate
我已经尝试了 2 种方法但同样的问题!
第一的 :
Session session = factory.getCurrentSession();
session.saveOrUpdate(p);
Run Code Online (Sandbox Code Playgroud)
第二 :
Session session = factory.openSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
session.close();
Run Code Online (Sandbox Code Playgroud)
我的 spring xml 配置中的数据源:我所有的 spring xml 配置都在这里https://pastebin.com/RZPr1GKL
<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yassir" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>tp.entities.Personne</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean> …Run Code Online (Sandbox Code Playgroud)