-->
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: Listing many to many database relationship
PostPosted: Thu Feb 21, 2008 7:07 pm 
Newbie

Joined: Thu Feb 21, 2008 2:39 pm
Posts: 3
Hi! I have a many to many relation into my database between the tables Category and Content.

Category -> CategoryContent <- Content

In the table CategoryContent I just jave have the id's from both Category and Content table.

My question is: How can I get the list of all Contents if I know the Category id?

public Set<Content> contentList (int categoryId).

My mapping files have been generate by MyEclipse and look like this:

CategoryContent.hbm.xml
<hibernate-mapping>
<class name="hibernate.CategoryContent" table="category_content" catalog="crestin_dev">
<composite-id name="id" class="hibernate.CategoryContentId">
<key-many-to-one name="category" class="hibernate.Category">
<column name="category_id" />
</key-many-to-one>
<key-many-to-one name="content" class="hibernate.Content">
<column name="content_id" />
</key-many-to-one>
</composite-id>
</class>
</hibernate-mapping>

AbstractCategory.java
public abstract class AbstractCategoryContent implements java.io.Serializable {
private CategoryContentId id;
// Constructors

/** default constructor */
public AbstractCategoryContent() {
}

/** full constructor */
public AbstractCategoryContent(CategoryContentId id) {
this.id = id;
}

// Property accessors

public CategoryContentId getId() {
return this.id;
}

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


AbstractCategoryContentId.java
public abstract class AbstractCategoryContentId implements java.io.Serializable {
// Fields
private Category category;
private Content content;
// Constructors

/** default constructor */
public AbstractCategoryContentId() {
}

/** full constructor */
public AbstractCategoryContentId(Category category, Content content) {
this.category = category;
this.content = content;
}

// Property accessors

public Category getCategory() {
return this.category;
}

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

public Content getContent() {
return this.content;
}

public void setContent(Content content) {
this.content = content;
}

public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof AbstractCategoryContentId) ) return false;
AbstractCategoryContentId castOther = ( AbstractCategoryContentId ) other;

return ( (this.getCategory()==castOther.getCategory()) || ( this.getCategory()!=null && castOther.getCategory()!=null && this.getCategory().equals(castOther.getCategory()) ) )
&& ( (this.getContent()==castOther.getContent()) || ( this.getContent()!=null && castOther.getContent()!=null && this.getContent().equals(castOther.getContent()) ) );
}

public int hashCode() {
int result = 17;

result = 37 * result + ( getCategory() == null ? 0 : this.getCategory().hashCode() );
result = 37 * result + ( getContent() == null ? 0 : this.getContent().hashCode() );
return result;
}

Category.hbm.xml
<hibernate-mapping>
<class name="hibernate.Category" table="category" catalog="crestin_dev">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<many-to-one name="category" class="hibernate.Category" fetch="select">
<column name="parent_category_id" />
</many-to-one>
<many-to-one name="language" class="hibernate.Language" fetch="select">
<column name="language_id" not-null="true" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" not-null="true" />
</property>
<property name="description" type="java.lang.String">
<column name="description" length="65535" />
</property>
<property name="sortingOrder" type="java.lang.Integer">
<column name="sorting_order" />
</property>
<property name="logoUrl" type="java.lang.String">
<column name="logo_url" />
</property>
<set name="categoryContents" inverse="true">
<key>
<column name="category_id" not-null="true" />
</key>
<one-to-many class="hibernate.CategoryContent" />
</set>
<set name="categories" inverse="true">
<key>
<column name="parent_category_id" />
</key>
<one-to-many class="hibernate.Category" />
</set>
</class>
</hibernate-mapping>

Content.hbm.xml
<hibernate-mapping>
<class name="hibernate.Content" table="content" catalog="crestin_dev">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="description" type="java.lang.String">
<column name="description" length="65535" />
<set name="quicklists" inverse="true">
<key>
<column name="content_id" not-null="true" />
</key>
<one-to-many class="hibernate.Quicklist" />
</set>
<set name="categoryContents" inverse="true">
<key>
<column name="content_id" not-null="true" />
</key>
<one-to-many class="hibernate.CategoryContent" />
</set>
</class>
</hibernate-mapping>

Can you please give me a hint on how could I use the DAO in order to receive the list, as I can't touch the table CategoryContent as I would normaly do with sql and join them together, so I have to get from Category to Content in some other way.
I have found no sollution after reading the docu and tutorials on the net after a day's search.

Thanks in advance for your answer!


Last edited by stancos on Fri Feb 22, 2008 7:10 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Listing many to many database relationship
PostPosted: Thu Feb 21, 2008 7:27 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
first you don't need to model CategoryContent unless you have relation information on it. So you may go ahead and get rid of it. As for your question you could use a HQL like:


Code:
select c from Category ct inner join ct.contents c where ct.id = :category_id




Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 9:05 pm 
Newbie

Joined: Thu Feb 21, 2008 2:39 pm
Posts: 3
Thanks you for your fast answer!

But I do have relation information on the table CategoryContent. As you can see they contain the id's from table Category and Content.

So I can't relate directly to "ct.contents"...
I can just relate with "ct.categoryContents" but then I receive all the rows from the table CategoryContent and not the list with the Content.

Many 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.