• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use Entity Bean Relationships  RSS feed

 
mike nu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I am new to the Entity Beans, and found that I really like it.
I was a little confused about the entity beans relationship. Can anyone give me an explanation on what's the correctly or proper way to use it?
Is there any special consideration need to be taken into when designing the database in order to use entity beans and their relationships?
Thanks a lot.
Mike
 
Sergiu Truta
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's exactly as the relations implemented inside the database. I'll give you the simplest example, about a one-to-one relationship. Let's say you have two tables inside the database: Order and Shipping. One order can have only one Shipping and one Shipping belongs only to one Order. CMR comes into picture for example when loading the Order entity. In BMP, you may choose now between lazy or aggressive loading. Lazy loading means loading a related bean only when you need that data, and aggresive loading means loading all the related data into a single transaction. With CMP lazy loading happens automatically behind the scenes.
You have to keep in mind that the relationships are set inside ejbPostCreate. For example if inside table Order you have to set OrderNumber, CustomerID and ShippingID every time you insert a new Order, you will set first two inside ejbCreate and the ShippingID inside ejbPostCreate (of course, only if you have a relation between Order and Shipment).
These are some benefits of CMR.
Read more about this in Ed Roman's "Mastering EJB 2nd Edition"
[ January 26, 2004: Message edited by: sergiu truta ]
 
Vasantha Prabha
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I too in the Learning stage of EJB.Can u tell me the importance
of ejbPostCreate in elaborate.Why u need to load the shippingID in the
ejbPostCreate.What will happen when u load it in create method itself
and I too have one more question..
Why we need to go for Entity bean.Why we can't use this statefull
session bean and look up the database..

Regards,
Sangeeth
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sangeetha Vasantha:
Can u tell me the importance of ejbPostCreate in elaborate.Why u need to load the shippingID in the ejbPostCreate. What will happen when u load it in create method itself

Use ejbCreate to create the identity of the bean -- its PK and any attributes you consider *essential* to the bean being a valid bean (name, key, etc). You set up all relationships (and any other attributes you need) in ejbPostCreate because in ejbCreate the bean has no PK and thus cannot be involved in relationships.
Originally posted by Sangeetha Vasantha:
Why we need to go for Entity bean.Why we can't use this statefull
session bean and look up the database..

Stateful session beans are tied to the client that created them. Entity beans can be accessed by many clients. As well, there is no such thing as "container-managed persistence for stateful session beans," and thus you'd be SOL unless you wanted to write your own JDBC code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!