首页 > 解决方案 > Spring 上下文索引器导致休眠实体映射出现问题

问题描述

我有一个在多个模块之间拆分的项目,每个模块都作为 maven 依赖项导入到主模块中。持久性实体可以位于任何项目中,但在同一个包下。我一直在尝试通过使用 spring-context-indexer 来改善应用程序的启动时间,但这似乎会导致检测实体出现问题。我的@EntityScan 配置如下:

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

奇怪的是错误看起来像这样

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

本质上,这两个实体都位于同一个包下,但 Bot 实体已解决,但 AmioWhatsAppConfigs 未解决。

应用程序在没有弹簧分度器的情况下启动得非常好。

我正在使用弹簧启动 2.2.1.RELEASE

实体类:

@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;
}

请帮忙

标签: springspring-boothibernate

解决方案


我正在编辑我的答案,请检查示例

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

推荐阅读