grails 中使用 javaDB 和 Hibernate 的用户表

发布于 2024-08-29 06:48:56 字数 437 浏览 12 评论 0原文

让我们看看我是否可以以一种可以理解的方式问这个问题......

我从 grails 开始并创建了一个名为 user 的域类。据我了解,Hibernate用于将这个领域类映射到数据库。这与 hsqldb 配合得很好。

现在我尝试切换到 javaDB 并收到错误消息,因为该表称为“user”(这似乎是 javaDB 的保留字)。

因此,类似的语句

create table user ...

将导致错误消息。

create table "user" ...

有效,但 Hibernate 似乎没有将表名放在引号中。

如何配置 Hibernate 使用引号以使其与我的表名一起使用?

PS:是的,我知道,我可以将域类映射到另一个表名称...:-)

let's see if I can ask this in an understandable way...

I started with grails and created a domain class called user. As far as I understand, Hibernate is used to map this domain class to the database. This works pretty fine with hsqldb.

Now I tried to switch to javaDB and get an error message because the table is called "user" (which seems to be a reserved word for javaDB).

So a statement like

create table user ...

will result in an error message.

create table "user" ...

works, but Hibernate seems not put put the table name in quotes.

How can I configure Hibernate to use quotes in order to make it work with my table name?

PS: yes, I know, I could map the domain class to another table name... :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷→糖丶微凉 2024-09-05 06:48:56

您是否尝试用 double 包装 名称引用:

class User {
  ..
  static mapping = {
      table '"user"'
  }
}

更新: 这样做的后果之一是您还必须使用 joinTable 关键字。这听起来很合理,但约定是 Grails 的好处之一,不依赖约定在某种程度上违背了它的理念。我个人会避免在这里使用保留字(即不是user)。

Did you try to wrap the table name with double quotes:

class User {
  ..
  static mapping = {
      table '"user"'
  }
}

Update: One of the consequence of this is that you'll have to customize the name of join tables too using the joinTable keyword. This sounds reasonable but conventions are one of the benefits of Grails and not relying on them goes somehow against its philosophy. I would personally just avoid using a reserved word (i.e. not user) here.

瑾夏年华 2024-09-05 06:48:56

知道了。
正如 Pascal 已经提到的,映射条目是关键。但不是使用双引号,反勾会带来成功:

static mapping = {
  table "`user"
}

这告诉 hibernate 在生成 sql 时将表名放在引号中。
它似乎也适用于所有相应的表...但不知何故它删除了其中一些表中的“user”的最后一个字符...:-(

所以看来确实将域类重新映射到表名,这不会导致任何问题都将是最好的方法:

static mapping = {
  table "appuser"
}

got it.
As Pascal already mentioned, the mapping entry is the key. But instead of using double quotes, a back tick will bring success:

static mapping = {
  table "`user"
}

this tells hibernate to put the table name in quotes when generating the sql.
It seems also to work with all corresponding tables... but somehow it drops the last character of "user" in some of them... :-(

So it seems that indeed remapping the domain class to a table name which will not cause any problems will be the best way:

static mapping = {
  table "appuser"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文