【发布时间】:2011-06-15 20:47:01
【问题描述】:
我有一个非常奇怪的问题。我已经在我的 Grails 1.3.6 应用程序中安装了 spring-security-core 1.0.1 并根据教程对其进行了配置 - 表是按顺序创建并由 BootStrap.groovy 填充的。我使用的是 PostgreSQL 8.4 数据库——整个过程都在我的本地主机上运行。现在,引导程序完美运行,但是当我尝试登录时,我得到一个异常,上面写着
org.hibernate.exception.SQLGrammarException: could not execute query
再往下:
Caused by: org.postgresql.util.PSQLException: ERROR: Column »username« does not exist
失败的查询如下:
select
authrole0_.id as id1_,
authrole0_.version as version1_,
authrole0_.authority as authority1_
from
auth_role authrole0_
where
username=?
这没关系,因为 auth_role 表不应该有用户名列。但是为什么 Hibernate 需要一个用户名列,而它不应该是一个呢?
关于如何解决这个问题的任何线索?
我尝试了两种不同的休眠方言,但没有任何效果。我注意到表的映射有点奇怪 - 查找表也映射了。不幸的是,它在插件的文档中说我不应该更改它,因为插件需要该类。
我的课是这样的:
class AuthUser {
String username
String password
boolean active
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static mapping = {
cache true
username column: '`username`'
password column: '`password`'
}
Set<AuthRole> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
}
import java.util.Set;
class AuthRole {
String authority
static constraints = {
authority blank: false, unique: true
}
}
import org.apache.commons.lang.builder.HashCodeBuilder
class UserRole implements Serializable {
AuthRole role
AuthUser user
boolean equals(other) {
if (!(other instanceof UserRole)) {
return false
}
other.user?.id == user?.id &&
other.role?.id == role?.id
}
int hashCode() {
def builder = new HashCodeBuilder()
if (role) builder.append(role.id)
if (user) builder.append(user.id)
builder.toHashCode()
}
static UserRole get(long roleId, long userId) {
find 'from UserRole where role.id=:roleId and user.id=:userId',
[roleId: roleId, userId: userId]
}
static UserRole create(AuthRole role, AuthUser user, boolean flush = false) {
new UserRole(role: role, user: user).save(flush: flush, insert: true)
}
static boolean remove(AuthRole role, AuthUser user, boolean flush = false) {
UserRole instance = UserRole.findByRoleAndUser(role, user)
instance ? instance.delete(flush: flush) : false
}
static void removeAll(AuthRole role) {
executeUpdate 'DELETE FROM UserRole WHERE role=:role', [role: role]
}
static void removeAll(AuthUser user) {
executeUpdate 'DELETE FROM UserRole WHERE user=:user', [user: user]
}
static mapping = {
id composite: ['user', 'role']
version false
}
}
它们几乎就是插件创建它们的方式。
谢谢, 人
【问题讨论】:
-
出于某种原因(也许我已经编辑了它)我在我的 Config.groovy 中有错误的类。而不是: grails.plugins.springsecurity.authority.className = 'auth.AuthRole' 我有: grails.plugins.springsecurity.authority.className = 'auth.AuthUser' 修复后我得到以下异常:2011-01-26 15:35:47,077 [http-9000-1] 错误 [/aquaWell].[default] - Servlet.service() 用于 servlet 默认抛出异常 groovy.lang.MissingPropertyException:没有这样的属性:为类启用:auth.AuthUser
标签: hibernate postgresql grails spring-security