Hi there,
I am lost again using hibernate. I am reworking an web application dealing with many mappings. But I don't think I am doing the right stuff right now.
Since I've managed to get the extense and existing test suite working, I moved on to integrate the 'solution' into the webapp. But again things got blown up.
Currently hibernate throws this exceptions at me: net.sf.hibernate.AssertionFailure: cannot access loading collection.
Since some other people ran into the same issus I got some infos from the forum. I got a raw idea why this is happening. So I think I have to go back and again question my self what hibernate is all about.
I don't get it... . :-( I invested so many time in thinking about what hibernate is and what it is doing. I learned something from the test suite and the provided examples.
But I missed the important things. Here is what I am trying to do:
I have a bunch of categories and products. The categories are forming itself a tree (common stuff). And every category may have one to many products.
But here the trouble starts. I am trying to do things like:
category.getProducts() - returns all products
category.setParent(parentCategory) - to move it
category.addProduct(product) - adds this new product
looks common and logical. But I can not get things to work properly. Here is how I have implemented the get/set/add/removeCategory[s] methods:
/**
* @hibernate.set
* role="categories"
* lazy="true"
* cascade="all"
* inverse="true"
* @hibernate.collection-key
* column="CATEGORY_FK"
* @hibernate.collection-one-to-many class = "org.ocsn.domain.Category"
* */
public Set getCategories() {
return categories;
}
public void setCategories(Set categories) {
this.categories=categories;
}
public void addCategory(Category category) {
if(!getCategories().contains(category))
category.setCategory(this);
}
public void removeCategory(Category category) {
if(getCategories().contains(category))
category.setCategory(null);
}
/**
* @hibernate.many-to-one cascade="save-update" column="CATEGORY_FK"
*/
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
if(this.category!=null)
this.category.getCategories().remove(this);
if(category!=null)
category.getCategories().add(this);
this.category=category;
}
This works great running it within the test suite using a hsqldb in the acceptence tests. But it's like it is already stated in the forum. It breaks if it comes to lazy loading without creating the required items first (which is done during the test cases).
So how does the proper hibernate way look like? You know it breaks since setCategory is called and this calls the add and remove methods of its parent which is currently not in a loaded state. So I tend to understand the reason of breaking but I can not came up with a solution :(.
Any hints, examples, ideas or solutions would be great.
Are there any complex CRUD examples available?
Thanks,
Martin (Kersten)
PS: I still like hibernate but sometimes I start eating my keyboard :) So hibernate is like a rollercoaster :)
|