-->
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.  [ 4 posts ] 
Author Message
 Post subject: [solved] join many tables (many-to-many and one-to-many)
PostPosted: Tue Jul 11, 2006 1:25 pm 
Newbie

Joined: Tue Jul 11, 2006 1:09 pm
Posts: 4
hi everybody,

i have read the documentation and still need help.

I have 4 tables THEME, FEED_THEME, FEED, NEWS.
theme and feed are many-to-many.
feed and news are one-to-many.
(News- id_news,id_feed)--manyToOne-->(Feed-id_feed)--manyToMany-->(Feed_Theme-id_feed,id_theme)--manyToMany-->(theme-id_theme)

i have this query working (last news ordered for a given theme):
Code:
SELECT n . *
FROM `feed_theme` ft, `feed` f, `news` n
WHERE ft.id_theme =3
AND f.id_feed = ft.id_feed
AND n.id_feed = f.id_feed
ORDER BY n.founddate DESC , n.newsdate
LIMIT 20


but i don't know how to get this query working with hibernate.
This query return a list of object News.

thanks for your help


Last edited by remix on Sat Jul 15, 2006 11:27 am, edited 4 times in total.

Top
 Profile  
 
 Post subject: Re: query involving 4 tables (many-to-many and one-to-many)
PostPosted: Wed Jul 12, 2006 5:48 am 
Newbie

Joined: Tue Jul 11, 2006 1:09 pm
Posts: 4
remix wrote:
hi everybody,

i have read the documentation and still need help.

I have 4 tables THEME, FEED_THEME, FEED, NEWS.
theme and feed are many-to-many.
feed and news are one-to-many.

i have this query working (last news ordered for a given theme):
Code:
SELECT n . *
FROM `feed_theme` ft, `feed` f, `news` n
WHERE ft.id_theme =3
AND f.id_feed = ft.id_feed
AND n.id_feed = f.id_feed
ORDER BY n.founddate DESC , n.newsdate
LIMIT 20


but i don't know how to get this query working with hibernate.
This query return a list of object News.

thanks for your help


another question: do you think it's possible to do that with hibernate?
more informations: a theme(wich is a category) has many feed, a feed has many news.

here are my hbm.xml file:

theme.hbm.xml
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 package="org.woox.givemefive.model">
   <class name="Theme" table="theme">
      <id name="id_theme" column="id_theme">
         <generator class="increment" />
      </id>
      <property name="title" column="title" />
      <property name="description" column="description" />
      
   <set name="feeds" table="feed_theme">
        <key column="id_theme"/>
        <many-to-many column="id_feed" class="Feed"/>
    </set>
   </class>
</hibernate-mapping>

feed.hbm.xml
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 package="org.woox.givemefive.model">
<class name="Feed" table="feed">
<id name="id_feed" column="id_feed" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="url" column="url" type="java.lang.String" />
<property name="name" column="name" type="java.lang.String" />
  <property name="title" column="title" type="java.lang.String" />
   <property name="description" column="description" type="java.lang.String" />
    <property name="siteurl" column="siteurl" type="java.lang.String" />
<property name="isactive" column="isactive" type="java.lang.Boolean" />
<property name="lastupdate" column="lastupdate" type="timestamp" />

   <set name="newsList">
       <key column="id_feed" />
       <one-to-many class="News"/>
   </set>
   
<set name="themes" table="feed_theme" inverse="true">
    <key column="id_feed"/>
    <many-to-many column="id_theme" class="Theme"/>
</set>

</class>
</hibernate-mapping>

and news.hbm.xml

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 package="org.woox.givemefive.model">
   <class name="News" table="news">
      <id name="id_news" column="id_news">
         <generator class="increment" />
      </id>
      <property name="id_feed" column="id_feed" />
      <property name="title" column="title" />
      <property name="description" column="description" />
      <property name="link" column="link" />
      <property name="author" column="author" />
      <property name="newsdate" column="newsdate" type="timestamp" />
      <property name="founddate" column="founddate" type="timestamp" />


      <many-to-one name="feed" class="Feed" insert="false" update="false" column="id_feed" />
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 15, 2006 9:19 am 
Newbie

Joined: Tue Jul 11, 2006 1:09 pm
Posts: 4
error :Repeated column in mapping for collection: org.woox.givemefive.model.News.feed_theme column: id_feed

whith this query :

Code:
public List getLastNNewsByIdTheme(Long id_Theme, Long howManyNews) {
   Query crit = getSession().createQuery(
            "select news from News as news" +
            "join news.feed as feed"+
            "join feed.themes as theme"+
            "theme.id_theme= 3"
            );
   crit.setMaxResults(howManyNews.intValue());

return crit.list();
}

i really need help ;) thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 15, 2006 9:20 am 
Newbie

Joined: Tue Jul 11, 2006 1:09 pm
Posts: 4
ok solved with this query:

Code:
   public List getLastNNewsByIdTheme(Long id_Theme, Long howManyNews) {

      Query crit = getSession().createQuery(
            "select news from News as news " +
            "join news.feed as feed "+
            "join feed.themes as theme "+
            "where theme.id_theme= 3 "
            );
      //crit.addOrder(Order.desc("founddate"));
        //crit.addOrder(Order.desc("newsdate"));
      crit.setMaxResults(howManyNews.intValue());
      return crit.list();
}


my pb: thinking SQL instead of thinking object defined by the mapping files.
what a nice day ;)


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