GenericApplicationContext + @Transactional -gt; lazy-init exception
pany.Beanlt;/valuegt;lt;/listgt; lt;/propertygt; lt;property name=quot;hibernatePropertiesquot;gt;lt;propsgt; lt;prop key=quot;hibernate.dialectquot;gt;${ar.hibernate.dialect}lt;/propgt; lt;prop key=quot;hibernate.connection.shutdownquot;gt;truelt;/propgt; lt;prop key=quot;hibernate.jdbc.batch_sizequot;gt;0lt;/propgt; lt;prop key=quot;hibernate.show_sqlquot;gt;${ar.show_sql}lt;/propgt; lt;prop key=quot;hibernate.hbm2ddl.autoquot;gt;updatelt;/propgt;lt;/propsgt; lt;/propertygt; lt;/beangt;
lt;bean id=quot;arDataSourcequot; class=quot;org..jdbc.datasource.DriverManagerDataSourcequot;gt; lt;property name=quot;driverClassNamequot; value=quot;${ar.database.driver}quot; /gt; lt;property name=quot;uclquot; value=quot;${ar.database.ucl}quot; /gt; lt;property name=quot;usernamequot; value=quot;${ar.database.username}quot; /gt; lt;property name=quot;passwordquot; value=quot;${ar.database.password}quot; /gt; lt;/beangt;
lt;/beansgt;
I have an entity bean containing an id and a set references to other beans of the same class:
Code:
@Entity
@Table(name=quot;beansquot;)
public class Bean { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;
@OneToMany(cascade = CascadeType.ALL) @JoinTable( name = quot;fromidquot;, joinColumns = @JoinColumn(name = quot;toidquot;), inverseJoinColumns = @JoinColumn(name = quot;fromidquot;) ) private Setlt;Beangt; beans; // getters and setters
}
A class to interface the database using hibernate template
Code:
public class StateDB { private HibernateTemplate template;
public void setHibernateTemplate(HibernateTemplate htpl) { this.template = htpl; }
public void store(Bean entry) { template.saveOrUpdate(entry); }
public Bean retrieve(String id) { template.find(quot;from Bean where ....quot;).get(0); }
... and a class which manages the above stateDB
Code:
public class StateDBManager { private StateDB stateDB;
public StateDBManager() { stateDB = new StateDB(); stateDB.setHibernateTemplate( SpringContext.getBean(quot;arHibernateTemplatequot;) ); }
@Transactional public void addBean(String id, Bean newBean) { Bean bean = stateDB.retrieve(id); bean.add(newBean); // lt;--------------- throws lazy init exception stateDB.store(bean); }
}I'll greatly appreciate any hints to how I can continue debugging this problem, and why it happens despite using the @Transactional annotations.
Use the forum search and read the reference guide.
@Transactional ONLY works on spring managed beans from the piece of code I suspect your StateDBManager isn't a spring managed bean. The same goes for your StateDB.
Another thing I suspect is that you are reloading and reloading and reloading and .... your applicationcontext to retrieve beans (why the ugly call to the SpringContext.getBean that code makes me shudder, use dependency injection).
Thanks for your quick reply!
@Transactional ONLY works on spring managed beans from the piece of code I suspect your StateDBManager isn't a spring managed bean. The same goes for your StateDB.
You're absolutely right. I've probably read that sentence already, but it didn't stick. Another thing I suspect is that you are reloading and reloading and reloading and .... your applicationcontext to retrieve beans (why the ugly call to the SpringContext.getBean that code makes me shudder, use dependency injection).
I couldn't agree more. Unfortunately our application must live within another application which isn't spring aware. |