你好我有域类用户和我创建自己的控制器来注册人。当我运行项目并输入字段并按回车时我得到此错误:
org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not extract ResultSet; bad SQL grammar [n/a]; nested exception is
org.postgresql.util.PSQLException: ERROR: column this_.id does not exist
我连接到postgresql。我所有其他域类在数据库中都有自己的表,但域类没有我认为是我收到此错误的原因,我该如何解决?
class User {
String login
String password
String name
static constraints = {
login size: 3..20 , unique: true, nullable: false
password size: 3..20, unique: false, nullable: false
name size: 3..20, unique: false, nullable: false
}
}
class UserController {
def scaffold = User
def login = {}
def authenticate = {
def user = User.findByLoginAndPassword(params.login, params.password)
if(user){
session.user = user
flash.message = "Hello ${user.name}!"
redirect(controller:"entry", action:"list")
}else{
flash.message = "Sorry, ${params.login}. Please try again."
redirect(action:"login")
}
}
def logout = {
flash.message = "Goodbye ${session.user.name}"
session.user = null
redirect(controller:"entry", action:"list")
}
}
class RegisterUserController {
def index() { }
def register(String user,String password,String name) {
def user2=new User(login: user,password : password,name: name)
if (user2.validate() && user2.save() ) {
redirect(url: "http://localhost:8080")
} else {
}
}
}
我收到错误:if(user2.validate()&& user2.save())
application.yml文件:
hibernate:
cache:
queries: false
use_second_level_cache: true
use_query_cache: false
region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
dataSource:
pooled: true
jmxExport: true
driverClassName: org.postgresql.Driver
username: postgres
password:
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:postgresql://localhost:5432/new
username: postgres
password:
test:
dataSource:
dbCreate: update
url: jdbc:postgresql://localhost:5432/new
username: postgres
password:
production:
dataSource:
dbCreate: update
url: jdbc:postgresql://localhost:5432/new
username: postgres
password:
答案 0 :(得分:0)
无需调用validate(),因为save()已经调用它。
试试这个
if (user2.save(flush:true, failOnError: true) ) {
redirect(url: "http://localhost:8080")
}
使用failOnError,您可以更加确定错误。
此外,如果这无助于尝试更改您的班级名称 “用户”为其他东西。 “MYUSER” 我曾经在postgresql中遇到过这个问题,如果我不记得错误,更改类名会解决它。