-->
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: Hibernate Joins Problem
PostPosted: Wed Aug 12, 2009 9:05 am 
Newbie

Joined: Tue Jun 09, 2009 7:54 am
Posts: 10
Hi all,


I am trying to implement a simple program using joins in Hibernate. Please check my mapping files and the POJO code I have written.
Channel and User have a many-to-many relationship. When i run the following code, the Set "users" in Channel is not getting populated even though the DB contains one valid record.
The design and requirement is quite simple. Kindly let me know what could be going wrong.


Thanks,
N
Code:
import java.util.List;
import java.util.Set;



public class Channel {
   
   private long id;
   private String channelName;
   private Set<User> users;
   private Set<Relation> relations;
   
   public String getChannelName() {
      return channelName;
   }
   public void setChannelName(String channelName) {
      this.channelName = channelName;
   }
      public Set<User> getUsers() {
      return users;
   }
   public void setUsers(Set<User> users) {
      this.users = users;
   }
   public long getId() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }
   public Set<Relation> getRelations() {
      return relations;
   }
   public void setRelations(Set<Relation> relations) {
      this.relations = relations;
   }
   
   
}




import java.util.Set;


public class User {

   private long id;
   private String username;   
   private String fullName;
   private Set<Channel> channels;
   
   public String getUsername() {
      return username;
   }
   public void setUsername(String username) {
      this.username = username;
   }
   
   public String getFullName() {
      return fullName;
   }
   public void setFullName(String fullName) {
      this.fullName = fullName;
   }
   
   public long getId() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }
   
   public Set<Channel> getChannels() {
      return channels;
   }
   public void setChannels(Set<Channel> channels) {
      this.channels = channels;
   }
   
   
}


import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;





public class FirstExample {


   public static SessionFactory sessionFactory = null;



   private void getChannelUsers (Long channelId) {      
      Configuration config = new Configuration();
      Configuration configuration  = config.configure();
      sessionFactory = configuration.buildSessionFactory();
      Session session  =sessionFactory.openSession();
      Transaction transaction = session.beginTransaction();
      ArrayList<User> subList = new ArrayList<User>();
      Channel channelObj = (Channel)  session.get(Channel.class, 2l);
      
      Set<User> userSet = channelObj.getUsers();
      
      System.out.println("LIST " );
      for (Iterator i = userSet.iterator(); i.hasNext();) {
         User user = (User) i.next();
         System.out.println("USER") ;
         System.out.println(user.getFullName());   
      }
      
       transaction.commit();

       session.close();

   }

   public static void main(String args[]){

      FirstExample eg = new FirstExample();
      eg.getChannelUsers (2l);


   }


}





XML FILES:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="User" table="USER">
   <id name="id" column="USER_ID">
 
  </id>
  <!--<property name="username">
     <column name="USERNAME" />
  </property>-->
<property name="fullName">
     <column name="FULLNAME" />
  </property>

<set name="channels" table="user_channel" >
   <key column="user_id"/>
   <many-to-many class="Channel"/>
</set>
</class>
</hibernate-mapping>



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="Channel" table="CHANNEL">
<!--  <id name="channelId" type="long" column="CHANNEL_ID">
   <generator class="increment"/>
  </id>-->
<id name="id" column="CHANNEL_ID">

  </id>
  <property name="channelName">
     <column name="CHANNEL_NAME" />
  </property>
  <property name="channelDesc">
    <column name="DESCRIPTION"/>
  </property>
<set name="relations" cascade="all">
   <key column="channel_id"/>
   <one-to-many class="Relation"/>
</set>
<set name="users" table="user_channel" >
   <key column="channel_id"/>
   <many-to-many class="User"/>
</set>
</class>
</hibernate-mapping>


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.