Scienze  
Syllabus ItalianoSyllabus IngleseindexlogoutArea Personale UNITN
Sistemi distribuiti: design Forum del Corso Messaggi del Thread
 
Sistemi distribuiti: design - Forum del Corso
Messaggi del Thread

Autore Messaggio
stefano.schivo
Thread
  Post Postato: 31 gennaio 2005
   Titolo: Doubt with Entity Bean relations
 

I have a little problem concerning Entity Bean relations, JBuilder and design patterns ...

Suppose we have two Entity Beans:
   - name : Fagiolo
       fields :
             . id [Integer]
             . username [String]
             . password [String]

   - name : Fagiolino  
       fields :
             . id [Integer]
             . name [String]

Then we want them to have a relation like "Fagiolo has many Fagiolinos". So we use JBuilder to add such relation. We have set the tool so that it creates a new table containing the association between the two Beans, and the result corresponds to the expectations.

Then we tell the nice tool to create also DTO, SessionFaçade and BusinessDelegate for each of the two Beans (it creates also an useful ServiceLocator class).
Then we look into the method setFagioloFromFagioloDto(Fagiolo, FagioloDto) and we discover that the only fields it cares about are username and password!  
It does NOT set in any part the Fagiolinos associated with the current Fagiolo. If we run a sample client with these settings (and hoping someone is actually thinking about setting the missing fields ) we see that all works, except the relation: in fact, the table containing the relation is never accessed! So, even if we told the application to associate a Fagiolino with our precious Fagiolo, it does NOT follow our instructions.  

To resolve this issue, I went to the setFagioloFromFagioloDto method and changed it adding a  few lines that do these steps:
    - use ServiceLocator to find a FagiolinoHome
    - for all FagiolinoDtos contained into the FagioloDto object, find the corresponding Fagiolinos using Home interface and add them to a Vector
    - set the resulting Vector as the Collection of Fagiolinos for the Fagiolo we are updating


Doing so, it all works fine..
My question is: does this break in some way the design patterns? I mean, the FagioloSessionFaçade accesses not only Fagiolo, but also Fagiolino...
I was also wondering if there is any other way to get the same results...

Thank you
afogarolli
  Post Postato: 31 gennaio 2005
   Titolo: Re: Doubt with Entity Bean relations
 

Hi!
I've never used jbuilder tools for generatin ejb code but I'll try to answer to your questions....
1.
....
Then we tell the nice tool to create also DTO, SessionFaçade and BusinessDelegate for each of the two Beans (it creates also an useful ServiceLocator class).
Then we look into the method setFagioloFromFagioloDto(Fagiolo, FagioloDto) and we discover that the only fields it cares about are username and password!  
It does NOT set in any part the Fagiolinos associated with the current Fagiolo. 
.....
Probably I just guess you have told Jbuilder to use an auto-incrementing primary key so the ejbCreate method that you got doesn't prompt you for the ID.


.....
If we run a sample client with these settings (and hoping someone is actually thinking about setting the missing fields ) we see that all works, except the relation: in fact, the table containing the relation is never accessed! So, even if we told the application to associate a Fagiolino with our precious Fagiolo, it does NOT follow our instructions.  
............

if you have created the relation in the right way you should be able to access the relationship field as a property. Let's say that we called the relation between fagiolo and fagioliono: fagiolinoRel. In FagioloBean you should have a field called fagiolinoRel in order to access the field of fagiolino (ex.name) you don't need to lookup fagiolinoHome but just using the property synatx: fagiolo.fagiolinoRel.name (navigation of relationship).

...

The relationship is configured in the ejb-jar.xml and in jboss-cmp-jdbc.xml 
If you have a 1 to 1 relation you don't need any support table, the fagiolino.id will be the fk and it will be the same as fagiolo.id.
For the 1 to n or n to n relationship you have to use an intermediary table.
Maybe something in your code generation went wrong or you mistake in accessing the relation.
If you cannot solve the problem try to send me the code I'll take a look at that.
Good luck.
stefano.schivo
  Post Postato: 31 gennaio 2005
   Titolo: Re: Re: Doubt with Entity Bean relations
 

afogarolli wrote:

Probably I just guess you have told Jbuilder to use an auto-incrementing primary key so the ejbCreate method that you got doesn't prompt you for the ID.


No no, the primary key has not to be set because it is the same as the one from the FagioloDto by hypothesis.. The problem is about the field related to the Fagiolinos associated with the current Fagiolo..


afogarolli wrote:

if you have created the relation in the right way you should be able to access the relationship field as a property. Let's say that we called the relation between fagiolo and fagioliono: fagiolinoRel. In FagioloBean you should have a field called fagiolinoRel in order to access the field of fagiolino (ex.name) you don't need to lookup fagiolinoHome but just using the property synatx: fagiolo.fagiolinoRel.name (navigation of relationship).


The fact is that the 'fagiolinoRel' is actually a Collection of Fagiolinos (and not FagiolinoDtos): so I have to "transform" all the FagiolinoDtos into Fagiolinos in order to set them properly into the Fagiolo.. And for this reason I use FagiolinoHome


afogarolli wrote:

The relationship is configured in the ejb-jar.xml and in jboss-cmp-jdbc.xml 
If you have a 1 to 1 relation you don't need any support table, the fagiolino.id will be the fk and it will be the same as fagiolo.id.
For the 1 to n or n to n relationship you have to use an intermediary table.
Maybe something in your code generation went wrong or you mistake in accessing the relation.


Yes, I know that a support table is not needed (it is a 1 to n relationship). I was only experimenting, because also the other way didn't set the proper fields..


afogarolli wrote:

If you cannot solve the problem try to send me the code I'll take a look at that.
Good luck.


Thank you
afogarolli
  Post Postato: 31 gennaio 2005
   Titolo: Re: Re: Re: Doubt with Entity Bean relations
 

....
The fact is that the 'fagiolinoRel' is actually a Collection of Fagiolinos (and not FagiolinoDtos): so I have to "transform" all the FagiolinoDtos into Fagiolinos in order to set them properly into the Fagiolo.. And for this reason I use FagiolinoHome
...
I'm not sure if I understood the problem right but..
if you iterate the collection than you can cast the Object to Fagiolino and than access its properties....

Anyway if I missed the point send me the code I'm glad to help you out.
stefano.schivo
  Post Postato: 2 febbraio 2005
   Titolo: Re: Doubt with Entity Bean relations
 

Ok, no problem.. I managed to understand that I have to use a further field (say, idFagiolo) in the Fagiolino Bean: this field is the missing link to Fagiolo! Now everything works properly!

Thank you anyway
afogarolli
  Post Postato: 2 febbraio 2005
   Titolo: Re: Doubt with Entity Bean relations
 

if you want to create a n-m relationship you should have this kind of situation:
Fagiolo
       fields :
             . idFagiolo [Integer]
             . username [String]
             . password [String]

Fagiolino   
       fields :
             . idFagiolino [Integer]
             . name [String

FagioloFagiolinoTable
       fields:
             .idFagiolo
             . idFagiolino

Otherwise for a 1-1 relationship you don't need any intermediary table and you will just have this situation (on db):
Fagiolo
       fields :
             . idFagiolo [Integer]
             . username [String]
             . password [String]
Fagiolino   
       fields :
             . idFagiolino [Integer]
             . name [String

In your FagioloBean you have to write a relationship field (not a CMP field) for accessing the FagioloFagiolinoRel that you alerady have defined on descriptors.

Any other interpretation of relationship implementation is wrong.
Hope it's clear.
Bye
 
Progetto eLeaf
Contact eLeaf team