I am using spring 3 + spring security 3 + hibernate.
I have some problems with mapping classes. I don’t know why, but some classes are mapped they can be used by Hibernate but at the same time some (which are used for Spring Security) are not!
forum-security.xml:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
</http>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService"/>
</authentication-manager>
</beans:beans>
UserServiceImpl:
package forum.service;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import forum.domain.ForumUser;
@Service
public class UserServiceImpl implements UserService{
@Autowired private SessionFactory sessionFactory;
@Autowired private Assembler assembler;
public List<ForumUser> listAllUsers(){
return null;
}
public List<ForumUser> listUsersBySellingPont(){
return null;
}
@Transactional
public ForumUser getUserByUsername(String username){
Session session = sessionFactory.getCurrentSession();
List<ForumUser> users = session.createQuery("from ForumUser").list();
ForumUser result = null;
for(ForumUser user : users){
if(user.getUsername().equals(username))
result = user;
}
return result;
}
public void addUser(ForumUser user){
}
public void updateUser(ForumUser user){
}
public void deleteUser(Integer id){
}
}
Assembler:
package forum.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import forum.domain.ForumUser;
import forum.domain.UserDetailsImpl;
@Service("assembler")
public class Assembler {
@Transactional(readOnly = true)
UserDetailsImpl buildUserFromUserEntity(ForumUser userEntity) {
Integer id = userEntity.getId();
String username = userEntity.getUsername();
String password = userEntity.getPassword();
String email = userEntity.getEmail();
Date enabled = userEntity.getEnabled();
Date lastEntered = userEntity.getLastEntered();
Date registered = userEntity.getRegistered();
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (GrantedAuthority role : userEntity.getAuthorities()) {
authorities.add(role);
}
UserDetailsImpl user = new UserDetailsImpl();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
user.setEnabled(enabled);
user.setAuthorities(authorities);
user.setLastEntered(lastEntered);
user.setRegistered(registered);
return user;
}
}
And now classes that are not mapped by hibernate (other classes are mapped):
package forum.domain;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.FetchType;
import javax.persistence.CascadeType;
import org.springframework.security.core.GrantedAuthority;
@Entity
@Table(name="users")
public class ForumUser {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name="email")
private String email;
@Column(name="registered")
private Date registered;
@Column(name="lastEntered")
private Date lastEntered;
@Column(name="enabled")
private Date enabled;
@OneToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY,mappedBy="forumUser")
private List<GrantedAuthority> authorities;
public Date getEnabled(){
return enabled;
}
public void setEnabled(Date enabled){
this.enabled = enabled;
}
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public Date getRegistered(){
return registered;
}
public void setRegistered(Date registered){
this.registered = registered;
}
public Date getLastEntered(){
return lastEntered;
}
public void setLastEntered(Date lastEntered){
this.lastEntered = lastEntered;
}
public List<GrantedAuthority> getAuthorities() {
return authorities;
}
public void setAuthorities(List<GrantedAuthority> authorities){
this.authorities = authorities;
}
}
And the second class:
package forum.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import org.springframework.security.core.GrantedAuthority;
@Entity
@Table(name="authorities")
public class Authority implements GrantedAuthority{
@ManyToOne(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY)
@JoinColumn(name="userId")
private ForumUser forumUser;
@Column(name="authority")
private String authority;
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
}
So when I am trying to retrieve an user by username from DB (UserServiceImpl.getUserByUsername() from ForumUser), it throws
org.hibernate.hql.ast.QuerySyntaxException: ForumUser is not mapped [from ForumUser]
And if I change thir HQL to another, for example “from Forum” (it is another class, that is working) it will throw another exception, doesn’t really matter what exactly, but the fact is that it mappes another class and can retrieve it.
How can I solve this problem?
Oh, I have found a solution! It all was because of my own inattention. I have forgotten to add new classes to a list of annotated persistent classes in my conf.