-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Need many examples
PostPosted: Mon Apr 12, 2004 3:31 pm 
Beginner
Beginner

Joined: Tue Mar 16, 2004 5:15 am
Posts: 33
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 :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 2:22 am 
Beginner
Beginner

Joined: Tue Mar 16, 2004 5:15 am
Posts: 33
Well found the solution. XDoclet does not support access=field operator. So I thought it was something like missbehaviour or unskillfull use of hibernate :-).

So now it works. I have blinked out the get/setCategory property (makin it private) and moved the logic of the former get/setCategory method to a set/getParent pair of methods. Now everything is working as expected. :-)

But if someone has a good and handy large scale example, I would be glad to see it.


Thanks,

Martin (Kersten)

PS: Corrected version:

/**
* @hibernate.many-to-one cascade="save-update" column="CATEGORY_FK"
*/
private Category getCategory() {
return category;
}
private void setCategory(Category category) {
this.category=category;
}

public Category getParent() {
return getCategory();
}
public void setParent(Category category) {
Category oldParent=getCategory();
if(oldParent!=null)
oldParent.getCategories().remove(this);
if(category!=null)
category.getCategories().add(this);
setCategory(category);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 8:17 pm 
Newbie

Joined: Wed Sep 29, 2004 8:06 pm
Posts: 4
I am attempting to implement a similar tree (self-reference).
The code above is the only documentation I have been able to find at all which amazes me.

Anyway it seems to work fine except when I am trying to update a category and its parnet at the same time.

I have a session wrapped around the following code.

public void saveObject(PDObjectIF objectIf, int parentId)
{
Category parentObj = categoryDAO.loadCategory(parentId);
******parentObj.getCategories().add( (Category)objectIf ); ******
storeCategory( parentObj );
}

I get an error on the ***** line of code above.

net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed

I have spring wrapping a transaction arround all store* methods so I am not sure how there can be no session.

Any help is greatly appreciated?

-JK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 9:49 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Please don't hijack threads, we don't know what your real problem is. Post a new entry and fill out the questions that are predefined. Make sure you also check CaveatEmptor, the example application first.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.