gpt4 book ai didi

java - JPA(Hibernate、EclipseLink)映射 : why doesn't this code work (chain of 2 relationships using JPA 2. 0,@EmbeddedId 复合 PK-FK)?

转载 作者:搜寻专家 更新时间:2023-10-31 20:30:55 25 4
gpt4 key购买 nike

我有三个表:

CREATE TABLE PostAddresses
(
contact_id INTEGER NOT NULL,
ordinal_nbr SMALLINT NOT NULL,
PRIMARY KEY (contact_id, ordinal_nbr)
);

CREATE TABLE Foos
(
contact_id INTEGER NOT NULL,
ordinal_nbr SMALLINT NOT NULL,
PRIMARY KEY (contact_id, ordinal_nbr),
FOREIGN KEY (contact_id, ordinal_nbr) REFERENCES PostAddresses (contact_id, ordinal_nbr)
);

CREATE TABLE Bars
(
contact_id INTEGER NOT NULL,
ordinal_nbr SMALLINT NOT NULL,
numba INTEGER NOT NULL,
PRIMARY KEY (contact_id, ordinal_nbr, numba),
FOREIGN KEY (contact_id, ordinal_nbr) REFERENCES Foos (contact_id, ordinal_nbr)
);

简单的逻辑:Bars -> Foos -> PostAddresses all by (contact_id, ordinal_nbr),不管它是什么意思。

这里是六个实体和各自的复合键类。

@Entity
@Table(name = "PostAddresses")
public class PostAddress implements Serializable
{
@EmbeddedId
private PostAddressId embeddedId;

...
}

@Embeddable
public class PostAddressId implements Serializable
{
@Column(name = "contact_id")
private Integer contactId;

@Column(name = "ordinal_nbr")
private Integer ordinalNbr = 1;

...
}

@Entity
@Table(name = "Foos")
public class Foo implements Serializable
{
@EmbeddedId
private FooId embeddedId;

@MapsId(value = "postAddressId")
@OneToOne
@JoinColumns(value = {@JoinColumn(name = "contact_id", referencedColumnName = "contact_id"), @JoinColumn(name = "ordinal_nbr", referencedColumnName = "ordinal_nbr")})
private PostAddress postAddress = null;

...
}

@Embeddable
public class FooId implements Serializable
{
@Embedded
private PostAddressId postAddressId; //just one field!

...
}

@Entity
@Table(name = "Bars")
public class Bar implements Serializable
{
@EmbeddedId
private BarId embeddedId;

...
}

@Embeddable
public class BarId implements Serializable
{
@Embedded
private FooId fooId;

@Column(name = "numba")
private Integer numba;

...
}

这真的没什么特别的,Bar 引用 Foo,Foo 引用 PostAddress,都是通过复合键类。由于外键是复合的并且是 PK 的一部分,因此我必须将 ID 类嵌套到 ID 类中。我认为这是正确的。但是,我使用 Hibernate 3.6 和 EclipseLink 2.2.0 获得了以下堆栈跟踪

hibernate :

org.hibernate.AssertionFailure: Unexpected nested component on the referenced entity when mapping a @MapsId: tld.transmuc.model.Foo
at org.hibernate.cfg.CopyIdentifierComponentSecondPass.doSecondPass(CopyIdentifierComponentSecondPass.java:101)
at org.hibernate.cfg.Configuration.processSecondPassesOfType(Configuration.java:1424)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1388)
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)
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.transmuc.Main.main(Main.java:27)
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: transmuc] 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.transmuc.Main.main(Main.java:27)
Caused by: org.hibernate.AssertionFailure: Unexpected nested component on the referenced entity when mapping a @MapsId: tld.transmuc.model.Foo
at org.hibernate.cfg.CopyIdentifierComponentSecondPass.doSecondPass(CopyIdentifierComponentSecondPass.java:101)
at org.hibernate.cfg.Configuration.processSecondPassesOfType(Configuration.java:1424)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1388)
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

eclipse 链接:

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.2.0.v20101118-r8514): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@1f7182c1
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20101118-r8514): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [transmuc] failed.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:136)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:65)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at tld.transmuc.Main.main(Main.java:27)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20101118-r8514): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [transmuc] failed.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1021)
at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:95)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:127)
... 4 more
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20101118-r8514): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [transmuc] failed.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:210)
... 7 more
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.EmbeddedIdAccessor.process(EmbeddedIdAccessor.java:189)
at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.processMappingAccessors(MetadataDescriptor.java:1417)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.processMappingAccessors(ClassAccessor.java:1405)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processMappingAccessors(EntityAccessor.java:1061)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.process(EntityAccessor.java:601)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(MetadataProject.java:1464)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:483)
at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:453)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:975)
... 6 more

EclipseLink 完全没有给我任何关于哪个类是违规类的提示。至少 Hibernate 告诉我有关 @MapsId 注释和 Foo 类的问题:

org.hibernate.AssertionFailure: Unexpected nested component on the referenced entity when mapping a @MapsId: tld.transmuc.model.Foo

这里有什么问题吗?

PS:我可以整理一个SSCCE,只问你是否需要(JavaSE、HSQLDB、Ant、Hibernate 3.6)。

最佳答案

简单地从引用依赖的 EmbeddedId 的 EmbeddedId 类型中删除 @Embedded 注释可能会解决您的问题。:

    @Embeddable    public class FooId implements Serializable    {        // no annotation.        private PostAddressId postAddressId; //just one field!        ...    }    ...        @MapsId(value = "postAddressId")        @OneToOne        @JoinColumns(value = {@JoinColumn(name = "contact_id", referencedColumnName = "contact_id"), @JoinColumn(name = "ordinal_nbr", referencedColumnName = "ordinal_nbr")})        private PostAddress postAddress = null; 

(编辑):另一个问题是 FooId 的使用。不应使用此类。 Foo 实体的 ID 类是 PostAddressId,OneToOne“postAddress”应该简单地标记为@Id 而不是@MapsId。同样,BarId 不应定义 Embedded,而应直接引用 PostAddressId。引用 PostAddress 的 Bar Mapping 应该使用 MapsId。

请提交一个针对 EclipseLink 的错误以解决可怕的异常,如果删除 @Embedded 和编辑中的其他更新不能解决您的问题,请同时提交一个 EclipseLink 错误。

关于java - JPA(Hibernate、EclipseLink)映射 : why doesn't this code work (chain of 2 relationships using JPA 2. 0,@EmbeddedId 复合 PK-FK)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4288531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com