使用 Grails 访问没有域类的数据库
我在尝试将 Grails 应用程序与 AS400/DB2 数据库组合在一起时遇到了问题。我无法映射大多数文件,因为它们没有可用作 ID 的唯一字段。即使它们这样做,它们也是基于文本的字段,而不是可以转换为长类型的格式。 (我不明白为什么 PK 必须是长数据类型?如果你想为 pk 提供一个序列或 AI,这是有意义的,但如果你只需要一个唯一的密钥怎么办?我在这里遗漏了什么吗?)
我想知道是否可以保留我设置的数据源并使用它直接 SQL 访问数据库而无需使用域对象?
我看到的是将域对象设置为瞬态。但我不知道如果没有 id 字段,您是否仍然可以做类似的事情。有人知道这是如何运作的吗?
有什么想法吗?
谢谢, 乔恩
I've ran into an issue while trying to put together a Grails app with an AS400/DB2 database. I cannot get most of the files mapped because they do not have a unique field to use as an id. And even if they do they are a text based field and not in a format that could be converted to a long type. (I don't get why the PK has to be a long data type? If you wanted to us a sequence or AI for the pk that would make sense but what if you just needed a unique key? Am I missing something here?)
I'm wondering if it is possible to keep the datasource that I have set up and just use it for straight SQL access to the DB without having to use domain objects?
Something I've seen was setting the domain object as transient. But I don't know if you could still do something like that without an id field. Anybody know how that works?
Any ideas?
Thanks,
Jon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以非常轻松地访问数据库,出于性能原因,我们在某些情况下也会这样做:
Groovy 的 本机 SQL 支持< /a> 也不错。
You can access the database quite easily, we are doing the same in certain cases for performance reasons:
Groovy's native SQL support is also nice.
没有要求主键太长,这只是 Hibernate 和 Grails 的标准。您可以将唯一的 varchar 列视为具有如下所示的域类的主键:
这适用于由此 DDL 定义的表:
我添加了“version false”,因为它是遗留系统,并且您可能没有“version” ' 乐观锁定列。
There's no requirement that a primary key be long, it's just the standard for Hibernate and Grails. You can treat a varchar column that's unique as the primary key with a domain class like this:
This works for a table defined by this DDL:
I added 'version false' since it's a legacy system and you probably don't have a 'version' optimistic locking column.