-->
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.  [ 3 posts ] 
Author Message
 Post subject: HQL for many-to-many mapping
PostPosted: Tue Jun 10, 2008 2:07 pm 
Newbie

Joined: Tue Jun 10, 2008 1:53 pm
Posts: 1
Location: Atlanta
Iam trying to implement a many-to-many mapped application , my domain Objects are as shown below :

public class Folder implements Persistable
{
private Long id;
private String folderName;

private Set<Filterable> myQueries;
private Set<Filterable> mySets;
private Set<Filterable> myScenarios;

public Folder(String name,List<Filterable> queries, List<Filterable>sets, List<Filterable>scenarios)
{
setFolderName(name);
setMyQueries(new HashSet<Filterable>(queries));
setMySets(new HashSet<Filterable>(sets));
setMyScenarios(new HashSet<Filterable>(scenarios));
}

public Folder()
{

}
public void addQuery(Filterable query)
{
this.getMyQueries().add(query);
}

public void addSet(Filterable set)
{
this.getMySets().add(set);
}

public void addScenario(Filterable scenario)
{
this.getMyScenarios().add(scenario);
}


public Long getId()
{
return id;
}

public void setId(Long id)
{
this.id = id;
}

public String getFolderName()
{
return folderName;
}

public void setFolderName(String folderName)
{
this.folderName = folderName;
}

public Set<Filterable> getMyQueries()
{
return myQueries;
}

public void setMyQueries(Set<Filterable> myQueries)
{
this.myQueries = myQueries;
}

public Set<Filterable> getMySets()
{
return mySets;
}

public void setMySets(Set<Filterable> mySets)
{
this.mySets = mySets;
}

public Set<Filterable> getMyScenarios()
{
return myScenarios;
}

public void setMyScenarios(Set<Filterable> myScenarios)
{
this.myScenarios = myScenarios;
}
}

As can be seen from the above class , One the association is :
1 folder can have many Queries/Sets/scenarions (all these are domain Objects themselves).
A single query/set/Scenario can exist in many Folders

My tables in play are :

Folder (Corresponds to the folder domain Object)
SearchQuery(Corresponds to the qury Domain Object)
Set(Corresponds to the set domain Object)
Scenario(Corresponds to the Scenario domain Object)
folder-Query (Link table btw the folder and the Query)
folder-set(Link table between the folder and the set)
folder-scenario(Link table between the folder and the scenario)

My hbm mapping is as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.equifax.ic.tdp.framework.catalog.dao.Folder" table="FOLDER">
<id name="id" column="id" type="long">
<generator class="native">
<param name="sequence">folder_seq</param>
</generator>
</id>
<property name="folderName" type="string" column="NAME" not-null="true" />
<set name="myQueries" table="FOLDER_QUERY" lazy="true" cascade="all">
<key column="FOLDER_ID"/>
<many-to-many class="com.equifax.ic.tdp.framework.catalog.dao.SearchQuery" column="QUERY_ID"/>
</set>
<set name="mySets" table="FOLDER_SET" lazy="true" cascade="all">
<key column="FOLDER_ID"/>
<many-to-many class="com.equifax.ic.tdp.framework.catalog.dao.ContentSet" column="SET_ID"/>
</set>
<set name="myScenarios" table="FOLDER_SCENARIO" lazy="true" cascade="all">
<key column="FOLDER_ID"/>
<many-to-many class="com.equifax.ic.tdp.framework.catalog.dao.BasicScenario" column="SCENARIO_ID"/>
</set>
</class>
<query name="find.folders.with.queries">
<![CDATA[
from Folder fldr join fetch fldr.myQueries
]]>
</query>
<query name="find.folders.with.sets">
<![CDATA[
from Folder fldr join fetch fldr.mySets
]]>
</query>
<query name="find.folders.with.scenarios">
<![CDATA[
from Folder fldr join fetch fldr.myScenarios
]]>
</query>
</hibernate-mapping>

Question:
Iam succesfully able to save the above many-to-many mapping, but when retrieving the folder Object, I am compelled to use three different queries as in the above hbm file , to retrive it with Queries/sets and Scenarios.

Iam not able to disjucnt this join , I want to retrieve the folder object with just One HQL which has all the queries/sets and scenarions.

In short if I have more than one many-to-many in the same class , how do I wrote the HQL query to get that Object and all the associated Objects.(Iam able to get one associate Object at a time) .


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 12:19 am 
Newbie

Joined: Tue Apr 29, 2008 1:42 pm
Posts: 12
Using the query "from Folder" is enough. Since you have getters and setters for Sets, Queries and Scenarios, you can call these methods after executing the query to get the associated objects. Hibernate itself generates the required queries to get these associated objects.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:39 am 
Newbie

Joined: Tue Apr 29, 2008 1:42 pm
Posts: 12
Or you can set lazy="false" in your mapping.

Thanks.


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