here is your problematic code:
Code:
List<Category> categories = categoryService.findByName("Autos", null);//here session is opened
// should only be one category in actuality
for (Category category : categories) {
System.out.println("category: "+category.getName());//here session is closed
Set<Category> subCategories = category.getCategories(); LAZY EXCEPTION HERE because youe session is closed
to solve this you must decouple your dao code from controller, because as I said before your method is not transactional
Code:
public ModelAndView onSubmit(Object command)
so your option is to do a dao method doing all this;
Code:
@Transactional
class DAO{
@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
public Set returnSet(){
List<Category> categories = categoryService.findByName("Autos", null);
for (Category category : categories) {
Set<Category> subCategories = category.getCategories();
return subCategories ;
}
}
with this method you will be sure your returnSet() transaction will overwrite inner transactions, and then in your Controller class you can call this method without exception.
finally your controller class would be like:
Code:
public class CategoryController extends SimpleFormController {
public ModelAndView onSubmit(Object command) throws ServletException {
CategoryBean categoryBean = (CategoryBean) command;
System.out.println("the name from the command bean is: "+categoryBean.getName());
ICategoryService categoryService = (ICategoryService)DataAccessServiceFactory.getBean("iCategoryService");
ITopicService topicService = (ITopicService)DataAccessServiceFactory.getBean("iTopicService");
Set<Category> subCategories = returnSet(); //CODE CHANGED
for (Category subCategory : subCategories) {
System.out.println("subCategory: "+subCategory.getName());
//Set<Topic> topics = subCategory.getTopics();
List<Topic> topics = topicService.findByCategoryID(subCategory.getCategoryId(), null);
System.out.println("****now to get topics if there are any!****");
for (Topic topic : topics) {
System.out.println("topic: "+topic.getTitle());
System.out.println("****now to get submissions if there are any!****");
Set<Submission> submissions = topic.getSubmissions();
for (Submission submission : submissions) {
System.out.println("submission topic: " +submission.getTopic());
System.out.println("submission title: " +submission.getTitle());
}
}
}
what do you think?