Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / October 2003

Tip: Looking for answers? Try searching our database.

CMP Persistence (1:n Relations)

Thread view: 
ralf sczepan - 20 Oct 2003 11:12 GMT
Hi Group,
I have got two Entity-Beans, one is named Product the other ist named
ProductGroup. I would like to implement a 1:n Relation wich means
every Product has one Productgroup and a Productgroup has many
Products.

The ProductGroupBean.java looks like this:
........
public abstract class ProductGroupBean  implements EntityBean{
protected EntityContext ctx;
     
         
public abstract Integer getId() ;
public abstract void setId(Integer id);
public abstract Collection getProduct();
public abstract void setProduct(Collection artikel);
......

In the Client I try set an Product in a ProductGroupBean

.......
ProductHome productHome =
(ProductHome)javax.rmi.PortableRemoteObject.narrow(obj,
ProductHome.class );
       Product product = productHome.create(new Integer(1183),
"Book", "j", "status", new Integer(1));
       
Object objg = ctx.lookup( "ProductGroup" );
ProductGroupHome productGroupHome =
ProductGroupHome)javax.rmi.PortableRemoteObject.narrow(objg,
ProductGroupHome.class );
ProductGroup productGroup = productGroupHome.create( new Integer(43),
"Media");
       
Collection artikels = new ArrayList();
artikels.add(artikel);
 
artikelgruppe.setArtikel(artikels);  //<-- This method causes a
ClassCastException
........

Whats wrong with this method? I can not sea any reason for this
Exception!
Or isn't it the way to use Collection in 1:n RelationShips whith
Beans?

Thanks for your help
Marek Lange - 20 Oct 2003 11:53 GMT
> Hi Group,
> I have got two Entity-Beans, one is named Product the other ist named
[quoted text clipped - 32 lines]
> Collection artikels = new ArrayList();
> artikels.add(artikel);
               ^^^^^^^^ is this your product created above?
>    
> artikelgruppe.setArtikel(artikels);  //<-- This method causes a
[quoted text clipped - 4 lines]
> Or isn't it the way to use Collection in 1:n RelationShips whith
> Beans?

I always used this way (adapted to your example):

Collection artikels = artikelGruppe.getArtikel();

// should be enough to set the relation
artikels.add(artikel);

Post more code if this does not work.

-marek
ralf sczepan - 21 Oct 2003 15:32 GMT
> >        
> > Collection artikels = new ArrayList();
> > artikels.add(artikel);
>              ^^^^^^^^ is this your product created above?
Yes that is the Product! (I "translated" the Source from German to
English and forgot some parts, sorry)

> I always used this way (adapted to your example):
>
[quoted text clipped - 6 lines]
>
> -marek
first of all thanks for help

If I use this Code in a SessionBean (means I use the Local -
Interfaces) it works. But I get an Exception if I try to get the
Collection filled with Artikels.

Collection artikels = artikelGruppe.getArtikel();  // throws an
Exception

Here some more Code:

public abstract class ArtikelBean implements EntityBean {
       protected EntityContext ctx;
       public abstract  Integer getArtid();
       public abstract void setArtid(Integer id);
       public abstract String getBezeichnung();
       public abstract void setBezeichnung(String name);
       
       public abstract Collection getArtikelGruppe();
       public abstract void setArtikelGruppe(Collection artgruppen);
       public void setEntityContext (EntityContext ctx) {
               this.ctx = ctx;
       }
....
     
}

public interface Artikel extends EJBObject  {
   public  Integer getArtid() throws RemoteException;
   public void setArtid(Integer id) throws RemoteException;
   public String getBezeichnung() throws RemoteException;
   public void setBezeichnung(String bez) throws RemoteException;
     
   public Collection getArtikelGruppe() throws RemoteException;
   public void setArtikelGruppe(Collection artgruppen) throws
RemoteException;
}

The ejb-jar.xml

<?xml version="1.0"?>

<!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>
 
  <entity>
     <ejb-name>Artikel</ejb-name>
     <home>satzmedia.sGrid.app.model.ArtikelHome</home>
     <remote>satzmedia.sGrid.app.model.Artikel</remote>
     <local-home>satzmedia.sGrid.app.model.ArtikelLocalHome</local-home>
     <local>satzmedia.sGrid.app.model.ArtikelLocal</local>
     <ejb-class>satzmedia.sGrid.app.model.ArtikelBean</ejb-class>
     <persistence-type>Container</persistence-type>
     <prim-key-class>java.lang.Integer</prim-key-class>
     <reentrant>False</reentrant>
     <cmp-version>2.x</cmp-version>
     <abstract-schema-name>ArtikelBean </abstract-schema-name>
     <cmp-field><field-name>artid</field-name></cmp-field>
      <cmp-field><field-name>bezeichnung</field-name></cmp-field>
       <primkey-field>artid</primkey-field>
     <query>
          <query-method>
              <method-name>findByBezeichnung</method-name>
              <method-params>
                   <method-param>java.lang.String</method-param>
              </method-params>
           </query-method>
           <ejb-ql>
               <![CDATA[SELECT OBJECT(a) FROM ArtikelBean AS a where
a.bezeichnung = ?1]]>
            </ejb-ql>
   </query>
   </entity>
    <entity>
     <ejb-name>ArtikelGruppe</ejb-name>
     <home>satzmedia.sGrid.app.model.ArtikelGruppeHome</home>
     <remote>satzmedia.sGrid.app.model.ArtikelGruppe</remote>
     <local-home>satzmedia.sGrid.app.model.ArtikelGruppeLocalHome</local-home>
     <local>satzmedia.sGrid.app.model.ArtikelGruppeLocal</local>
     <ejb-class>satzmedia.sGrid.app.model.ArtikelGruppeBean</ejb-class>
     <persistence-type>Container</persistence-type>
     <prim-key-class>java.lang.Integer</prim-key-class>
     <reentrant>False</reentrant>
     <cmp-version>2.x</cmp-version>
     <abstract-schema-name>ArtikelGruppeBean</abstract-schema-name>
     <cmp-field><field-name>id</field-name></cmp-field>
     <cmp-field><field-name>bezeichnung</field-name></cmp-field>
     <primkey-field>id</primkey-field>
     <query>
          <query-method>
              <method-name>findByBezeichnung</method-name>
              <method-params>
                   <method-param>java.lang.String</method-param>
              </method-params>
           </query-method>
           <ejb-ql>
               <![CDATA[SELECT OBJECT(a) FROM ArtikelGruppeBean AS a
where a.bezeichnung = ?1]]>
            </ejb-ql>  
   </query>
   </entity>
</enterprise-beans>

 

<relationships>
               <ejb-relation>
                     
<ejb-relation-name>Artikel-ArtikelGruppe</ejb-relation-name>
                       <ejb-relationship-role>
                               <ejb-relationship-role-name>
                                       Artikel haben ArtikelGruppen
                               </ejb-relationship-role-name>
                               <multiplicity>Many</multiplicity>
                               <relationship-role-source>
                                       <ejb-name>Artikel</ejb-name>
                               </relationship-role-source>
                               <cmr-field>
                                     
<cmr-field-name>ArtikelGruppe</cmr-field-name>
                                     
<cmr-field-type>java.util.Collection</cmr-field-type>
                               </cmr-field>
                       </ejb-relationship-role>
                       <ejb-relationship-role>
                               <ejb-relationship-role-name>
                                       Artikelgruppen haben Artikel
                               </ejb-relationship-role-name>
                               <multiplicity>Many</multiplicity>
                               <relationship-role-source>
                                     
<ejb-name>ArtikelGruppe</ejb-name>
                               </relationship-role-source>
                               <cmr-field>
                                     
<cmr-field-name>Artikel</cmr-field-name>
                                     
<cmr-field-type>java.util.Collection</cmr-field-type>
                               </cmr-field>
                       </ejb-relationship-role>
               </ejb-relation>
  </relationships>
</ejb-jar
Marek Lange - 22 Oct 2003 19:45 GMT
ralf sczepan schrieb:
> If I use this Code in a SessionBean (means I use the Local -
> Interfaces) it works. But I get an Exception if I try to get the
> Collection filled with Artikels.

I saw your posting in d.c.l.j.  See my comments on bean design please.

> public abstract class ArtikelBean implements EntityBean {
>         protected EntityContext ctx;
[quoted text clipped - 8 lines]
>                 this.ctx = ctx;
>         }

Looks good.

> public interface Artikel extends EJBObject  {
>     public  Integer getArtid() throws RemoteException;
[quoted text clipped - 6 lines]
> RemoteException;
> }

I assume from this that you are using a remote interface for your Entity
Beans? Please try to change to local interfaces. Accessing an entity
remotely is poor design.

> The ejb-jar.xml

Nothing seems to be wrong in your descriptor. I can't explain the
ClassCastException so far.

-marek


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.