How to configure Hibernate to put quotes around table names

前端 未结 3 1729
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 14:39

I\'ve got a situation where I\'m trying to create a table called \'user\' in Postgres, which throws an error due to Hibernate not putting table names in quotes:



        
相关标签:
3条回答
  • 2020-12-06 15:02

    This answer might help you: https://stackoverflow.com/a/3611916/947357 It shows how to quote table names in Hibernate and in JPA.

    0 讨论(0)
  • 2020-12-06 15:07

    You can quote the table name in the mapping block with backticks. Hibernate will convert those to whatever the quoting approach is for the database based on the Dialect:

    static mapping = {
        table "`user`"
    }
    

    You can also just rename the table to something else that doesn't require quoting/escaping, e.g.

    static mapping = {
        table "users"
    }
    
    0 讨论(0)
  • 2020-12-06 15:14

    Assuming you're follwing the same tutorial as I do ("Getting started with Grails" by InfoQ) you've already understood it doesn't provide any coverage to PostgreSQL. Based on this post:

    1. "User" is a reserved word in Postgres.
    2. The mapping should be added into the class itself (it's not absolutely obvious):
    class User { 
       String username 
       String password 
       ... 
       static mapping = { 
          table 'users' 
          password column: '`password`' 
       } 
    }
    

    or

    class User { 
       String username 
       String password 
       ... 
       static mapping = { 
          table '`user`' 
          password column: '`password`' 
       } 
    }
    
    0 讨论(0)
提交回复
热议问题