Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6922815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:27:19+00:00 2026-05-27T10:27:19+00:00

I have quite common problem, but no idea how to solve it in practice

  • 0

I have quite common problem, but no idea how to solve it in practice and explain my choice/solution.
I have a situation when there are two tables in the database: Account and Contact. They both represent the same data, because one person (Account) can have in contact list only those people, who also have accounts in the created system. So it is classic one to many relationship between table Account and Contact.
I chose to use such model, that there are two tables instead of one, self-referencing. I am not sure if I chose well.
Here is how these tables look like:

CREATE TABLE account (
    id INT NOT NULL AUTO_INCREMENT,
    login VARCHAR(20) NOT NULL,
    firstName VARCHAR(15) NOT NULL,
    lastName VARCHAR(40) NOT NULL,
    passwordHash CHAR(50) NOT NULL,
    PRIMARY KEY(id));
CREATE TABLE contact (
    ownerid INT NOT NULL,
    contactid INT NOT NULL,
    PRIMARY KEY (ownerid, contactid),
    FOREIGN KEY (ownerid) REFERENCES account(id),
    FOREIGN KEY (contactid) REFERENCES account(id));

My problem is that I don’t know how to construct entity classes using annotations. I probably might have used some IDE wizards, but it is not the solution for me, because I could not even check whether everything is correct. I would rather prefer understand topic fully.
My aim is to have situation such that every Account possesses list of contacts. When I remove contact from this list, account is not removed from Account table.
I have read something about inverse and cascade, but unfortunately I don’t feel this.

Please show me correct path to solve this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:27:20+00:00Added an answer on May 27, 2026 at 10:27 am

    The additional table is just a join table for a many-to-many relationship between Account (as a person/owner) and Account (as a contact/owned). A person owns many contacts, and a contact is owned by several persons.

    So you should have a unique entity with the following association:

    @Entity
    public class Account {
        // ...
    
        @ManyToMany
        @JoinTable(name = "contact",
                   joinColumns = @JoinColumn(name="ownerid"),
                   inverseJoinColumns = @JoinColumn(name="contactid")
        private Set<Account> contacts;
    }
    

    You can also make it bidirectional:

    @Entity
    public class Account {
        // ...
    
        @ManyToMany
        @JoinTable(name = "contact",
                   joinColumns = @JoinColumn(name="ownerid"),
                   inverseJoinColumns = @JoinColumn(name="contactid")
        private Set<Account> contacts;
    
        @ManyToMany(mappedBy = "contacts")
        private Set<Account> owners;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a problem that I think is quite common but I have
I guess my problem is quite common, so there must be an easy solution.
I have a quite common problem, as I saw in the various user groups
I am sure this a pretty common problem: I have a UITableView with quite
I have a supposedly common problem to solve (done easily with other grid controls
It's quite common to have a form with a checkbox stating Use foo immediately
In a typical business application it is quite common to have forms that are
I have quite a problem concerning the use of relational database concepts in Delphi
I guess it's quite a common problem in databinding scenarios. What do you usually
This seems to be quite a common theme and a few people have given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.