| Author |
Hibernate - multiple collection with restrictions mapping problem
|
mat novak
Greenhorn
Joined: May 26, 2003
Posts: 7
|
|
Hello, I have a problem concerning mapping of multiple collections. Lets's say I want to have a List of ZOO's objects. I have a table called "zoo" with two fields: id - id of ZOO name - name of ZOO Simple export from this table could look like this: id:1 ; name:ZOO1 id:2 ; name:ZOO2 id:3 ; name:ZOO3 Then I have second table - "Animals" with four fields: id - id of animal zoo_id - id of zoo (this is a FK) animal_type - describes type of animal (there can by only four types of animals: dog, cat, fish, bird) animal_name - name of animal A simple export from this table could look as below: id:1 ; zoo_id:1 ; animal_type og ; animal_name: doggy1 id:2 ; zoo_id:1 ; animal_type og ; animal_name: doggy2 id:3 ; zoo_id:1 ; animal_type:cat ; animal_name: cat1 id:4 ; zoo_id:2 ; animal_type:cat ; animal_name: cat2 id:5 ; zoo_id:2 ; animal_type:fish ; animal_name: fishy1 id:6 ; zoo_id:2 ; animal_type:bird ; animal_name: bird1 Now, I want to have Java class called "ZOO" as follows: class Zoo{ Set dogs; //can be a Hashset Set cats; Set fish; Set birds; //I ommitt the rest of the listing of this class } Finally, I would like to retrieve from DB the list of ZOO's with eager-fetched animals just by calling List zooList = session.createCriteria(Zoo.class).list(); As a result I should get List with two elements element1 - object corresponding to ZOO1 (Set dogs should contain 2 elements ; Set cats should contain 1 elements ; Set fish should contain 0 elements ; Set birds should contain 0 elements) element2 - object corresponding to ZOO2 (Set dogs should contain 0 elements ; Set cats should contain 1 elements ; Set fish should contain 1 elements ; Set birds should contain 1 elements) Could you please advise me how to map tables/classes etc.?
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8209
|
|
Here's just a sample. There might be more ways of doing this. Zoo.hbm.xml: Animal.hbm.xml:
|
[My Blog] [JavaRanch Journal]
|
 |
mat novak
Greenhorn
Joined: May 26, 2003
Posts: 7
|
|
Hello, thank you for reply. Still I do something wrong. According to suggestion above, I did a mapping as shown below (I show only mapping for Zoo's): Zoo.hbm.xml: code: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="org.myapp.hibernateobject.Zoo" table="ZOO" > <id> ..... </id> <property name="name" type="java.lang.String" column="Name" > </property> <set name="cats" lazy="false" inverse="true" cascade="all-delete-orphan"> <key> <column name="id" /> </key> <one-to-many class="org.myapp.hibernateobject.Cat" /> </set> <set name="dogs" lazy="false" inverse="true" cascade="all-delete-orphan"> <key> <column name="id" /> </key> <one-to-many class="org.myapp.hibernateobject.Dog" /> </set> <set name="fish" lazy="false" inverse="true" cascade="all-delete-orphan"> <key> <column name="id" /> </key> <one-to-many class="org.myapp.hibernateobject.Fish" /> </set> </class> </hibernate-mapping> And if I call Cat/Dog/Fish class explicitly everything is ok. i.e. Criteria criteria = session.createCriteria(Cat.class); List projects = criteria.list(); produces SQL like this (I simplified this a little): (ble ble ble) where Animal_Type='Cat' (so the proper mapping is chosen) But if I call a list of ZOO's List zooList = session.createCriteria(Zoo.class).list(); for every collection declared in ZOO class (that is: Set dogs; Set cats; Set fish generated SQL looks as follows: (ble ble ble) where zoo_id=? and there is missing part " and Animal_Type='Cat' " Could you please tell me what do I do wrong?
|
 |
 |
|
|
subject: Hibernate - multiple collection with restrictions mapping problem
|
|
|