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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:13:55+00:00 2026-05-28T08:13:55+00:00

I am trying to save an user. The user entity and group entity has

  • 0

I am trying to save an user. The user entity and group entity has a bi-directional many-to-many relationship. However the groups are already saved in the database (only two though: administrator and user).

Here I just “fake” the group which already exists because I thought that would work. I can’t add the cascade = CascadeType.PERSIST to the user because then it would save the group again, causing duplicate data. How would I fix this?

From the EJB

public void persistAsUser(User user) {
    Group group = new Group(2L, "user");
    user.addGroup(group);

    userRepository.persist(user);
}

javax.ejb.EJBException: Transaction aborted

Caused by: java.lang.IllegalStateException: During synchronization a
new object was found through a relationship that was not marked
cascade PERSIST:


Here are the entities:

    @Entity
    @Table(name = "app_user")
    public class User implements Serializable {

        public static final String ALL = "User.all";

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String firstName;
        private String lastName;
        private Integer age;
        private String username;
        private String email;
        private String password;

        @ManyToMany(mappedBy = "users")
        private List<Group> groups;

        ....

public void addGroup(Group group) {

    if (!getGroups().contains(group)) {
        getGroups().add(group);
    }

    if (!group.getUsers().contains(this)) {
        group.getUsers().add(this);
    }
}
}

and

@Entity
    public class Group implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String groupName;

        @ManyToMany
        @JoinTable(joinColumns = {@JoinColumn(name="group_id")}, inverseJoinColumns = {@JoinColumn(name="user_id")})
        private List<User> users;

       ....

}
  • 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-28T08:13:56+00:00Added an answer on May 28, 2026 at 8:13 am

    This issue usually happens when

    • you have bidirectional associations that are not populated in both sides
    • you have a managed (User) entity referencing an unmanaged one (Group).

    If you don’t want to persist Group, just set groups to null, as per in my comment.

    However, if you want to associate the User to the Group, one option would be

    1. load the Group you want to add the user to
    2. Add the User to the Group and viceversa.
    3. merge the Group

    Remember to mark the Group -> User relationship as CascadeType.MERGE

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

Sidebar

Related Questions

I'm having hard time trying to figure out how to auto-save user data in
I'm trying to save the site that a user came from when they sign
I am trying to save a custom object as a user setting in a
I'm trying to save a CFPropertyList to a location in the user's home folder.
I am trying to control admin item entry where non-super user accounts can't save
Im trying to save a bitmap jpg format with a specified encoding quality. However
I am trying to save data to a database on a button push, but
I'm trying to save a PDF file to SQL Server and I already have
I am trying to wrap my head around Entity Groups in Google AppEngine. I
I'm trying to save a model like so: $this->User->save( array( 'User' => array( 'id'

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.