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

  • Home
  • SEARCH
  • 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 7851919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:17:41+00:00 2026-06-02T19:17:41+00:00

I am trying to implement basic user/roles A user can have zero to many

  • 0

I am trying to implement basic user/roles

A user can have zero to many roles.

public enum Role {
  ROLE_USER,
  ROLE_ADMIN;
}

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

  private static final long serialVersionUID = 2936736450886451500L;
  private Long id;
  private Individual individual;
  private Set<Role> roles = new HashSet<Role>();

  @Id
  @Column(name = "ID")
  @GeneratedValue
  public Long getId() {
    return id;
  }

  @SuppressWarnings("unused")
  private void setId(Long id) {
    this.id = id;
  }

  @ElementCollection(targetClass=Role.class)
  @JoinTable(name = "USER_ROLES", joinColumns = @JoinColumn(name = "USER_ID"))
  @Enumerated(EnumType.STRING)
  @Column(name = "role", nullable = false)
  public Set<Role> getRoles() {
    return roles;
  }

  public void setRoles(Set<Role> roles) {
    this.roles = roles;
  }

  public void addRole(Role role) {
    roles.add(role);
  }

}

My Unit Test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/META-INF/spring/resources/resource-context.xml", "classpath:/META-INF/spring/services/persistence-context.xml"})
public class UserDaoJpaImplTest {

@Autowired
UserDao userDao;

@Transactional
@Test
public void testCreate() {
    User user = new User();
    user.setIndividual(new Individual());
    user.addRole(Role.ROLE_USER);
    user.addRole(Role.ROLE_ADMIN);
    userDao.create(user);

    Assert.assertNotSame(user.getId(), 0);
}

the problem is, that jpa (backed by hibernate), is not doing the insert on the USER_ROLES table (it is creating it correctly, with the correct composite PK (ROLE, USER_ID), and FK to user.

I can see in the console, that only a single insert is being done, on the user

Hibernate: insert into USERS (INDIVIDUAL_ID) values ( ? )
Hibernate: insert into INDIVIDUALS values ( )

I assume I am just missing something simple.

  • 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-02T19:17:43+00:00Added an answer on June 2, 2026 at 7:17 pm

    Elements of collection are inserted into database during flush of persistence context. By default SpringJUnit4ClassRunner triggers a rollback after @Transactional test, therefore automatic flush upon transaction commit doesn’t happen.

    You need to override this behaviour with @Rollback(false) or @TransactionConfiguraton(defaultRollback = false), or just trigger an explicit flush using flush() method.

    Also if you need to override default properties of collection table you should use @CollectionTable, not @JoinTable.

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

Sidebar

Related Questions

I am trying to implement performance testing on ActiveMQ, so have setup a basic
I'm trying to implement a basic popup window that asks the user if they
I have a basic MVC 2 beta app where I am trying to implement
I'm trying to implement basic auditing for a system where users can login, change
I'm fairly new to Rails and I'm trying to implement a really basic user
I am trying to implement a simple API where a user can dictate the
I'm trying to use SQLAlchemy to implement a basic users-groups model where users can
I'm trying to implement a basic jQuery infinite carousel. As much for the learning
General use case I am trying to implement a basic shell. Description I need
I am trying to implement a simple code tester : a very basic version

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.