当@MapKey指的是在可嵌入的映射值映射到的列AnnotationException

问题描述 投票:1回答:1

用例

我有其中包含地图的UserProfiles的,你可以看到用户配置被映射为一个元素集合,而不是作为一个单独的实体,用户实体。

@Entity
@Table(name = "user_")
public class User extends AbstractEntity<UserId> {

    ...

    @ElementCollection
    @CollectionTable(name = "user_profile")
    @MapKey(name = "userProfileType")
    @MapKeyEnumerated(STRING)
    private Map<UserProfileType, UserProfile> userProfiles;

}

@Embeddable
@NoArgsConstructor(access = PACKAGE)
@AllArgsConstructor(staticName = "of")
@EqualsAndHashCode
public class UserProfile {

    @Enumerated(STRING)
    private UserProfileType userProfileType;

    ...

}

注:我使用Hibernate 5.2.14。

问题

当Hibernate处理这些映射,我可以在以下情况例外:

Caused by: org.hibernate.AnnotationException: Associated class not found: UserProfile
    at org.hibernate.cfg.annotations.MapBinder.bindKeyFromAssociationTable(MapBinder.java:168) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.cfg.annotations.MapBinder.access$000(MapBinder.java:66) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:101) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1635) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1603) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:861) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:388) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]

解决方法尝试

我试图用的,而不是@MapKeyColumn @MapKey,但产生另一个错误声称柱user_profile_type的物理表示已经存在。

我想,如果这是应该根据JPA规范工作?

我认为它应该工作,因为我发现了一个非常类似的问题,它已经被固定在休眠5:https://hibernate.atlassian.net/browse/HHH-5393

谢谢你的帮助!

干杯,拉斯洛

hibernate hibernate-mapping
1个回答
0
投票

虽然我没有参与这个项目最近,我可以看到我检查下列方式代码到Git的张贴问题SOF后的几天。

@Entity
@Table(name = "user_")
public class User extends AbstractEntity<UserId> {

  ...

  @ElementCollection
  @CollectionTable(name = "user_profile")
  @MapKeyColumn(name = "user_profile_type", insertable = false, updatable = false)
  @MapKeyEnumerated(STRING)
  private Map<UserProfileType, UserProfile> userProfiles;

  ...

}

@Embeddable
@NoArgsConstructor(access = PACKAGE)
@AllArgsConstructor(staticName = "of")
@EqualsAndHashCode
@ToString(of = {"userProfileType", "fullName"})
public class UserProfile {

  @Enumerated(STRING)
  @Column(name = "user_profile_type")
  private UserProfileType userProfileType;

  ...

}

public enum UserProfileType {
  FACEBOOK, GOOGLE;
}

招数,我想,一定是添加insertable = false, updatable = false并消除了user_profile_type已定义的错误。

© www.soinside.com 2019 - 2024. All rights reserved.