Foreign key object as primary key
i have the following domain-class-entity:
class AccountSupplier {
static mapping = {
table 'MY_TABLE'
version false
// references-column-mapping
accountReference: column:'REFACNTID'
supplierReference column:'REFSUPID'
// primary / foreign keys
id generator: 'assigned', column: 'REFACNTID'
id generator: 'assigned', column: 'REFSUPID'
}
Account accountReference
Supplier supplierReference
static constraints = {
accountReference(insert:false, update:false, nullable:false)
supplierReference(insert:false, update:false, nullable:false)
}
}
which should image a real table in an oracle database (already existing and contains thousands of records.
when i want to start grails i get the following exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception 开发者_开发问答is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: AccountSupplier column: REFSUPID (should be mapped with insert="false" update="false")
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.codehaus.groovy.grails.commons.spring.ReloadAwareAutowireCapableBeanFactory.doCreateBean(ReloadAwareAutowireCapableBeanFactory.java:105)
What is wrong with this? do i need to modify the related entities? Cant i make a foreign key to a primary key?
Thanks
First, the usual comment: Foreign keys are not useful as primary keys. It will (and probably has already) cause you a lot of headache. If you can change it, change it.
That said: Your problem is that you added two ID generators to the entity. My guess is that Grails only supports one per entity (each entity can have only a single primary key, after all).
I think you're mixing the DB table and the mapping in your mind. The mapping doesn't use IDs. It only knows about references. That there is an ID somewhere should not concern you.
In fact, for this mapping, you should not use any ID generator. The IDs are automatically assigned when you assign instances to the fields. If you want ID generators, add them to the mappings for Account
and Supplier
.
精彩评论