-->
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.  [ 8 posts ] 
Author Message
 Post subject: mapping hell
PostPosted: Thu Sep 22, 2005 6:13 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
I'm new to Hibernate and am having some very serious problems trying to do something which is *very* simple in basic SQL code - join two tables together in a one-to-many relationship.

I've got two objects w/ a "one-to-many" parent/child association, real simple stuff. I am trying to select the parent and automatically select the children - all of this is defined in the two mapping *.hbm.xml files of the two classes - all cobbled together from the Hibernate doucmentation and the examples found in Hibernate in action, which I purchased last week.

Anyhow, here are my two classes. AttributeCategory is the parent and Attribute is the child - an AttributeCategory can have many Attributes...which is defined as a List in the AttributeCategory class...see below:

Code:
package com.agribeef.bol;

/**
* @author vjenks
*
*/
public class Attribute implements IAttribute
{
   private int _id;
   private int _categoryID;
   private IAttributeCategory _category;
   private String _name;
   private String _contents;

   /**
    *
    */
   public Attribute()
   {
      // TODO Auto-generated constructor stub
   }

   public IAttributeCategory getCategory()
   {
      return this._category;
   }

   public void setCategory(AttributeCategory category)
   {
      this._category = category;
   }
   
   public int getCategoryID()
   {
      return this._categoryID;
   }
   
   public void setCategoryID(int categoryID)
   {
      this._categoryID = categoryID;
   }

   public String getContents()
   {
      return this._contents;
   }

   public void setContents(String contents)
   {
      this._contents = contents;
   }

   public int getID()
   {
      return this._id;
   }

   public void setID(int id)
   {
      this._id = id;
   }

   public String getName()
   {
      return this._name;
   }

   public void setName(String name)
   {
      this._name = name;
   }
}


Code:
package com.mycompany.bol;

import java.util.List;

public class AttributeCategory
{
   private int _id;
   private int _directoryID;
   private String _category;
   private List<Attribute> _attributes;

   public AttributeCategory()
   {
   }   

   public String getCategory()
   {
      return this._category;
   }

   public void setCategory(String category)
   {
      this._category = category;
   }

   public int getDirectoryID()
   {
      return this._directoryID;
   }

   public void setDirectoryID(int directoryid)
   {
      this._directoryID = directoryid;
   }

   public int getID()
   {
      return this._id;
   }

   public void setID(int id)
   {
      this._id = id;
   }

   public List<Attribute> getAttributes()
   {
      return this._attributes;
   }

   public void setAttributes(List<Attribute> attributes)
   {
      this._attributes = attributes;
   }
}


Here are my AttributeCategory and Attribute hbm.xml files, respectively:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class
      name="com.agribeef.bol.Attribute"
      proxy="com.agribeef.bol.Attribute"
      table="ed_attribute">
     <id name="ID" column="attribute_id" type="int" unsaved-value="0">
           <generator class="identity"/>
    </id>
     <property name="CategoryID" column="attribute_category_id"/>
     <property name="Name" column="name"/>
     <property name="Contents" column="contents"/>
     <many-to-one
        name="Category"
        cascade="save-update"
        class="com.agribeef.bol.AttributeCategory"
        lazy="false"
        insert="false"
        update="false"
        column="attribute_category_id" />
   </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class
      name="com.agribeef.bol.AttributeCategory"
      proxy="com.agribeef.bol.AttributeCategory"
      table="ed_attribute_category">
     <id name="ID" column="attribute_category_id" type="int" unsaved-value="0">
       <generator class="identity"/>
    </id>
     <property name="DirectoryID" column="directory_id"/>
     <property name="Category" column="category"/>
     <list
        name="Attributes"
        table="ed_attribute"
        inverse="true"
        lazy="true"
        cascade="all-delete-orphan">
        <key column="attribute_category_id" />
        <index column="directory_id" />
        <one-to-many class="com.agribeef.bol.Attribute" />
     </list>
   </class>
</hibernate-mapping>


My call to retrieve a list of AttributeCategory objects (with nested Attribute lists in each) is too dispersed amongst app layers to provide all the code but rest assured, I'm able to call unrelated lists just fine the same way - I'm only getting an error when trying to go beyond a single map-to-class in the mapping files. The error I'm getting is:

Code:
15:14:16,702 ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: com.agribeef.bol.AttributeCategory.Attributes - no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.agribeef.bol.AttributeCategory.Attributes - no session or session was closed


The session is most definitely not closed - like I said, I'm using the same boilerplate HibernateUtil from Caveat Emptor all over this application and it works fine as long as I'm only mapping one class to one mapping file...this is the only case I get a "session closed" problem.

Please point out what I've done wrong here, I can't spend another 8 hours (at it for 3 days now) trying to figure out how to simply map a parent/child relationship together...and I'm mentally spent.

Thanks everybody, I appreciate the help!

Hibernate version: 3.0.5


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 2:04 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
zambizzi wrote:
The session is most definitely not closed - like I said, I'm using the same boilerplate HibernateUtil from Caveat Emptor all over this application and it works fine as long as I'm only mapping one class to one mapping file...this is the only case I get a "session closed" problem.

Though it is not a direct help, my advice would be that you write a simple unit test that mimics the behavior of your application related to the collection of attributes (therefore without all those app layers in between).

If the problem remains, post the unit test so that I (and others) can also reproduce the problem and help you more efficiently. If the problem does not occur, then you'll know that it is somehow due to those app layers.

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 10:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
The session is most definitely not closed

If you get this exception, the session is most definitely closed, whether you think it is or not.

Take Eric's advice and simplify down your use case.


Top
 Profile  
 
 Post subject: simple test
PostPosted: Fri Sep 23, 2005 11:17 am 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
I got around the session management issue (which from what I've read here and after much googling - is a big deal in Hibernate) - simply by moving the opening/closing of the session, through encapsulation, right down to the servlet level so there is one session per request...which may not be the best way of doing it but after all the fighting I've done w/ this simple example....I'm ready to accept (for now.)

All I was hearing in here in reply was "session management works depending on what you're doing" - which is a non-answer if I've ever heard one...I think most people tend to avoid this question like the plague on these forums...but I hear "Seam" will take care of this issue?

Anyhow, now that I've gotten that far, it is *still* not retrieving the list as I would have expected!! Even though the query shows a left outer join...and when I run it against the data...it works, the Attributes list is null.

I've made slight modifications to my hbm.xml files...here's AttributeCategory and Attribute, respecitvely:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class
      name="com.agribeef.bol.AttributeCategory"
      proxy="com.agribeef.bol.AttributeCategory"
      table="ed_attribute_category">
     <id name="ID" column="attribute_category_id" type="int" unsaved-value="0">
       <generator class="identity"/>
    </id>
     <property name="DirectoryID" column="directory_id"/>
     <property name="Category" column="category"/>
     <list
        name="Attributes"
        table="ed_attribute"
        inverse="true"
        lazy="true"
        fetch="join"
        cascade="save-update">
        <key column="attribute_id" />
        <index column="attribute_category_id" />
        <one-to-many class="com.agribeef.bol.Attribute" />
     </list>
   </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class
      name="com.agribeef.bol.Attribute"
      proxy="com.agribeef.bol.Attribute"
      table="ed_attribute">
     <id name="ID" column="attribute_id" type="int" unsaved-value="0">
      <generator class="identity"/>
    </id>
     <property name="CategoryID" column="attribute_category_id"/>
     <property name="Name" column="name"/>
     <property name="Contents" column="contents"/>
     <many-to-one
        name="Category"
        cascade="save-update"
        class="com.agribeef.bol.AttributeCategory"
        lazy="false"
        fetch="join"
        insert="false"
        update="false"
        column="attribute_category_id" />
   </class>
</hibernate-mapping>


...the classes remain the same.

I can pull the AttributeCategory when "querying" it...but obviously I can't go any deeper...the list is Attributes list in AttributeCategory wasn't populated.

Here are the generated queries (formatted for readability):

Code:
select
attributec0_.attribute_category_id as attribute1_,
attributec0_.directory_id as directory2_5_,
attributec0_.category as category5_
from
ed_attribute_category attributec0_


Code:
select
attributes0_.attribute_id as attribute1_2_,
attributes0_.attribute_category_id as attribute2_2_,
attributes0_.attribute_id as attribute1_1_,
attributes0_.attribute_category_id as attribute2_4_1_,
attributes0_.name as name4_1_,
attributes0_.contents as contents4_1_,
attributec1_.attribute_category_id as attribute1_0_,
attributec1_.directory_id as directory2_5_0_,
attributec1_.category as category5_0_
from
ed_attribute attributes0_
left outer join
ed_attribute_category attributec1_
on
attributes0_.attribute_category_id=attributec1_.attribute_category_id
where attributes0_.attribute_id=?


The PK returned in the first query is "1"....so if I run the second query, having replaced the "?" w/ "1"....I get the data I expect!

What am I doing wrong here?

Thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 11:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Mate, the tone of your posts is really starting to piss me off.

People have been very helpful to you, but it seems you are more interested in trying to shift the blame for your problems off of yourself, than in seeking understanding.

Read the rules of the forum.

Stop blaming Hibernate for the fact that you do not understand the idea of persistence contexts properly.

If you want to continue in the vein, please go elsewhere.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 12:15 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
gavin wrote:
Mate, the tone of your posts is really starting to piss me off.

People have been very helpful to you, but it seems you are more interested in trying to shift the blame for your problems off of yourself, than in seeking understanding.

Read the rules of the forum.

Stop blaming Hibernate for the fact that you do not understand the idea of persistence contexts properly.

If you want to continue in the vein, please go elsewhere.


Well I'm sorry for "pissing you off" but to be perfectly straight w/ you, your tone is no better. Every time I post something I'm told it's my fault for being ignorant - because I don't understand this or don't understand that...well no! I don't understand, that's precisely why I'm here!

Finally, last night you tell me that there *hasn't* been a good implementation of session management...since it had been left up to "the frameworks" to figure out. Fine, now I know what I'm up against...at least you can understand the frustration. As for the mapping stuff I'm asking about here, I'm following simple examples from your book and not getting the results I would have expected, so *excuse* me for asking.

There's no need to get defensive and lash out against what could have potentially been an enterprise customer...it might have been better to at least explain a work around.

I think you've got a great technology here but your idea of customer service is seriously skewed.

Not a problem though, thanks for the help, I'll simply seek out another technology for recommendation...there are enough out there.

I won't bother to check back.

Thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 1:21 pm 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
zambizzi wrote:
Every time I post something I'm told it's my fault for being ignorant - because I don't understand this or don't understand that...well no! I don't understand, that's precisely why I'm here!

Finally, last night you tell me that there *hasn't* been a good implementation of session management...since it had been left up to "the frameworks" to figure out. Fine, now I know what I'm up against...at least you can understand the frustration. As for the mapping stuff I'm asking about here, I'm following simple examples from your book and not getting the results I would have expected, so *excuse* me for asking.

There's no need to get defensive and lash out against what could have potentially been an enterprise customer...it might have been better to at least explain a work around.

I think you've got a great technology here but your idea of customer service is seriously skewed.

Not a problem though, thanks for the help, I'll simply seek out another technology for recommendation...there are enough out there.

I won't bother to check back.

Thanks again!

I don't think you got it right.

Lots of people here (including Gavin, though you may not believe it) are willing to help you - but you don't provide us with the information allowing us to help you efficiently.

Instead you complain about this and that and throw your frustration at us - this leads nowhere (it just pisses off Gavin...). Personally I wouldn't care, if only you provided me with the necessary information to help you.

For sure Hibernate is not always that simple (I confess it took me a few months to understand certain aspects of Hibernate and I certainly do not master quite a few), but if toddlers could understand it, no one would pay us for building applications with it.

Keep cool, be patient - things take time. Take one hurdle after another (my feeling is that you'd be frustrated with any non-trivial technology - but I bet a trivial technology would not interest you). And when you have a problem, help others help you. And last but not least, be friendly - it does cost much, and increases the readiness of others to help you.

Whatever you might think of me after this post, consider these (cheap) advices - they're helpful, here and everywhere.

If you're willing to provide the information I need to understand and reproduce your problem, I'm still willing to help you - but never forget that neither I nor Hibernate are the actual cause of your frustration.

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 1:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
your idea of customer service is seriously skewed


Oh, you are a customer? My apologies, I did not realize.

If you are a customer, please use the JBoss Network customer support portal. Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.