外键对象作为主键

时间:2011-07-13 08:57:13

标签: oracle grails gorm

我有以下域类实体:

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)
}
}

应映像到oracle数据库中的真实表(已存在且包含数千条记录。

当我想开始grails时,我得到以下异常:

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)

这有什么问题?我需要修改相关实体吗?我可以为主键创建一个外键吗?

由于

1 个答案:

答案 0 :(得分:0)

首先,通常的注释:外键不能用作主键。它会(并且可能已经)引起你很多头痛。如果您可以更改它,请更改它。

那说:你的问题是你向实体添加了两个ID生成器。我的猜测是Grails只支持每个实体一个(毕竟每个实体只能有一个主键)。

我认为你正在混合数据库表和映射。映射不使用ID。它只知道参考文献。在某个地方有一个ID不应该关注你。

实际上,对于此映射,您不应使用任何ID生成器。将实例分配给字段时会自动分配ID。如果您需要ID生成器,请将它们添加到AccountSupplier的映射中。