This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Unidirectional mandatory one-to-one: mandatory RI aspect. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unidirectional mandatory one-to-one: mandatory RI aspect." Watch "Unidirectional mandatory one-to-one: mandatory RI aspect." New topic
Author

Unidirectional mandatory one-to-one: mandatory RI aspect.

H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
1. Basically, I want to understand the "Referential Integrity" aspect (like database RI) via Java code
2. I set up a Unidirectional mandatory one-to-one: Invoice---->BillOfLading
3. For "mandatory" aspect, I reenforce it at the constructor Invoice and set level setBillOfLading
4. For testing:

Invoice invoice=new Invoice(new BillOfLading());
Invoice invoice2=new Invoice(new BillOfLading());
invoice.setBillOfLading(invoice2.getBillOfLading());
Assert.assertEquals(invoice.getBillOfLading(), invoice2.getBillOfLading());

In this case 2 different invoices point to the same BillOfLading which is many-to-one.
Where do I miss the reenforcement for this case? OR Am I doing something wrong in the first place?


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You will have to enforce that relationship yourself through code. You might change your requiredCheck method to something like this:

Then write the checkAndAssignInvoice() method on billOfLading which keeps track of the Invoice objects which call the method, and makes sure only one ever does.


Steve
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
1. I changed checkAndAssignInvoice to IsAlreadyAssigned so that I can see it from my end/head.


2. The updated BillOfLading is below.



3. (Test passed.)

If I translated correctly what you have in mind, would it be OK that BillOfLading have a hidden reference to Invoice? (I do not what is the correct wording is.)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

That is what I would expect. One change I would make is to check if the Invoice being passed into IsAlreadyAssigned is the same one already assigned to this.invoice. If the passed in Invoice is the same as the stored Invoice then the One Invoice per BillOfLading relationship is maintained, and there is no need to fail the check.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
Is this what you're looking for? If I read what you said previously correctly.

H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
1. Thanks Steve.

2. Is there any code template (or standard) to handle RI via Java?
OR it comes with experience on handling these RI?


3. Just a side note:

H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
Steve,

Just a side note again, when I put these 2 to have a UML class diagram, a bi-directional from BillOfLading to Invoice is
show up. This is unexpected side effect.

For a good laugh: If such thing like @UMLTransient so that I can put on

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unidirectional mandatory one-to-one: mandatory RI aspect.