Reputation: 31
I have an abstract Event
class which holds properties shared by all Event types and beneath it I have two subclasses with one table per class (MasterEvent
and ParentEvent
).
The problem is that in MasterEvent
the id column is of type "number" and in the ParentEvent
it is of type "varchar2".
This means that when I try to run the app I get :
Caused by HibernateException: Wrong column type in * for column event_id. Found: number, expected: varchar2(255 char).
Note that this is a legacy database, so there's no chance in changing the column types on the database level.
The sample code below should help in understanding better:
Event.groovy
package a
abstract class Event {
String id
static mapping = {
tablePerHierarchy "false"
id column: "id"
}
}
ParentEvent.groovy
package a
class ParentEvent extends Event {
static mapping = {
id column: "id"
}
}
MasterEvent.groovy
package a
class MasterEvent extends Event {
static mapping = {
id column: "id", type: "number"
}
}
I have tried putting type: number
all sorts of combinations, but it's always giving me the same error.
Is there anyway to either cast the sqlType
directly to String
or have grails ignore this validation?
Upvotes: 3
Views: 1444
Reputation: 3881
You could try a custom Hibernate UserType which takes the number id column and converts it to a String. This would then go in the MasterEvent class:
static mapping = {
id column: "id", type: NumberToStringType
}
Upvotes: 0
Reputation: 3904
type: 'number'
does not work because it is not a hibernate basic type
Try changing it to type: 'long'
(or whatever fits your legacy DB)
Also see the docs: Link
Edit after your comment
I don't know what you are doing in your abstract class, but perhaps you can do something like that and use the getter in your abstract class (not tested!):
class MasterEvent extends Event {
Long longId
static mapping = {
longId column: "id"
}
@Override
String getId() {
longId?.toString()
}
}
Upvotes: 1
Reputation: 2363
Did you try to define id field in subclasses? "String id" in Parent event and "long id" in Master event?
Upvotes: 0