I’m trying to map a table (legacy from the Oracle Forms time) that have only a Unique Key, because some values can be null. The user can choose if the data in other applications exists in char or number and the table have the two fields (one varchar2 and other number).
It is possible to use a Domain Class for this?
Example
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)
The unique constraint is composed by all fields except “default”.
I’ve tried this:
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')
}
}
When I try to query, only five of 56 records returns
println IntegrationSchedules.findAll().size() //prints 5 but table have 56
I tried remove the relation with OfficeSchedule, but still returns just five rows.
I noticed then that the rows that return is because they have all fields informed, that makes sense because I’m defining the key as if it were a PK.
I cannot change the table because are legacy applications that use it.
One workaround that I think is to transfor this as a groovy bean and use a service to create the objects, but with this I cannot use the criteria and GORM findBy methods.
My solution was, as mentioned by @ideasculptor, to create a hibernate mapping:
src/java/domain
And I had to create a hibernate config (hibernate.cfg.xml in conf/hibernate)
The interesting is that dynamic methods of GORM works, eg: