1

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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
ex3v
  • 3,518
  • 4
  • 33
  • 55
  • `@Column` seems wrong. Try removing it. Also try moving that enum to its own file. The naming strategy can be changed via setting `spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy` – bekce Jan 20 '17 at 06:53
  • Possible duplicate of [Hibernate field naming issue with Spring Boot (naming strategy)](https://stackoverflow.com/questions/36451620/hibernate-field-naming-issue-with-spring-boot-naming-strategy) – Jens Schauder Jun 20 '18 at 07:45

2 Answers2

0

The following should match your existing tables.

@Entity
@Table("usr")
public class User {

    public static enum Role {
        UNVERIFIED, BLOCKED, ADMIN
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ElementCollection 
    @CollectionTable(name = "User_roles", joinColumns = @JoinColumn(name = "User_Id")
    private Set<Role> roles = new HashSet<Role>();

    (rest of properties, getters and setters etc)
}
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • Thanks for your input, but my problem is not matching existing tables, but different behavior or pure hibernate vs Spring Data JPA + saving related Enum set – ex3v Dec 23 '14 at 12:55
0

My Solution:

  • Role.java

    public enum Role {

    USER, ADMIN

    }

  • User.java

    @Entity

    @Table("usr")

    public class User {

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private long id;

    @ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)

    @JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "id"))

    @Column(name = "roles", nullable = false)

    @Enumerated(EnumType.STRING)

    private Set roles;

    (rest of properties, getters and setters etc)

    }

William Li
  • 41
  • 1
  • 5