简体   繁体   中英

Hibernate with Spring Security

I have well working Spring Security app which is using Oracle DB. And now I'm having 2 tables (according to this tutorial): users and user_roles .

SQL> DESCRIBE user_roles;

 ----------------------------------------- -------- ----------------------------
 USER_ROLE_ID                              NOT NULL NUMBER
 USERNAME                                  NOT NULL VARCHAR2(100)
 ROLE                                      NOT NULL VARCHAR2(100)

SQL> DESCRIBE users;

 ----------------------------------------- -------- ----------------------------
 USERNAME                                  NOT NULL VARCHAR2(100)
 PASSWORD                                  NOT NULL VARCHAR2(100)
 ENABLED                                   NOT NULL NUMBER(1)

As you can see there is no ID field in users table. Two tables binded with username . Also Hibernate entity requires field with @Id annotation. And now the main question:

Is it ok to mark not integer variable as an @Id ?

This is my entity:

@Entity
@Table(name = "Users")
public class Users {

    @Id
    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "enabled")
    private int enabled;

    //getters and setters

}

As mentioned, yes it is possible.

More importantly the tutorial you are using is rather bad (having attempted it myself a few times long time ago) in comparison to others available.

A good code example that you will learn a lot from (as did I) can be found here:

http://fruzenshtein.com/spring-mvc-security-mysql-hibernate/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM