File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes CMR relationship prob. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "CMR relationship prob." Watch "CMR relationship prob." New topic
Author

CMR relationship prob.

Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
Dear All,

I am new to EJB and trying to learn CMR relationships


Suppose I have one master table
TABLE NAME : ORDER_HEADER
OrderNo number(5,0) not null,
CustNo number(5,0) not null,
OrderDate Date not null,

constraint pk_order_header primary key ( OrderNo,CustNo,OrderDate)



And Detail Table

TABLE NAME: ORDER_LINE

OrderNo number(5,0) not null,
CustNo number(5,0) not null,
OrderDate Date not null,
LineNo number(5,0) not null,
ItemNo number(5,0)
Qty number(5,0)
Rate number(7,2),
constraint pk_order_detail primary key (OrderNo,CustNo,OrderDate,LineNo),
constraint fk_order_detail_order_header FOREIGN KEY (OrderNo,CustNo,OrderDate) REFERENCES OrderHeader (OrderNo,CustNo,OrderDate)


Now I want write cmr relationship for these two tables using entity beans

which one of following is correct

1)
public abstract class OrderHeaderBean implements EntityBean {
private EntityContext ctx;

/********* CMP ***********/
public abstract Integer getOrderNo();
public abstract void setOrderNo(Integer orderNo);
public abstract Integer getCustNo();
public abstract void setCustNo(Integer custNo);
public abstract long getInvDate();
public abstract void setInvDate(long invDate)


/********* CMR ***********/

public abstract java.util.Collection orderLines getOrderLines();
public abstract void setOrderLines(java.util.Collection orderLines);

............................

...........................


public abstract class OrderLineBean implements EntityBean {
private EntityContext ctx;

/********* CMP ***********/
public abstract Integer getOrderNo();
public abstract void setOrderNo(Integer orderNo);
public abstract Integer getCustNo();
public abstract void setCustNo(Integer custNo);
public abstract long getInvDate();
public abstract void setInvDate(long invDate);
public abstract Integer getLineNo();
public abstract void setLineNo(Integer lineNo);
public abstract Integer getItemNo();
public abstract void setItemNo(Integer itemNo);
public abstract Integer getQty();
public abstract void setQty(Integer qty);
public abstract float getRate();
public abstract void setRate(float rate);



/********* CMR ***********/

public abstract OrderHeaderLocal orderLines getOrderHeader();
public abstract void setOrderHeader(OrderHeaderLocal orderHeader);

.....................
.....................


}


2)
public abstract class OrderHeaderBean implements EntityBean {
private EntityContext ctx;

/********* CMP ***********/
public abstract Integer getOrderNo();
public abstract void setOrderNo(Integer orderNo);
public abstract Integer getCustNo();
public abstract void setCustNo(Integer custNo);
public abstract long getInvDate();
public abstract void setInvDate(long invDate)


/********* CMR ***********/

public abstract java.util.Collection orderLines getOrderLines();
public abstract void setOrderLines(java.util.Collection orderLines);

............................

...........................


public abstract class OrderLineBean implements EntityBean {
private EntityContext ctx;

/********* CMP ***********/
public abstract Integer getLineNo();
public abstract void setLineNo(Integer lineNo);
public abstract Integer getItemNo();
public abstract void setItemNo(Integer itemNo);
public abstract Integer getQty();
public abstract void setQty(Integer qty);
public abstract float getRate();
public abstract void setRate(float rate);

/********* CMR ***********/

public abstract java.util.Collection orderLines getOrderLines();
public abstract void setOrderLines(java.util.Collection orderLines);

............................

...........................


Sandeep
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

If you want to hear my suggestion :
simplify you database design, it is horrible.
it is just MHO.
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
Suppose if you are working on maintenance project which is developed in other technology and now is to be redeveloped in n-tier architecture but you are not allowed to change anything in database ( its a fact with me ) then?? and what changes you want me to do in database design ?
Rahul kapoor
Greenhorn

Joined: Sep 21, 2004
Posts: 16
I agree. Your DB design is not normalized. Your line item table need not refer to customer and order date in line items table. They are part of Order master table and a column like order_id in master table could be referred as foreign key in line items table.

Regarding the methods in beans, it depends on the direction of relationships that exist between order and line items. If you want to know the lineitems for a particular order, then the OrderEJB should have a method to get the getLineItems(..). The method will return you a collection of line items for that particular order.
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
Ok let me put it in other way.

Suppose I have one table Table_A with composite primary key( column1, column2) I have another table Table_B with composite foregin key (column1,column2) references Table_A(column1,column2).

Now if

EJB_B {
public abstract DATATYPE getColumn1();
public abstract void setColumn1(DATATYPE value);
public abstract DATATYPE getColumn2();
public abstract void setColumn2(DATATYPE value);

public abstract Collection getBs();
public abstract void setBs(Collection Bs)


}

is right or


EJB_B {

public abstract Collection getBs();
public abstract void setBs(Collection Bs)


}
Rahul kapoor
Greenhorn

Joined: Sep 21, 2004
Posts: 16
What kind of OO relationship exists between A and B ?
[ October 06, 2004: Message edited by: Rahul kapoor ]
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
I dont know. Honestly I dont know OO Designs. But have you understood what I am trying to say?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Rajesh Thankare:
Suppose I have one table Table_A with composite primary key( column1, column2) I have another table Table_B with composite foregin key (column1,column2) references Table_A(column1,column2).

So you have a one-to-many relationship (though that could be one-to-one) from A to B. If you want it unidirectional from A to B, then you'll need the following accessors:You also need to create the CustomPK class to hold the two columns. The container, via CMR, will manage the foreign keys in bean B for you -- that's the CM in CMR.
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
Do I need to have getter and setter methods for individual columns of Key ( Primary or foreign ) of just getter and setter method for composite primary key and getter and setter methods for CMR is enough ???

Suppose PK is (Column1,Coulmn2),

are these methods necessary ???
public abstract DATATYPE getCloumn1();
public abstract void setColumn1(DATATYPE value);

or

just

public abstract CustomPK getPK();
public abstract void setPK(CustomPK pk);

is enough.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Rajesh Thankare:
Do I need to have getter and setter methods for individual columns of Key ( Primary or foreign ) of just getter and setter method for composite primary key and getter and setter methods for CMR is enough ???

Actually, I believe you need individual field accessors -- not a single PK field accessor. I've never used multi-column PKs in entity beans, though, but when using XDoclet you specify each column in the PK and tag it with @ejb.pk-field.
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
David Thank you very much for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CMR relationship prob.