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

  • SEARCH
  • Home
  • 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 8717431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:18:18+00:00 2026-06-13T06:18:18+00:00

I want do a one-to-many relationship between two tables using a join table. This

  • 0

I want do a one-to-many relationship between two tables using a join table.

This is why I want to use a join table:

  • Hibernate unidirectional one to many association – why is a join table better?
  • Why is it recommended to avoid unidirectional one-to-many association on a foreign key?

Finally, I want to use Hibernate annotations to perform this.

I found some examples to do this using xml mapping but nothing with annotations.

I believe this would be how the tables need to be created

CREATE TABLE `PRODUCT` (
  `PRODUCT_ID`      BIGINT       PRIMARY KEY AUTO_INCREMENT
);

CREATE TABLE `PARTS` (
  `PART_ID`         BIGINT       PRIMARY KEY AUTO_INCREMENT
);

-- USER_IMAGE_ID must be unique if we want a one-to-many relationship between PRODUCTS & PARTS tables
CREATE TABLE `USER_DETAILS_IMAGE` (
  `PRODUCT_ID`      BIGINT,
  `PART_ID`         BIGINT UNIQUE,
  CONSTRAINT `FK_PRODUCT_ID`  FOREIGN KEY (`PRODUCT_ID`)   REFERENCES `PRODUCT`(`PRODUCT_ID`),
  CONSTRAINT `FK_PART_ID`     FOREIGN KEY (`PART_ID`)      REFERENCES `PARTS`(`PART_ID`)
);
  • 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-06-13T06:18:19+00:00Added an answer on June 13, 2026 at 6:18 am

    Don’t look for examples. Read the official documentation:

    @Entity
    public class Product {
    
        private String serialNumber;
        private Set<Part> parts = new HashSet<Part>();
    
        @Id
        public String getSerialNumber() { return serialNumber; }
        void setSerialNumber(String sn) { serialNumber = sn; }
    
        @OneToMany
        @JoinTable(
                name="PRODUCT_PARTS",
                joinColumns = @JoinColumn( name="PRODUCT_ID"),
                inverseJoinColumns = @JoinColumn( name="PART_ID")
        )
        public Set<Part> getParts() { return parts; }
        void setParts(Set parts) { this.parts = parts; }
    }
    
    
    @Entity
    public class Part {
       ...
    }
    

    Also, note that this is the default for unidirectional one-to-many associations. So you don’t even have to provide the @JoinTable annotation if the default table and column names suit you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an optional many-to-one relationship between two classes. Hibernate translates the property to
I have a one-to-many relationship between two tables: table1: NUMBER users_id (primary key) field2
I have a one to many relationship between two tables in mysql. One called
I want to use many bars like this one in my app (i don't
I have a problem :( I have a many-many table between two tables(1&2), via
I have a one to many entity relationship between two entities: EntityP (Parent) <-->>
This is a question about Hibernate's generated sql about deleting one relationship under many-to-many
I've got two entities with a one-to-many relationship between them. The entity that holds
I have a 1 to many relationship between two tables... say Organisation and Members
Suppose i have a one to many relationship between two model entities Entity One

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.