-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem fetching Object with two one-to-many bags (xml)
PostPosted: Mon May 30, 2011 6:14 pm 
Newbie

Joined: Mon May 30, 2011 4:58 pm
Posts: 3
Hello,

Im relativly new to Hibernate, but have to define an quite "complex" data model with Collections.
I allready tried alot, read many forum posts, googled alot, read books, but I allways have the same problem.

I have an quite simple java-object like the one below:
Code:
package de.model;

public class Stock {

    private Long id; // id auto_increment

   [...]

    private List<Alternative> alternatives;
   
   private List<Parameter> parameters;

   [geters and seters]
}


package de.model;

public class Alternative {

    private Long id; // id auto_increment

   [...]

    private Stock stock;
   
   [geters and seters]
}


I need the three objects (the third is as the Alternative class) in this relation:
One Stock has [0...n] Alternatives and [0...n] Parameters.
When generating these objects and saving the Stock object the following hibernate mapping works.
But on loading I only receive an List with 0/1 Elements in return lazy loading is off, session is closed.

[I tried so many different annotations with those two possibilities.]
[I tried: inverse=true/false, fetch=subselect/join (the last one conflicts with both bags that can't be eager fetched simultaneously.]

Code:
<?xml version="1.0" encoding="utf-8"?>
<!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping>
   
   <class name="de.model.stock" table="stock">
      <id name="id" column="id" type="long">
         <generator class="sequence"> <param name="sequence">seq_stock</param> <param name="increment_size">1</param> </generator>
      </id>                   

      [...]
      <bag  name="alternatives" inverse="true" lazy="false" fetch="subselect" cascade="all">
         <key column="group_alternatives_id" />
         <one-to-many class="de.model.alternative" />
      </bag >
      
      <bag name="parameters" inverse="true" lazy="false" fetch="subselect" cascade="all">
         <key column="parameter_id" />
         <one-to-many class="de.model.parameter" />
      </bag>      
   </class>

   
   <class name="de.model.Alternative" table="stockalts">
      <id name="id" column="alternatives_id" type="long">
         <generator class="sequence"> <param name="sequence">seq_stockalts</param> <param name="increment_size">1</param> </generator>
      </id>       
      <property name="isin" type="string" column="isin" length="255"/>   

      [...]
      <many-to-one name="stock" column="stock_id" lazy="false"
                class="de.model.stock"               
      />      
   </class>

   
   <class name="de.model.Parameter" table="params">
      <id name="id" column="parameter_id" type="long">
         <generator class="sequence"> <param name="sequence">seq_params</param> <param name="increment_size">1</param> </generator>
      </id>       

      [...]
      <many-to-one name="stock" column="stock_id" cascade="all" lazy="false" not-null="true"
                class="de.model.stock"               
      />
   </class>   
</hibernate-mapping>



Acessing the Stock. The TODOs and the manual load of the Alternative-list is exactly my problem.
Without this i only receive 1 element in maximum.

Code:
   public List<Stock> findStockByIsinAndStockListId(String isin, Long stockListId) {

       List<Stock> list = getHibernateTemplate().find("from Stock s where s.isin = ? AND s.stocksContext.id = ?", new Object[]{isin, stockListId});
       
        for (Stock stock : list) {
           /* TODO: isn't this possible directly via hibernate mapping?*/
            // TODO: hibernate only loads one element each
           List<Alternative> altList = getHibernateTemplate().find("from Alternative a where a.stock.id = ? ", stock.getId());
           stock.setAlternatives(altList);
        }
        return list;
    }



Has anyone an idea? I'm quite desperate. ;)
Help would be very much appreciated.
But I must admit that I allready read lot's of posts about similar problems, so i'm afraid an link to an other thread (without explaination what exactly i'm doing wrong) will most likely not help me. ;)

Thanks in advance.

Stevit


Top
 Profile  
 
 Post subject: Re: Problem fetching Object with two one-to-many bags (xml)
PostPosted: Tue May 31, 2011 8:33 am 
Newbie

Joined: Mon May 30, 2011 4:58 pm
Posts: 3
I fixed my isse my (unintentionally) creating the notation for an other relationship.... ;)

I had to add an reference to the childs table (and the parents id there) to the parental (within Stock) one-to-many mapping.

Code:
<bag name="alternatives" inverse="true" cascade="all" lazy="false" table="STOCKALTS" fetch="select">
    <key column="stock_id"/>
     <one-to-many class="de.model.Alternative" />
</bag>

<bag name="portfolioParameters" inverse="true" cascade="all" lazy="false" table="STOCKPARAMS" fetch="select">
    <key column="stock_id"/>
    <one-to-many class="de.model.PortfolioParameter" />
</bag>


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