我使用彈簧3 +彈簧安全3 +休眠。 我有一些映射類的問題。我不知道爲什麼,但是一些類被映射了,它們可以被Hibernate使用,但同時一些(用於Spring Security)不是這樣! 論壇-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){
}
}
彙編:不受休眠映射(其他類映射)
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;
}
}
現在類:
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;
}
}
而第二類:
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;
}
}
所以,當我試圖通過用戶名從(從ForumUser UserServiceImpl.getUserByUsername())DB檢索的用戶,它拋出 org.hibernate.hql.ast.QuerySyntaxException:ForumUser是不是映射[來自ForumUser]
如果我改變你的HQL到另一個,例如「從論壇」(這是另一個類,這是工作)它會拋出另一個異常,什麼都不重要,但事實是它映射另一個類並可以檢索它。 我該如何解決這個問題?
它看起來像休眠關聯問題:請發佈主題休眠/關聯配置。 – Ralph 2011-12-17 08:18:58