我正在尝试使用Spring,Hibernate和JSF创建用户管理系统。我创建了自己的Users类,它实现了java.io.Serializable,org.springframework.security.core.userdetails.UserDetails 和角色班。
我的问题是UserLoginDAOImpl类的方法loadUserByName(String name)没有加载持久的角色集。
我使用Netbeans 7.3 beta IDE,Glassfish服务器3+作为网络服务器。
以下是调试快照。 Here is the Debugging snapshot
以下是hbm查询的调试跟踪:
INFO: Hibernate: select users0_.USERS_ID as USERS1_2_, users0_.USERS_USERNAME as USERS2_2_, users0_.USERS_PASSWORD as USERS3_2_, users0_.USERS_ENABLED as USERS4_2_, users0_.USERS_CREATED_DT as USERS5_2_, users0_.USERS_MODIFIED_DT as USERS6_2_, users0_.USERS_EMAIL as USERS7_2_ from ats.users users0_ where users0_.USERS_USERNAME='user'
INFO: Hibernate: select roleses0_.ROLES_USERNAME as ROLES2_1_, roleses0_.ROLES_ID as ROLES1_1_, roleses0_.ROLES_ID as ROLES1_1_0_, roleses0_.ROLES_USERNAME as ROLES2_1_0_, roleses0_.ROLES_ROLE_NAME as ROLES3_1_0_, roleses0_.ROLES_CREATED_DT as ROLES4_1_0_, roleses0_.ROLES_MODIFIED_DT as ROLES5_1_0_ from ats.roles roleses0_ where roleses0_.ROLES_USERNAME=?
用户和角色表架构:
CREATE TABLE `users` (
USERS_ID
int(11)NOT NULL AUTO_INCREMENT,
USERS_USERNAME
varchar(45)NOT NULL,
USERS_PASSWORD
varchar(255)NOT NULL,
USERS_ENABLED
varchar(5)NOT NULL,
USERS_CREATED_DT
varchar(45)NOT NULL,
USERS_MODIFIED_DT
varchar(45)DEFAULT NULL,
USERS_EMAIL
varchar(50)DEFAULT NULL,
PRIMARY KEY(USERS_ID
),
独特的钥匙USERS_USERNAME_UNIQUE
(USERS_USERNAME
)
)ENGINE = InnoDB AUTO_INCREMENT = 3 DEFAULT CHARSET = utf8 $$
CREATE TABLE `roles` (
ROLES_ID
int(11)NOT NULL AUTO_INCREMENT,
ROLES_USERNAME
varchar(45)NOT NULL,
ROLES_ROLE_NAME
varchar(45)NOT NULL,
ROLES_CREATED_DT
varchar(45)NOT NULL,
ROLES_MODIFIED_DT
varchar(45)DEFAULT NULL,
PRIMARY KEY(ROLES_ID
),
KEY FK_ROLES_ID
(ROLES_USERNAME
),
CONSTRAINT FK_ROLES_ID
FOREIGN KEY(ROLES_USERNAME
)引用users
(USERS_USERNAME
)ON更新没有操作更新无操作
)ENGINE = InnoDB AUTO_INCREMENT = 4 DEFAULT CHARSET = utf8 $$
这是我的DAOImpl类方法:
@Override
public Users loadUserByName(String name) {
if (name != null && !name.equals("")) {
Session session=hibernateTemplate.getSessionFactory().openSession();
Transaction transaction=null;
transaction=session.beginTransaction();
List<Users> user=session.createQuery("from Users where usersUsername='"+name+"'").list();
transaction.commit();
if (user.size() == 1) {
return user.get(0);
} else {
return null;
}
} else {
return null;
}
}
Users.hbl.xml
<hibernate-mapping>
<class name="com.ats.customer.model.Users" table="users" catalog="ats">
<id name="usersId" type="java.lang.Integer">
<column name="USERS_ID" />
<generator class="native" />
</id>
<property name="usersUsername" type="string">
<column name="USERS_USERNAME" length="45" not-null="true" unique="true" />
</property>
<property name="usersPassword" type="string">
<column name="USERS_PASSWORD" not-null="true" />
</property>
<property name="usersEnabled" type="string">
<column name="USERS_ENABLED" length="5" not-null="true" />
</property>
<property name="usersCreatedDt" type="string">
<column name="USERS_CREATED_DT" length="45" not-null="true" />
</property>
<property name="usersModifiedDt" type="string">
<column name="USERS_MODIFIED_DT" length="45" />
</property>
<property name="usersEmail" type="string">
<column name="USERS_EMAIL" length="50" />
</property>
<set name="roleses" table="roles" inverse="true" lazy="false" fetch="select">
<key>
<column name="ROLES_USERNAME" not-null="true" />
</key>
<one-to-many class="com.ats.customer.model.Roles" />
</set>
</class>
<sql-query name="getUserDetailsByUserNameProc">
<return alias="users" class="com.ats.customer.model.Users"/>
<![CDATA[CALL getUserDetailsByUserName(:username)]]>
</sql-query>
Roles.hbl.xml
<hibernate-mapping>
<class name="com.ats.customer.model.Roles" table="roles" catalog="ats">
<id name="rolesId" type="java.lang.Integer">
<column name="ROLES_ID" />
<generator class="native" />
</id>
<many-to-one name="users" class="com.ats.customer.model.Users" fetch="select">
<column name="ROLES_USERNAME" not-null="true" />
</many-to-one>
<property name="rolesRoleName" type="string">
<column name="ROLES_ROLE_NAME" length="45" not-null="true" />
</property>
<property name="rolesCreatedDt" type="string">
<column name="ROLES_CREATED_DT" length="45" not-null="true" />
</property>
<property name="rolesModifiedDt" type="string">
<column name="ROLES_MODIFIED_DT" length="45" />
</property>
</class>
用户类:Users.java
public class Users implements java.io.Serializable,org.springframework.security.core.userdetails.UserDetails {
private Integer usersId;
private String usersUsername;
private String usersPassword;
private String usersEnabled;
private String usersCreatedDt;
private String usersModifiedDt;
private String usersEmail;
private Set<Roles> roleses = new HashSet();
//private GrantedAuthority[] authorities = null;
public Users() {
}
public Users(String usersUsername, String usersPassword, String usersEnabled, String usersCreatedDt) {
this.usersUsername = usersUsername;
this.usersPassword = usersPassword;
this.usersEnabled = usersEnabled;
this.usersCreatedDt = usersCreatedDt;
}
public Users(String usersUsername, String usersPassword, String usersEnabled, String usersCreatedDt, String usersModifiedDt, String usersEmail, Set<Roles> roleses) {
this.usersUsername = usersUsername;
this.usersPassword = usersPassword;
this.usersEnabled = usersEnabled;
this.usersCreatedDt = usersCreatedDt;
this.usersModifiedDt = usersModifiedDt;
this.usersEmail = usersEmail;
this.roleses = roleses;
}
public Integer getUsersId() {
return this.usersId;
}
public void setUsersId(Integer usersId) {
this.usersId = usersId;
}
public String getUsersUsername() {
return this.usersUsername;
}
public void setUsersUsername(String usersUsername) {
this.usersUsername = usersUsername;
}
public String getUsersPassword() {
return this.usersPassword;
}
public void setUsersPassword(String usersPassword) {
this.usersPassword = usersPassword;
}
public String getUsersEnabled() {
return this.usersEnabled;
}
public void setUsersEnabled(String usersEnabled) {
this.usersEnabled = usersEnabled;
}
public String getUsersCreatedDt() {
return this.usersCreatedDt;
}
public void setUsersCreatedDt(String usersCreatedDt) {
this.usersCreatedDt = usersCreatedDt;
}
public String getUsersModifiedDt() {
return this.usersModifiedDt;
}
public void setUsersModifiedDt(String usersModifiedDt) {
this.usersModifiedDt = usersModifiedDt;
}
public String getUsersEmail() {
return this.usersEmail;
}
public void setUsersEmail(String usersEmail) {
this.usersEmail = usersEmail;
}
public Set<Roles> getRoleses() {
return this.roleses;
}
public void setRoleses(Set<Roles> roleses) {
this.roleses = roleses;
}
@Override
public String getPassword() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public String getUsername() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isAccountNonExpired() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isAccountNonLocked() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isCredentialsNonExpired() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isEnabled() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
角色类:Roles.java
public class Roles implements java.io.Serializable {
private Integer rolesId;
private Users users;
private String rolesRoleName;
private String rolesCreatedDt;
private String rolesModifiedDt;
public Roles() {
}
public Roles(Users users, String rolesRoleName, String rolesCreatedDt) {
this.users = users;
this.rolesRoleName = rolesRoleName;
this.rolesCreatedDt = rolesCreatedDt;
}
public Roles(Users users, String rolesRoleName, String rolesCreatedDt, String rolesModifiedDt) {
this.users = users;
this.rolesRoleName = rolesRoleName;
this.rolesCreatedDt = rolesCreatedDt;
this.rolesModifiedDt = rolesModifiedDt;
}
public Integer getRolesId() {
return this.rolesId;
}
public void setRolesId(Integer rolesId) {
this.rolesId = rolesId;
}
public Users getUsers() {
return this.users;
}
public void setUsers(Users users) {
this.users = users;
}
public String getRolesRoleName() {
return this.rolesRoleName;
}
public void setRolesRoleName(String rolesRoleName) {
this.rolesRoleName = rolesRoleName;
}
public String getRolesCreatedDt() {
return this.rolesCreatedDt;
}
public void setRolesCreatedDt(String rolesCreatedDt) {
this.rolesCreatedDt = rolesCreatedDt;
}
public String getRolesModifiedDt() {
return this.rolesModifiedDt;
}
public void setRolesModifiedDt(String rolesModifiedDt) {
this.rolesModifiedDt = rolesModifiedDt;
}
}
请告诉我如何解决此问题。
答案 0 :(得分:1)
在Dao班中尝试:
List<Users> user=
session.createQuery("from Users u where u.usersUsername= :username")
.setParameter("username", name).list();