jQuery in Action, 2nd edition
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
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply 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: 299
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: 3041
    
    4

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: 299
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: 3041
    
    4

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: 299
Is this what you're looking for? If I read what you said previously correctly.

H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 299
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: 299
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unidirectional mandatory one-to-one: mandatory RI aspect.
 
Similar Threads
Convert application to object oriented application
ManyToMany relationship with extra field in the joinTable
non-static variable this cannot be referenced from a static context
Inner Classes
Generic TreeSet