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 7020989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:23:26+00:00 2026-05-27T23:23:26+00:00

I have a question similar to @ManyToMany without join table (legacy database) with an

  • 0

I have a question similar to @ManyToMany without join table (legacy database) with an additional issue.

I have two tables A and B

  • A with a multiple column primary key (ID and ID2)
  • B with a multiple column primary key (ID and ID3)

An row in A can reference several rows in B (B.ID = A.ID) and a row in B can be referenced by several rows in A.

EDIT: the database is a read-only legacy database that I cannot change. I do not need to map the relationships with JPA (I could just do it in my program logic with additional selects) but it would be nice.

It is basically a many-to-many relationship without a join table. Since, as for the linked question, I just have to read the tables, I tried with two one-to-many relationships in both classes.

The additional problem that I have is that both IDs used for the join are not the primary key.

I have the following classes:

@Entity
@Table( name = "A" )
@IdClass( PrimaryKeysA.class )
public class A {

    @Id
    @Column( name = "ID", insertable = false, updatable = false, columnDefinition = "char" )
    private String id;

    @Id
    @Column( name = "ID2", insertable = false, updatable = false )
    private int id2;

    @OneToMany( cascade = CascadeType.ALL )
    @JoinColumn( name = "ID", columnDefinition = "char", referencedColumnName = "ID" )
    private Set< B > setOfBs;

}

@Entity
@Table( name = "B" )
@IdClass( PrimaryKeysB.class )
public class B {

    @Id
    @Column( name = "ID", insertable = false, updatable = false, columnDefinition = "char" )
    private String id;

    @Id
    @Column( name = "ID3", insertable = false, updatable = false )
    private int id3;

    @OneToMany( cascade = CascadeType.ALL )
    @JoinColumn( name = "ID", columnDefinition = "char", referencedColumnName = "ID" )
    private Set< A > setOfAs;

}

Hibernate generates the following error:

Exception while preparing the app : referencedColumnNames(ID) of package.B referencing package.A not mapped to a single property

I don’t really get the message: B.id is referencing a single property in A (A.id).

EDIT: as requested:

public class PrimaryKeysA implements Serializable {

private static final long   serialVersionUID    = 1L;

private int    id1;
private int    id2;

    // getters/setters/equals/hashcode

}

PrimaryKeysB is similar with id3 instead of id2. Both classes A and B are simplified (anonymized) examples.

  • 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-27T23:23:27+00:00Added an answer on May 27, 2026 at 11:23 pm

    You could create a view that would act as join table:

    CREATE VIEW AJOINB AS
    SELECT A.ID as AID, A.ID2 as AID2, B.ID as BID, B.ID3 as BID3
    FROM A JOIN B ON A.ID = B.ID
    

    And then map it in JPA as a ManyToMany with AJOINB as join table.

    If A.ID2 and B.ID3 were unique by themselves, you wouldn’t even need to map A.ID and B.ID in your JPA beans.

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

Sidebar

Related Questions

I have a similar question in the forum. Change baud rate in C# without
I have a question similar to the one here: Event handlers inside a Javascript
I have a similar question to this one SQL products/productsales I want to do
I have asked similar question for Linux RPM ( Adding License Agreement in RPM
I have a similar question than django cross-site reverse . But i think I
I have asked a similar question before, but I didn't have a firm grasp
I know I have already answered a similar question ( Running Batch File in
(Please note that I have seen a similar question on StackOverflow recently, however I
I made a similar question a few days ago, but now I have new
Similar to this question I have a custom subclass of UITableViewCell that has a

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.