0

I have a Spring app , many-to-many relationship between Order and Product with intermediary table ProductOrder.

If I save the ProductOrder directly to the db, it works fine, but I don't think that's the correct way, so I use the code below to add an Order to a Product by fetching and adding to the lazy collection.

I had a LazyInitializationException, so I tried to solve it with Hibernate initialize, but now I'm getting org.hibernate.HibernateException: collection is not associated with any session. I debugged and session=null for the PersistentCollection. I thought the @Transactional annotation is meant to begin a transaction and open a session, but it doesn't seem to work. I looked for some answers , and the cause is that the current session is not the same as when the object was created, but I don't know how to try solving the problem, please help me!

(I also have OpenEntityManagerInViewFilter in web.xml , but it doesn't seem to make any difference in solving this)

@Transactional
public void addClient(RequestContext context,ShoppingCart cart)
{

            ...
    List<Item> items = cart.getItems();
    Order order = new Order();
    order.setClient(client);
    order.setDate(new Date());

    order = orderService.save(order);

    for(Item item : items)
    {
        ProductOrder po = new ProductOrder();
        po.setOrder(order);
        po.setProduct(item.getProduct());

        Hibernate.initialize(item.getProduct().getProductOrders());

        item.getProduct().getProductOrders().add(po);
        productService.save(item.getProduct());

}

Additional info:

@Service("productService")
@Repository
@Transactional
public class ProductServiceImpl implements ProductService{

@Autowired
private ProductRepository productRepository;

@Override
public Product save(Product product) {

    return productRepository.save(product);
}
}
Natasha
  • 1,470
  • 5
  • 18
  • 24
  • Where do the items come from? Why do you have a `ProductOrder` entity? If it is only for the relationship you can do without it. You should be adding the `ProductOrder` to the Order and save the order. Why save the product your cascading rules should take care of that. – M. Deinum Jun 13 '14 at 13:23
  • It seems to be working if I add the ProductOrder to the Order and save the order, but why would this work but when adding to product wouldn't , after all the relationship is many-to-many, shouldn't I be able to add either to Order or to Product ? – Natasha Jun 13 '14 at 14:01
  • Either would work but you are working with the Order not the product. Also why do you have the ProductOrder even? If it is only 2 ids you don't need that class as hibernate is perfectly capable of managing the dependencies without. You don't need an object for each and every table in your database. – M. Deinum Jun 13 '14 at 15:29
  • The ProductOrder links Order and Product, but also has an extra column. And about working, I get the LazyInitializationExceptioon when adding to Product, but it seems to work when I use Order and I'm not sure why – Natasha Jun 13 '14 at 16:50

0 Answers0