Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you're looking for? If I read what you said previously correctly.

 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic