• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

many to many relationship

 
Greenhorn
Posts: 5
Eclipse IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The system seems to be inconsistent to manage the customer details and address details in the same class. So how it would be if we split the class into two classes called the 'Customer' and 'Address' and just by using both of their  id to uniquely identify each other by placing the customer id in the address class and address id in the customer class so that we can map many addresses with customer and vice versa?

Let us seek how we can do that. First of all the customer details are collected along with the address id and address details are collected along with customer id so that they can be interrelated easily.
Sample Input and Output 1:
[All text in bold are input and the remaining are output]


Enter the customer details
Enter the  id :
1
Enter the name :
Shri
Enter the email :
shri@gmail.com
Enter the purpose :
Commercial
Enter the other customer details :
NIL
Enter address id :
1


Do you want to enter more ?
YES


Enter the  id :
1
Enter the name :
Shri
Enter the email :
shri@gmail.com
Enter the purpose :
domestic
Enter the other customer details :
NIL
Enter address id :
2


Do you want to enter more ?
NO


Enter the  address details
Enter the address id :
1
Enter the address line :
34,la villa
Enter the city name :
montreal
Enter the zipcode :
123455
Enter the country id:
1
Enter the state name
o hio
Enter the customer id :
1
Do you want to enter more ?
YES


Enter the address id :
2
Enter the address line :
36,kk flats
Enter the city name :
Boston
Enter the zipcode :
789456
Enter the country id:
2
Enter the state name
MA
Enter the customer id :
2
Do you want to enter more ?
NO


Enter the customer id :
1
Address Line :34,la villa
City :montreal
State hio
Country Code:1
Zipcode :123455
Address Line :36,kk flats
City :Boston
State :MA
Country Code:2
Zipcode :789456
Enter the address id :
1
Name :Shri
Email :shri@gmail.com
Purpose :Commercial
Other details :NIL
Name :Shri
Email :shri@gmail.com
Purpose :domestic
Other details :NIL


Sample Input and Output 2:
[All text in bold are input and the remaining are output]

Enter the customer details
Enter the  id :
1
Enter the name :
George
Enter the email :
geo123@gmail.com
Enter the purpose :
Commercial
Enter the other customer details :
NIL
Enter address id :
1


Do you want to enter more ?
YES


Enter the  id :
2
Enter the name :
Vincent
Enter the email :
via@gmail.com
Enter the purpose :
domestic
Enter the other customer details :
NIL
Enter address id :
2


Do you want to enter more ?
NO


Enter the  address details
Enter the address id :
1
Enter the address line :
34,la villa
Enter the city name :
MAL
Enter the zipcode :
789456
Enter the country id:
2
Enter the state name
MA
Enter the customer id :
1
Do you want to enter more ?
YES


Enter the address id :
2
Enter the address line :
123,viagram villa
Enter the city name :
vietnam city
Enter the zipcode :
123456
Enter the country id:
34
Enter the state name
LA
Enter the customer id :
2
Do you want to enter more ?
NO


Enter the customer id :
3
Customer with id 3 not found !
Address information does not exist for this customer yet !
Enter the address id :
3
Address with id 3 not found !
Customer information does not exist for this customer yet !



Please do help me with code am half way in it... but still i couldn't sort out the problem...int he second half...
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what is your question exactly?

From your setup (assuming it's database), you do have a) Customer table with customer ID as primary key and address ID as foreign key; b) Address table with address ID as primary key, customer ID as foreign key.

Customer 1---M Address

Now how are you setting up your classes? Composition or using the foreign key or neither?







 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And this isn't many-to-many (if a customer has an address id and an address has a customer id).
That's a one-to-one.
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:And this isn't many-to-many (if a customer has an address id and an address has a customer id).
That's a one-to-one.



That composition can be changed to List<Address> or for foreign key List<BigInteger>

Edit: wait that's just one-to-many

To OP: Actually thinking about it can many customer ID have many addresses?

For every customer ID has many addresses clearly make more sense. Flipping around 1 address can have many customer ID ... don't make that much sense (of course unless these are family members)



 
Sheriff
Posts: 17687
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Flipping around 1 address can have many customer ID ... don't make that much sense (of course unless these are family members)


If you search for multiple companies sharing same address you'll see that it's not an uncommon occurrence.
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True true given the customer can be a company
 
Hey! You're stepping on my hand! Help me tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic