简体   繁体   English

Spring 上下文索引器导致 hibernate 实体映射出现问题

[英]Spring context indexer causes issues with hibernate entity mapping

I have a project that is split between multiple modules, each module is imported into the main one as a maven dependency.我有一个在多个模块之间拆分的项目,每个模块都作为 maven 依赖项导入主模块。 Persistence entities can be located at any of the projects but under the same package.持久性实体可以位于任何项目中,但位于相同的 package 下。 I have been trying to improve the startup time of the application by using the spring-context-indexer but it seems to cause an issue with detecting entities.我一直在尝试通过使用 spring-context-indexer 来改善应用程序的启动时间,但这似乎会导致检测实体出现问题。 My @EntityScan is configured like this:我的@EntityScan 配置如下:

@EntityScan(basePackages = {"com.botscrew", "com.botscrew.demoadminpanel.entity.jpa","com.botscrew.admin.entity"})

The strange thing is that error looks like this奇怪的是错误看起来像这样

org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.botscrew.admin.entity.Bot.amioWhatsAppConfigs references an unknown entity: com.botscrew.admin.entity.services.configs.AmioWhatsAppConfigs

Essentially both entities are located under the same package but Bot entity was resolved but AmioWhatsAppConfigs was not.本质上,这两个实体都位于同一个 package 下,但 Bot 实体已解决,但 AmioWhatsAppConfigs 未解决。

The application starts perfectly fine without spring indexer.应用程序在没有 spring 索引器的情况下可以正常启动。

I am using spring boot 2.2.1.RELEASE我正在使用 spring 启动 2.2.1.RELEASE

Entities classes:实体类:

@Getter
@Setter
@Builder
@Entity
@ToString(of = {"id", "name"})
@AllArgsConstructor
@Table(name = "admin_bot")
@DiscriminatorValue("Bot")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Bot {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Convert(converter = EmojiConverter.class)
    private String name;

    private Integer timezone;

    private String greetingText;

    @Column(columnDefinition = "tinyint(1) default 1")
    private Boolean active;

    @Column(unique = true, updatable = false, nullable = false)
    private String publicIdentifier;

    @OneToOne(fetch = FetchType.LAZY)
    private PersistentMenuEntity persistentMenuEntity;

    //TODO FetchType.LAZY
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "widget_id")
    private Widget widget;

    //TODO FetchType.LAZY
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private MessengerConfigs messengerConfigs;

    //TODO FetchType.LAZY
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private AmioWhatsAppConfigs amioWhatsAppConfigs;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private TwilioConfigs twilioConfigs;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private DialogflowConfigs dialogflowConfigs;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private ChatbaseConfig chatbaseConfig;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private SupportSettings supportSettings;

    @OneToMany
    private Set<Tag> tags;

    @OneToMany(mappedBy = "bot")
    private List<Broadcast> broadcasts;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "admin_bot_features",
        joinColumns = {@JoinColumn(name = "bot_id")},
        inverseJoinColumns = {@JoinColumn(name = "feature_id")})
    private Set<Feature> features;

    public Bot() {
        this.active = true;
    }

    public Bot(String name, DefaultWidgetProperties defaultWidgetProperties) {
        this.publicIdentifier = UUID.randomUUID().toString();
        this.chatbaseConfig = new ChatbaseConfig();
        this.amioWhatsAppConfigs = new AmioWhatsAppConfigs();
        this.timezone = 0;
        this.name = name;
        this.active = true;
        this.messengerConfigs = new MessengerConfigs();
        this.dialogflowConfigs = new DialogflowConfigs();
        this.widget = new Widget(defaultWidgetProperties);
        this.supportSettings = new SupportSettings(false);
    }
    
}


@Getter
@Setter
@Entity
@Accessors(chain = true)
@ToString
@Table(name = "admin_amio_whatsapp_configs")
public class AmioWhatsAppConfigs implements AmioWhatsAppBot {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String channelId;
    private String accessToken;
    private String secret;
}

Please help请帮忙

I am editing my answer please check example我正在编辑我的答案,请检查示例

@EntityScan(basePackages = {"com.botscrew", 
"com.botscrew.demoadminpanel.entity.jpa","com.botscrew.admin.entity.services.configs.*"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM