4 Replies Latest reply on Jul 24, 2002 2:54 PM by Leila Tite

    Problem with CMR please help

    navneet Newbie

      hi,

      Can anyone help me with CMR fields. I have three entity User,Topic and Subscription. Subscription is the many to many relation between user and topic.
      Each subscription has one user and one topic. The User and Topic need not know about the Subscription.

      My subscriptionbean looks like this
      -------------------------------------

      public abstract class SubscriptionBean implements EntityBean{

      private EntityContext context = null;

      public abstract Integer getSubscriptionId();
      public abstract LocalUser getUser();
      public abstract LocalTopic getTopic();

      public abstract void setSubscriptionId(Integer s);
      public abstract void setUser(LocalUser s);
      public abstract void setTopic(LocalTopic s);


      public Integer ejbCreate(String username, String topic) throws CreateException {
      //generate pk
      //for testing purpose have to be changed
      setSubscriptionId(new Integer((int)(Math.random()*10000)));
      return null;
      }
      public void ejbPostCreate(String username, String topic) throws CreateException
      {
      try
      {
      //CMR fields
      InitialContext ctx = new InitialContext();
      Object objRef = ctx.lookup("local/User");
      LocalUserHome home=(LocalUserHome)PortableRemoteObject.narrow(objRef, LocalUserHome.class);
      LocalUser u=home.findByPrimaryKey(username);
      System.out.println("looked up user: " + u.getUsername());
      setUser(u);

      Object objRef1 = ctx.lookup("local/Topic");
      LocalTopicHome thome=(LocalTopicHome)PortableRemoteObject.narrow(objRef1, LocalTopicHome.class);
      LocalTopic t=thome.findByPrimaryKey(topic);
      System.out.println("looked up topic: " + t.getTopicName());
      setTopic(t);
      }
      catch (Exception ex)
      {
      System.out.println("error in post create");
      }
      }
      .....
      }
      -------------------------------------------------------------

      I tried to attach my ejb-jar file but it wasnt working so i am putting relevant parts here
      ejb-jar looks the following

      ------------------------------------------------------------
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC
      "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
      "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

      <ejb-jar>
      <enterprise-beans>

      <display-name>User</display-name>
      <ejb-name>User</ejb-name>
      ....
      <primkey-field>username</primkey-field>


      <display-name>Topic</display-name>
      <ejb-name>Topic</ejb-name>
      .....
      <primkey-field>topicName</primkey-field>



      <display-name>Subscription</display-name>
      <ejb-name>Subscription</ejb-name>
      ....
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      False
      <ejb-local-ref>
      <ejb-ref-name>local/User</ejb-ref-name>
      <ejb-ref-type>Entity</ejb-ref-type>
      <local-home>mobile.entity.LocalUserHome</local-home>
      mobile.entity.LocalUser
      <ejb-link>User</ejb-link>
      </ejb-local-ref>
      <ejb-local-ref>
      <ejb-ref-name>local/Topic</ejb-ref-name>
      <ejb-ref-type>Entity</ejb-ref-type>
      <local-home>mobile.entity.LocalTopicHome</local-home>
      mobile.entity.LocalTopic
      <ejb-link>Topic</ejb-link>
      </ejb-local-ref>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>Subscription</abstract-schema-name>
      <cmp-field>
      <field-name>subscriptionId</field-name>
      </cmp-field>
      <primkey-field>subscriptionId</primkey-field>

      </enterprise-beans>

      <ejb-relation>
      <ejb-relation-name>user-subscription</ejb-relation-name>
      <ejb-relationship-role>
      User
      <ejb-relationship-role-name>UserSubscriptionRole</ejb-relationship-role-name>
      One
      <relationship-role-source>
      user
      <ejb-name>User</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>
      <ejb-relationship-role>
      subscription
      <ejb-relationship-role-name>subscriptionUserRole</ejb-relationship-role-name>
      Many
      <relationship-role-source>
      subscription
      <ejb-name>Subscription</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>user</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>
      <ejb-relation>
      <ejb-relation-name>topic-subscription</ejb-relation-name>
      <ejb-relationship-role>
      Topic
      <ejb-relationship-role-name>TopicSubscriptionRole</ejb-relationship-role-name>
      One
      <relationship-role-source>
      topic
      <ejb-name>Topic</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>
      <ejb-relationship-role>
      subscription
      <ejb-relationship-role-name>subscriptionUserRole</ejb-relationship-role-name>
      Many
      <relationship-role-source>
      subscription
      <ejb-name>Subscription</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>topic</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>


      <assembly-descriptor>
      ....

      ------------------------------------------------------------

      I already have a user 'abc' and topic 'testtopic'. Now when i create a subscription from using

      LocalSubscription subs = home.create("abc",testtopic");

      it should create a subscription for user 'abc'; topic 'testtopic'
      but then if i do

      subs.getTopic() or subs.getUser() i get null
      even if i do 'select * from subscription' i get rows with user and topic column as NULL

      I am new to this. I know i must be doing something wrong. Just cannot figure out what.
      Please someone help.