0

最初的问题源于这个问题:

为什么这个 JPA 2.0 映射在 Eclipse/JBoss Tools 中给我一个错误?

如您所见,这个星座也吓坏了 Eclipse Dali JPA 验证器。JPA 规范似乎允许这样做,如此处所示(有点可靠):

JPA 规范是否允许引用非主键列?

第一个问题中发布的确切代码也引发了 Hibernate MappingException:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: geoareas] Unable to configure EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:374)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at tld.geoareas.Main.main(Main.java:69)
Caused by: org.hibernate.MappingException: property [_tld_geoareas_model_Zip__identifierMapper.country] not found on entity [tld.geoareas.model.Country]
    at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:378)
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:414)
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:115)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1550)
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1473)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1389)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1477)
    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:278)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:362)
    ... 4 more
Caused by: org.hibernate.MappingException: property [_tld_geoareas_model_Zip__identifierMapper.country] not found on entity [tld.geoareas.model.Country]
    at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:424)
    at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:375)
    ... 15 more

我对“递归属性”特别恼火。毕竟,它只是对 GeoAreas 子表 Country 的 UNIQUE、NOT NULL CHAR(2) 列的简单单列引用。请注意,继承在这里可能会产生一些影响,但恕我直言,它仍然不是太复杂。

那么,这是一个 Hibernate 错误吗?

4

1 回答 1

0

回答我自己的问题:

这不是 Hibernate 中的错误。JPA 2.0 规范在同时用作派生标识符时禁止引用非 PK 列。有关更多详细信息,请参阅我的其他答案:

为什么这个 JPA 2.0 映射在 Eclipse/JBoss Tools 中给我一个错误?

请注意,它也会导致 EclipseLink 的映射异常(您只会获得更有意义的堆栈跟踪):

Exception Description: The derived composite primary key attribute [country] of type [java.lang.String] from [tld.geoareas.model.ZipId] should be of the same type as its parent id field from [tld.geoareas.model.Country]. That is, it should be of type [java.lang.Integer].
    at org.eclipse.persistence.exceptions.ValidationException.invalidDerivedCompositePKAttribute(ValidationException.java:1134)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.validateDerivedPKClassId(MetadataDescriptor.java:1817)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processId(ObjectAccessor.java:450)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processOwningMappingKeys(ObjectAccessor.java:659)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ManyToOneAccessor.process(ManyToOneAccessor.java:110)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.processDerivedId(ClassAccessor.java:1409)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processDerivedId(EntityAccessor.java:823)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processAccessorsWithDerivedIDs(MetadataProject.java:1294)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage3(MetadataProject.java:1560)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:484)
    at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:453)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1080)
    ... 6 more
于 2011-04-29T08:35:01.577 回答