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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:00:13+00:00 2026-06-07T10:00:13+00:00

I´m trying to map an existing database to Hibernate using annotations. The problem is

  • 0

I´m trying to map an existing database to Hibernate using annotations.
The problem is as follows.

‘organization’ and ‘organization_roles’ has a many to many relationship which is resolved in a
table ‘organization_has_roles’ using the two primary keys as a composite primary key in the join table.

Next there is a ‘users’ table. Users can have a single role in multiple organizations. This is again resolved in a join table named ‘users_has_organizations_and_role’ which has a composite primary key of the ‘user_id’, ‘organization_id’ and ‘organization_role_id’ and a unique_index on the ‘user_id’ and ‘organization_id’.

The ‘users_has_organizations_and_role’ references the ‘organization_has_roles’ composite primary key(organization_id, organization_role_id) and the users table (user_id)

I have not been able to find examples on this matter that worked.

Is it possible to map this. Which Annotations should I use and how?

I´m using Hibernate 4, although it might not matter..

  • 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-07T10:00:17+00:00Added an answer on June 7, 2026 at 10:00 am

    The main problem with this approach is that you have an unmappable ORM mismatch: you have an entity in your database (organization_has_roles) which is translated as a relationship in OO. As this relationship is not an entity, and has no @Id, it cannot be referenced into yet another entity.

    A solution is to map the database entity organization_has_roles into a JPA/Hibernate entity, with either a composite key, or with a synthetic key (whatever you prefer). You can then refer to this entity in your User model.

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

Sidebar

Related Questions

Reverse engineering an existing database to map with N-Hibernate using Fluent N-Hibernate. How can
I'm trying to map an existing mongodb database to domain objects using datanuclues (3.0.0-m6)
I have a existing parent-child relationship I am trying to map in Fluent Nhibernate:
I've been trying to map an object to a database using SQLAlchemy but have
I'm trying to set up a relationship as follows. Each Master item has one
I have an existing database. At the moment I am trying to map my
Trying to map the following schema using the Entity Framework. A Customer can have
I am trying to map a legacy database here and I'm running into a
I'm trying to map a manyToMany relationship in Fluent NHibernate and running into a
I'm having some issues with a many-to-many relationship I am trying to create. The

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.