@IndexedEmbedded 在 class 上包含其他 @IndexedEmbedded 字段

@IndexedEmbedded on class containing other @IndexedEmbedded fields

我正在尝试管理 class 上的休眠搜索索引,其中的字段由自定义 @Embeddable 实体上的 @IndexedEmbedded 映射。该实体还包含@MappedSuperclass 中的其他@IndexedEmbedded 字段。 这些是涉及的实体:

@Builder
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Indexed
@GeoPointBinding(fieldName = "location") 
public class Insertion {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "INSERTION_SEQ")
    private Long id;

    @NotNull
    @GenericField
    private Boolean publicated;

    @NotNull
    @Latitude
    private Double latitude;

    @NotNull
    @Longitude
    private Double longitude;

    @JsonIgnore
    private Point location;

    @FullTextField(analyzer = "generic_text")
    @KeywordField(name="city_sort", sortable = Sortable.YES, normalizer = "sort")
    private String city;

    @NotNull
    @Embedded
    @Valid
    @IndexedEmbedded
    private Amount amount;
    
    @IndexedEmbedded(name = "insertion_mate_preferences")
    @Embedded
    @Valid
    private MatePreference matePreferences;

}

还有这些 classes:

@SuperBuilder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class MatePreference extends BaseProfile {
    
    @NotNull
    @GenericField
    private Integer minAge;
    
    @NotNull
    @GenericField
    private Integer maxAge;
    
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@MappedSuperclass
public abstract class BaseProfile implements BaseProfileView {

    @GenericField
    private Boolean smoker;

    @GenericField
    private Boolean children;

    @GenericField
    private Boolean hasAnimals;

    @GenericField
    private Boolean student;

    @GenericField
    private Boolean employed;
    
    @GenericField
    private String animalsDescription;
    
    @Enumerated(EnumType.STRING)
    @GenericField
    private Genre genre;
    
    @Enumerated(EnumType.STRING)
    @GenericField
    private OccupationSector occupation;
    
    @Enumerated(EnumType.STRING)
    @ElementCollection(fetch = FetchType.EAGER)
    @GenericField
    @IndexedEmbedded
    private Set<Personality> personalities;

    @Enumerated(EnumType.STRING)
    @ElementCollection(fetch = FetchType.EAGER)
    @GenericField
    @IndexedEmbedded
    private Set<Lifestyle> lifestyles;
    
    @ElementCollection(fetch = FetchType.EAGER)
    @GenericField
    @IndexedEmbedded
    private Set<Language> languages;
    
    @Enumerated(EnumType.STRING)
    @GenericField
    private StudyTitle titleOfStudy;
    
    @Enumerated(EnumType.STRING)
    @GenericField
    private StudyField studyField;
}

当运行应用程序时,Hibernate Search抛出以下错误:

org.hibernate.search.util.common.SearchException: HSEARCH000520: Hibernate Search encountered failures during bootstrap. Failures:

    Hibernate ORM mapping: 
        type 'it.friendshome.api.common.model.insertion.Insertion': 
            path '.matePreferences<no value extractors>.languages': 
                index 'Insertion': 
                    field 'insertion_mate_preferences': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'languages'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.matePreferences<no value extractors>.lifestyles': 
                index 'Insertion': 
                    field 'insertion_mate_preferences': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'lifestyles'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.matePreferences<no value extractors>.personalities': 
                index 'Insertion': 
                    field 'insertion_mate_preferences': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'personalities'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
        type 'it.friendshome.api.common.model.profile.Profile': 
            path '.languages': 
                index 'Profile': 
                    index schema root: 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'languages'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.lifestyles': 
                index 'Profile': 
                    index schema root: 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'lifestyles'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.personalities': 
                index 'Profile': 
                    index schema root: 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'personalities'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
        type 'it.friendshome.api.common.model.search.Search': 
            path '.matePreference<no value extractors>.languages': 
                index 'Search': 
                    field 'matePreference': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'languages'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.matePreference<no value extractors>.lifestyles': 
                index 'Search': 
                    field 'matePreference': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'lifestyles'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.
            path '.matePreference<no value extractors>.personalities': 
                index 'Search': 
                    field 'matePreference': 
                        failures: 
                          - HSEARCH400520: Duplicate index field definition: 'personalities'. Index field names must be unique. Look for two property mappings with the same field name, or two indexed-embeddeds with prefixes that lead to conflicting index field names, or two custom bridges declaring index fields with the same name.

我 运行 它具有以下依赖项:

关于如何管理这种情况有什么建议吗?

此代码不正确:

    @GenericField
    @IndexedEmbedded
    private Set<Language> languages;

对于 @GenericField,您要求 Hibernate Search 为语言生成一个“值”字段(字符串、整数...)。这通常是行不通的,因为 Hibernate Search 不知道如何将 Language 转换为 string/integer/...,所以这是第一个问题。

但是第二个问题,也是 Hibernate Search 报告的问题,是通过将 @IndexedEmbedded 放在同一个 属性 上,您还要求 Hibernate Search 生成一个“对象” (复合)字段,@GenericField 中的“值”字段同名。所以有一个冲突:“值”字段和“对象”字段具有相同的名称“语言”。

您应该在使用 @IndexedEmbedded 的任何地方删除 @GenericField


请注意,从技术上讲,只要您明确设置使用 @GenericField(name = ...)@IndexedEmbedded(name = ...) 在任一注释上使用不同的名称。但是,除非您使用 custom value bridge,否则 @GenericField 将无法工作,而且我不认为您打算这样做。所以请不要那样做,除非你有实际原因并且 @IndexedEmbedded 没有按照你的意愿去做。