Trying to write a simple SpringBatch job with a chunk based on Hibernate reading HibernateCursorItemReader and writing HibernateItemWriter and I get the following error :
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions. Collection : [Author.bookList#50]
<job xmlns="http://www.springframework.org/schema/batch"
id="rien" incrementer="simpleIncrementer">
<step id="chunk">
<tasklet>
<chunk reader="reader" processor="processor" writer="writer" commit-interval="10"/>
</tasklet>
</step>
</job>
<bean id="reader" class="org.springframework.batch.item.database.HibernateCursorItemReader">
<property name="useStatelessSession" value="false" />
<property name="sessionFactory" ref ="sessionFactory" />
<property name="queryString" value="from Author" />
</bean>
<bean id="processor" class="Processeur" />
<bean id="writer" class="org.springframework.batch.item.database.HibernateItemWriter">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<array>
<value>[OBJECT_PATH]</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.batch_versioned_data">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
@Entity
@Table(name = "book", schema = "mde")
public class Book implements Serializable
{
private static final long serialVersionUID = 6466106157952706315L;
@Id
@Column(name = "ID")
private Long id;
@Column(name = "VALUE")
private String value;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "author_id")
private Author author;
@Entity
@Table(name = "author", schema = "mde")
public class Author implements Serializable
{
private static final long serialVersionUID = -6248898317777915222L;
@Id
@Column(name = "ID")
private Long id;
...
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private List<Book> bookList;
public class Processeur implements ItemProcessor<Author, Author>
{
@Override
public Author process(Author author) throws Exception
{
for (Book item : author.getBookList())
{
item.setValue("99");
}
return author;
}
}
Removing cascade = CascadeType.ALL does not help at all