Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA @ElementCollection @Enumerated not inserting
    primarykey
    data
    text
    <p>I am trying to implement basic user/roles</p> <p>A user can have zero to many roles.</p> <pre><code>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&lt;Role&gt; roles = new HashSet&lt;Role&gt;(); @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&lt;Role&gt; getRoles() { return roles; } public void setRoles(Set&lt;Role&gt; roles) { this.roles = roles; } public void addRole(Role role) { roles.add(role); } } </code></pre> <p>My Unit Test</p> <pre><code>@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); } </code></pre> <p>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.</p> <p>I can see in the console, that only a single insert is being done, on the user</p> <pre><code>Hibernate: insert into USERS (INDIVIDUAL_ID) values ( ? ) Hibernate: insert into INDIVIDUALS values ( ) </code></pre> <p>I assume I am just missing something simple.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload