使用 Grails 访问没有域类的数据库

发布于 2024-10-06 10:23:14 字数 344 浏览 6 评论 0原文

我在尝试将 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 技术交流群。

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

发布评论

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

评论(2

擦肩而过的背影 2024-10-13 10:23:14

您可以非常轻松地访问数据库,出于性能原因,我们在某些情况下也会这样做:

class SomeService {
    def dataSource;

    def nativeAccessMethod = {
        def sql = new Sql(dataSource);
        def rows = sql.rows("select * from myTable");
        /* processing continues ...*/
    }
}

Groovy 的 本机 SQL 支持< /a> 也不错。

You can access the database quite easily, we are doing the same in certain cases for performance reasons:

class SomeService {
    def dataSource;

    def nativeAccessMethod = {
        def sql = new Sql(dataSource);
        def rows = sql.rows("select * from myTable");
        /* processing continues ...*/
    }
}

Groovy's native SQL support is also nice.

泪冰清 2024-10-13 10:23:14

没有要求主键太长,这只是 Hibernate 和 Grails 的标准。您可以将唯一的 varchar 列视为具有如下所示的域类的主键:

class Person {

   String username
   String firstName
   String lastName

   static mapping = {
      id name: 'username', generator: 'assigned'
      version false
   }
}

这适用于由此 DDL 定义的表:

create table person (
   username varchar(255) not null,
   first_name varchar(255) not null,
   last_name varchar(255) not null,
   primary key (username)
);

我添加了“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:

class Person {

   String username
   String firstName
   String lastName

   static mapping = {
      id name: 'username', generator: 'assigned'
      version false
   }
}

This works for a table defined by this DDL:

create table person (
   username varchar(255) not null,
   first_name varchar(255) not null,
   last_name varchar(255) not null,
   primary key (username)
);

I added 'version false' since it's a legacy system and you probably don't have a 'version' optimistic locking column.

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