本文介绍了休眠:将引用映射到子表的备用/自然键列时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始问题源自于此问题:

The original question stems from this question:

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

如您所见,这个星座也怪异了Eclipse Dali JPA验证器. JPA规范似乎允许这样做(有点可靠):

As you can see this constellation also freaks out the Eclipse Dali JPA validator. The JPA spec seems to allow this as seen here (somewhat reliable):

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

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

The exact code posted in the first question also raises a 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子表Countrys的UNIQUE,NOT NULL CHAR(2)列的简单单列引用.请注意,继承可能会在这里产生一些影响,但它仍然不是IMHO过于复杂.

I'm especially irritated by the "recursive property". After all, it's just a simple single-column reference to a UNIQUE, NOT NULL CHAR(2) column of a GeoAreas sub table Countries. Note the inheritance might have some impact here, but it's still not overly complicated IMHO.

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

So, is this a Hibernate bug?

推荐答案

回答我自己的问题:

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

It's not a bug in Hibernate. It's the JPA 2.0 spec that prohibits references to non-PK columns when used as a derived identifier at the same time. See my other answer here for more details:

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

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

Note it's also causing a mapping exception with EclipseLink (you just get a more meaningful stack trace):

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

这篇关于休眠:将引用映射到子表的备用/自然键列时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:17