2016-06-19 38 views
-1

我正经历着一个弹簧休眠例子 http://websystique.com/spring-security/spring-security-4-remember-me-example-with-hibernate/优势采用枚举

嗨,这里笔者使用枚举为它的默认初始化(感谢JB Nizet理解这一点)。但是如果没有初始化,它会正常工作。那么使用这个枚举还有什么其他的好处吗?使用枚举

@Entity 
@Table(name="USER_PROFILE") 
public class UserProfile { 

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

    @Column(name="TYPE", length=15, unique=true, nullable=false) 
    private String type = UserProfileType.USER.getUserProfileType(); 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 


    @Override 
    public int hashCode() { 
    //hash code implementaion 
    } 

    @Override 
    public boolean equals(Object obj) { 
     //equals implementation 
    } 

    @Override 
    public String toString() { 
     return "UserProfile [id=" + id + ", type=" + type + "]"; 
    } 


} 

代码枚举实现

public enum UserProfileType { 
    USER("USER"), 
    DBA("DBA"), 
    ADMIN("ADMIN"); 

    String userProfileType; 

    private UserProfileType(String userProfileType){ 
     this.userProfileType = userProfileType; 
    } 

    public String getUserProfileType(){ 
     return userProfileType; 
    } 

} 

(我原来的问题是通话是如何伸手getUserProfileType功能,以及如何枚举得到initialized.Thanks JB Nizet您先前的评论。 )

回答

0

填充数据库的数据库脚本包含

INSERT INTO APP_USER_USER_PROFILE (user_id, user_profile_id) 
SELECT user.id, profile.id FROM app_user user, user_profile profile 
where user.sso_id='sam' and profile.type='ADMIN'; 

So Sam有一个UserProfile,这个配置文件的类型是'ADMIN'。

UserProfile实体中的字段type被映射到表user_profile的列type

+0

谢谢JB Nizet的快速回复。我在这个问题上做了一些改变,以明确我的疑问。你可否请经过它。 – jaison

+0

我不知道你的附录是什么意思。我不知道你是如何在数据库中定义TESTTYPE列的,你在这个列中存储了什么值,如何定义getTestUserProfileType()以及你正在调试什么。 –

+0

我改变了db数据 - > INSERT INTO USER_PROFILE(type)VALUES('ADMIN'); 至 - > INSERT INTO USER_PROFILE(type,testtype)VALUES('ADMIN','TESTADMIN'); 我简单地在枚举定义 公共字符串getTestUserProfileType()中使用的getter/setter为testUserProfileType {返回testUserProfileType;} 公共无效setTestUserProfileType(字符串testUserProfileType){this.testUserProfileType = testUserProfileType;} 我试图在getTestUserProfileType – jaison