Announcement Announcement Module
Collapse
No announcement yet.
how to delete object though hibernate session in ItemProcessor<I, O> ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to delete object though hibernate session in ItemProcessor<I, O> ?

    hi

    my code intention is to delete the inactive user though batch job and log it.

    I use HibernateCursorItemReader as my ItemReader<I>, and I wanna delete my domain object in ItemProcessor<I, O>, the log it on ItemWriter<O>.

    However i had encounter an exception :

    Code:
    2009-05-12 23:46:01,062 ERROR [org.springframework.batch.core.step.AbstractStep] - <Encountered an error executing the step: class org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions>
    org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
    	at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:432)
    	at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:66)
    	at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:124)
    	at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:84)
    	at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:78)
    	at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:146)
    	at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:123)
    	at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
    	at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:793)
    	at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:771)
    	at batch.ItemProcessorImpl.process(ItemProcessorImpl.java:21)
    	at batch.ItemProcessorImpl.process(ItemProcessorImpl.java:1)
    here my code:

    Code:
    public class ItemProcessorImpl implements ItemProcessor<User, User>
    {
    	@Autowired
    	private SessionFactory sessionFactory;
    
    	public User process(User user) throws Exception {
    		
    		Session session = sessionFactory.getCurrentSession();
    		
    		if(!user.isActivate()) { 
    			session.delete(user);
                            return user;
                    } 
    				
    		return null;
    	}
    }

    I have some question:

    Is any transaction wrapper around the reader, processor and writer ? how to set it ?

    how to solve this exception, it state that i have use 2 open hibernate session. is that anyway to obtain the same session ?

    is this code suitable for using in batch job ?

    any idea ?


    happy hacking !
    -----------------
    kiwi
Working...
X