-->
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.  [ 1 post ] 
Author Message
 Post subject: Listing many to many database relationship
PostPosted: Sat Feb 23, 2008 7:36 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>

Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.