• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help on implementing one-one,many-to-many,many-to-one relationships in java  RSS feed

 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am a new to Java programming. I am reading the oops concept. I am thinking of working on a small project which involves implementing one-to-many relationship. It may further get divided into many-to-one or many-to-many.
I need help on the below:

1. Is it possible to implement one-to-many, many-to-many and many-to-one relationships in core java ?

2. If yes, what are the possible ways one can program one-to-one, many-to-one relationships using core java language ?

3. Any java tutorial with examples to help me understand the implementation of one-to-one, many-to-one relationships using core java language ?

4. Please help me on technical approach or planning required for designing any project.
For example,
I have to write backend code using core java code for a online portal, then how to decide:
  • On the number of interfaces required.
  • When to implement aggregation/composition.
  • If I need to declare an enum type.


  • Regards,

     
    S Majumder
    Ranch Hand
    Posts: 349
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Joy ,
    We can do the cone-to-one / many-to-one relation by using core java .Here is the code snippet :

    Assuming each Employee has EmployeeDetails , it could be one-to-one :


    An Employee can have multiple Accounts so it could be one-to-many relation between Employee and Account:

     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    S Majumder wrote: . . .
    Assuming each Employee has EmployeeDetails , it could be one-to-one : . . .
    Was the circular dependency there accidental?
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you can model all those types of relationships.

    I suggest the easiest way to do it is to start by drawing a diagram of the entities. You can then see what relates to what.
     
    S Majumder
    Ranch Hand
    Posts: 349
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    S Majumder wrote: . . .
    Assuming each Employee has EmployeeDetails , it could be one-to-one : . . .
    Was the circular dependency there accidental?


    Hi Campbell ,
    We can avoid the circular dependency in many ways ,, such as :

    Create Employee and EmployeeDetails class and call the setter methods of Employee and Employee4Details separately .
    We can create one proxy of Employee such as IEmpProxy and instead of creating the actual object of employee we can create the proxy in the EmployeeDetails class.

    Satya
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did you see what I meant about a circular dependency?
     
    S Majumder
    Ranch Hand
    Posts: 349
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell ,
    I'm not getting , could you elaborate a little ..

    Satya
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Employee class has an EmployeeDetail field and the EmployeeDetail class has an Employee field
     
    S Majumder
    Ranch Hand
    Posts: 349
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes true ,
    there is a chance of circular dependency , but I already told how to avoid this in my previous post .

    Satya
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!