我正在尝试映射仅具有唯一键的表(Oracle Forms时间的旧版),因为某些值可以为null。用户可以选择其他应用程序中的数据是否以char或number存在,并且该表具有两个字段(一个varchar2和另一个number)。

可以使用域类吗?


Mytable
---------------------------------------------------------------
office_schedule_id        NUMBER(5) NOT NULL
office_company_char       VARCHAR2(6)
office_schedule_char      VARCHAR2(10)
office_company_num        NUMBER(6)
office_schedule_num       NUMBER(5)
default                   VARCHAR2(1)

唯一约束由“默认”以外的所有字段组成。

我已经试过了:
class OfficeSchedule {
   int officeScheduleId
   String officeScheduleName

   static mapping = {
     version false
     table 'office_schedules'
     id column: 'office_schedule_id', name: 'officeScheduleId'
   }

   static hasMany = [ schedules : IntegrationSchedules ]
}
//this represents MyTable
class IntegrationSchedules {
    String officeCompanyChar
    String officeScheduleChar
    Integer officeCompanyNum
    Integer officeScheduleNum
    String default

    static belongsTo = [ officeSchedule : OfficeSchedule ]

    int hashCode() {
      def builder = new HashCodeBuilder()
      //added all fields of the unique key
      builder.toHashCode()
    }

    static mapping = {
      version false
      table 'mytable'
      id composite: ['officeSchedule','officeCompanyChar','officeScheduleChar','officeCompanyNum','officeScheduleNum']
      officeSchedule(column:'office_schedule_id')
    }

}

当我尝试查询时,只有56条记录中的5条返回
println IntegrationSchedules.findAll().size() //prints 5 but table have 56

我尝试删除与OfficeSchedule的关系,但仍然只返回五行。

然后,我注意到返回的行是因为它们已告知所有字段,这是有道理的,因为我将键定义为好像是PK。

我不能更改表,因为有使用它的旧版应用程序。

我认为一种解决方法是将其转换为Groovy Bean并使用服务来创建对象,但是与此同时,我无法使用条件和GORM findBy方法。

最佳答案

最好的选择是浏览休眠文档(并执行google搜索)以找到一种机制来将其映射到普通休眠状态。在hibernate中找到解决方案后(为了灵活起见,hibernate付出了极大的努力,以便可以在旧数据库中使用它),然后返回gorm并尝试确定gorm是否提供相同的功能。至少,它可以允许您构造一个邮件列表查询,该查询将生成该查询不会产生的响应。有很多关于gorm的文档都没有被很好地记录,因此了解gorm DSL如何映射到休眠配置对于做出有根据的猜测如何在gorm中完成至关重要

关于java - 映射旧模型-仅具有唯一约束的表可用作域类?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10557451/

10-15 23:38