Thursday, April 19, 2012

JPA @ElementCollection @Enumerated not inserting

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 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.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 values ( )


I assume I am just missing something simple.





No comments:

Post a Comment