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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:26:00+00:00 2026-05-30T21:26:00+00:00

I have the following (simple) mapping: @Entity public class Role { @OneToMany( fetch =

  • 0

I have the following (simple) mapping:

@Entity
public class Role {
   @OneToMany( fetch = FetchType.EAGER ) private Set< Privilege > privileges;
}

I want to do the following (simplified):

  • I have created a Role (R1) with a Privilege entity (P1)
  • I want to create another, new Role (R2) and give it the same privilege (P1)

So, when the new R2 is created, it’s Set of Privilege is also new (HashSet not PersistedSet) but it contains the existing P1; It seems that Hibernate is unable to recognise the fact that P1 already exists and properly persist the relation

I have already tried to following:

  • save the new Role (R2) with both entityManager.persist and entityManager.merge
  • add cascade = { CascadeType.ALL } to the mapping between Role and Privilege
  • tried to preload the Privileges (so that the entities exist in the current session) before saving the new Role

I have not tried (yet):

  • specifying the @JoinColumn( name = "PRIV_ID" ) on the mapping
  • doing more operations by hand (like first persisting the empty Role entity and them updating the Privileges) – last resort

I’m thinking that this is a pretty standard usecase, so perhaps I’m missing something that is preventing me to persist this association properly.
Any ideas?
Thanks.

  • 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-30T21:26:01+00:00Added an answer on May 30, 2026 at 9:26 pm

    If two roles may have the same privilege, then it’s not a OneToMany anymore, but a ManyToMany. So start by changing the mapping of the privileges collection.

    Then show us the code which adds P1 to the set of privileges of R2. I suspect that you’re creating a new Privilege instance, instead of getting P1 from the session.

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

Sidebar

Related Questions

I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have the following simple function: private void EnableDisable941ScheduleBButton() { if (this._uosDepositorFrequency.Value != null)
i have the following simple script <input class=input type=text name=password style=color: #797272; value= <?php
I have an entity like so: public class Land { public virtual IDictionary<string, int>
Basically I have a simple base class: public abstract class BasePoco { public virtual
I'm new to entity frame work code first. I have simple class called Cat
I have a fairly simple Many to One relationship between two classes: @Entity public
my application has the following entity: public class User { public virtual int UserID
We have the following simple Stored Procedure that runs as an overnight SQL server

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.