I've got a handful of classes (simplified) in grails 2.5.5:
class Solution implements Serializable {
long id
String name
static hasOne = [
entity : SolutionEntity
]
static mapping = {
datasource 'companion'
id generator:'sequence', params:[sequence: 'SOLUTION_SEQ']
entity cascade: 'delete'
}
static constraints = {
name nullable: true, blank:false, unique: true
entity nullable: true
}
}
class SolutionEntity implements Serializable {
Solution solution
Long externalEntityId
private Object externalEntity
void setExternalEntity(def value){
externalEntityId = value.id
}
static transients = ["externalEntity"]
static constraints = {
solution nullable: false, unique: true
externalEntityId nullable: false
}
static mapping = {
datasource 'companion'
id generator:'sequence', params:[sequence: 'SOLUTION_ENTITY_SEQ']
tablePerHierarchy true
}
}
class SolutionPPB extends SolutionEntity implements Serializable {
PPB getPPB(){
if(!externalEntity && externalEntityId) {
externalEntity = PPB.get(externalEntityId)
}
externalEntity
}
static constraints = {
}
}
where the basic idea is that I want to associate each Solution
with a SolutionEntity
, which may be of different types (I've shown PPB
here). This is based on Burt Beckwith's recommendation because the PPB
are in different datasources. I'm able to create and modify Solution
entries as I like, there doesn't seem to be any problem there. However, when I try to create a SolutionEntity
object like this:
def link = SolutionPPB.findOrCreateBySolution(solution)
link.setExternalEntity(entity)
link.save(failOnError: false)
I get the exception:
org.springframework.orm.hibernate4.HibernateSystemException: Unknown entity: myapp.Solution; nested exception is org.hibernate.MappingException: Unknown entity: myapp.Solution
I can't figure out what I'm doing wrong. It seems surprising that Solution
is unknown since I didn't have any trouble making the Solution
before trying to link it.