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