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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:42:09+00:00 2026-06-18T04:42:09+00:00

I am inserting a User object with roles(existing) roles . It is giving me

  • 0

I am inserting a User object with roles(existing) roles .
It is giving me unable insert null in user_role.id

Schema:

@Entity
@Table(name = "user_data")
public class User extends Serializable {

     @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * user can have set of roles profiles
     */
    @ManyToMany(cascade = {CascadeType.ALL} ,fetch=FetchType.EAGER)
    @JoinTable(name = "user_role", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") })
    private Set<Role> roles;

}


@Entity @Table(name = "role")
public class Role extends Serializable {

    private static final long serialVersionUID = -5954247513572163065L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "name")
    private String name;

    @ManyToMany
    @JoinTable(name = "role_permission", joinColumns = { @JoinColumn(name = "role_id",
        referencedColumnName = "id") },
        inverseJoinColumns = { @JoinColumn(name = "permission_id",
        referencedColumnName = "id") })
    private Set<Permission> permissions;

    [...]

}

Now when i am trying to create user it is giving me the exception
java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("USER_ROLE"."ID")

For creating a user I am using spring repository code as below:

@Override
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public User createUser(User user) throws BusinessException {
    List<Error> errors = new ArrayList<Error>();
    validateUserRoles(user  ,errors);
    if ( errors.size() > 0 )
    {
        throw new BusinessException(errors);
    }


    user.setUserIdentifier(user.getUserIdentifier().trim());
    logger.info("Creating User with username: " + user.getUserIdentifier());
    return this.userRepositotry.save(user);
}


private void validateUserRoles(User user ,List<Error> errors) {
    Set<Role> roles = new HashSet<Role>();
    if (user.getRoles() != null) 
    {
        for(Role role : user.getRoles())
        {
            Role r = null;
            if(role.getId() != null)
            {
                r = this.roleRepository.findOne(role.getId());
            } else if( role.getName() != null ) 
            {
                r = this.roleRepository.findByName(role.getName());
            }
            if(r == null) 
            {
                errors.add(new Error(ErrorCode.INVALID_ARGUMENTS,"Invalid user Role.",r));
            } else
            {
                roles.add(r);
            }
        }
    }

    user.setRoles(roles);
}
  • 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-18T04:42:10+00:00Added an answer on June 18, 2026 at 4:42 am

    Your user_role join table has an id column, that Hibernate doesn’t know about. All hibernate knows about is the role_id and user_id columns. The id column isn’t useful, and you should remove it from the user_role table. If you leave it there, you need to make it nullable, or to make it have a default value.

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

Sidebar

Related Questions

I want to add the user in Users table with Entity Framework. I added
I am inserting a user using membership provider protected void Button3_Click(object sender, EventArgs e)
I want that as a user is inserting data into a table, he is
I am having a basic mysql stored procedure for inserting user data along with
I'm trying to prevent user from inserting * in a textbox. This is what
When inserting a row into a table with an auto increment primary key column,
Before inserting data into table i need to check against duplicate records and report
Hi Im creating a Temp table and inserting data to the Table. Im going
Me again! I have a question re. inserting into a sql table using LINQ.
I am trying to create the following scenario... A user logs in by inserting

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.