I'm trying out Spring Boot (latest version, using Hibernate 4.3.7) and I have a problem with my User
entity. Here it is (most important part of it):
@Entity
@Table("usr")
public class User {
public static enum Role {
UNVERIFIED, BLOCKED, ADMIN
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
@ElementCollection
private Set<Role> roles = new HashSet<Role>();
(rest of properties, getters and setters etc)
}
I am also using Spring Boot
JPA repositories to save my entities:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(String email);
}
The problem is that when I add some Roles
to roles
set, Hibernate won't save it. It will create reference table, but it will only insert data to User
table.
I tried to work this problem out, so I created pure Java
+ Hibernate
project and copied my User
class into it. Guess what? It worked!
Fun fact is that when I use pure Hibernate
on my second project, created roles
table looks different that the one created in my Spring Boot
project.
On my clean Hibernate
project I have table like:
User_roles:
User_Id bigInt(20)
roles int(11)
While using Spring JPA, I got
user_roles (notice lower case)
User (no "_id" part)
roles
What's going on? What I am doing wrong? Is it related to Spring Boot
configuration? Thanks.