hibernate - Grails 命名 id 列错误

标签 hibernate postgresql grails identifier quoting

我的领域类有问题。每次我尝试插入条目时都会出现此错误:

The column name "Sequence_No" was not found in this ResultSet.

这是我的领域类:

class Record {

String firstName
String middleName
String lastName
String motherMaidenLastName
String motherMaidenMiddleName
Date birthDate
String examTypeCode
Date examinationDate

static constraints = {
    firstName nullable:false, blank:false, maxSize: 50
    middleName nullable:true, blank:true, maxSize: 50
    lastName nullable:false, blank:false, maxSize: 50
    motherMaidenLastName nullable:true, blank:true, maxSize: 50
    motherMaidenMiddleName nullable:true, blank:true, maxSize: 50
    birthDate  nullable: false
    examTypeCode  nullable: false, maxSize: 4
    examinationDate  nullable: false
}

static mapping  = {
    datasource 'oevs'
    table '`elig`'
    id column: '`Sequence_No`'
    firstName column: '`FirstName`'
    middleName column: '`MiddleName`'
    lastName column: '`LastName`'
    motherMaidenLastName column: '`MotherMaidenLastName`'
    motherMaidenMiddleName column: '`MotherMaidenMiddleName`'
    birthDate column: '`Date_Birth`'
    examTypeCode column: '`ExamType`'
    examinationDate column: '`Date_Exam`'
    }
}

但我试图保存的条目已插入到数据库中,它只是抛出该错误。我正在使用 PostgreSQL。我被要求以这种方式命名列名。这就是为什么我需要在命名我的列时放置一个反引号字符,因为如果不这样做,PostgreSQL 会自动将其全部转换为小写字母。每次我删除 id 列中的反引号字符时,错误都不会出现,但显然列名已转换为小写。

最佳答案

错误在于这个语句:

That's why I need to place a back tick character in naming my columns because PostgreSQL automatically converts it all to lowercase letters if don't do that.

反引号 (``) 仅在 MySQL 中使用(毫无意义地违反了 SQL 标准 - 您可以使用 SET sql_mode = 'ANSI'; 来解决这个问题)。反引号对 PostgreSQL 或标准 SQL 没有特殊意义。在 PostgreSQL 中使用 双引号("")(实际上是在任何地方)来实现您的目标。更多详细信息,请参阅 this related answer .

您几乎是对的:在 PostgreSQL 中,不是双引号 的标识符被强制转换为小写。阅读章节Identifiers and Key Words在手册中了解详细信息。

我通常建议不要在 PostgreSQL 中完全使用 CamelCase 标识符。有很多方法可以解决这个问题。困惑的用户的绝望问题不断出现在这里。如果可以,不要使用 CamelCase 并且永远不要使用双引号标识符。一旦你创建了一个非标准的标识符,你必须在任何时候都用双引号引起来......

关于hibernate - Grails 命名 id 列错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13486261/

相关文章:

javax.servlet.ServletException : Filter execution threw an exception java. lang.StackOverflowError Struts

java - @GenerateValue 不适用于非 id 字段

sql - 仅使用具有 `in` 子句中所有值的列连接查询

database - Grails 2.4.3:管理变更的架构

tomcat - Grails WAR 部署问题

java - 如何在jsp页面上动态显示数据库中的图像

java - 对 Hibernate(和 JPA)标准的表达能力的限制?

postgresql - Postgres 如何在不将其复制到数据库的情况下使用 csv 文件

node.js - NodeJS,pg-promise 和数组作为参数

grails - 如何在 Grails 中指定默认事务隔离级别