创建 EntityManagerFactory 时出现 ArrayIndexOutOfBoundsException

Posted

技术标签:

【中文标题】创建 EntityManagerFactory 时出现 ArrayIndexOutOfBoundsException【英文标题】:ArrayIndexOutOfBoundsException when creating a EntityManagerFactory 【发布时间】:2021-12-19 00:40:48 【问题描述】:

除了spring-data-jpa,我还在使用Spring Framework。我想通过创建 bean 来创建我的 DataSourceEntityManagerFactory,因为据我所知,不可能在纯 Spring 上下文中使用包含所有这些内容的属性文件。

如果有什么不合理的地方,请不要怀疑我的配置。这只是我从互联网上获得的一堆代码,因为我不喜欢 hibernate/spring-data-jpa/jpa 以及它的调用方式。

所以我现在的问题是我想创建我的EntityManagerFactory,但是它抛出了一个ArrayIndexOutOfBoundsException,我不知道它从哪里得到以及如何摆脱它。

堆栈:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [de/nebelniek/BukkitConfiguration.class]: Invocation of init method failed; nested exception is java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:93) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at de.nebelniek.Subserver.onEnable(Subserver.java:15) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[patched_1.17.1.jar:git-Purpur-1418]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:370) ~[patched_1.17.1.jar:git-Purpur-1418]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:500) ~[patched_1.17.1.jar:git-Purpur-1418]
    at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugin(CraftServer.java:561) ~[patched_1.17.1.jar:git-Purpur-1418]
    at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugins(CraftServer.java:475) ~[patched_1.17.1.jar:git-Purpur-1418]
    at net.minecraft.server.MinecraftServer.loadWorld(MinecraftServer.java:733) ~[patched_1.17.1.jar:git-Purpur-1418]
    at net.minecraft.server.dedicated.DedicatedServer.initServer(DedicatedServer.java:353) ~[patched_1.17.1.jar:git-Purpur-1418]
    at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1230) ~[patched_1.17.1.jar:git-Purpur-1418]
    at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:322) ~[patched_1.17.1.jar:git-Purpur-1418]
    at java.lang.Thread.run(Thread.java:831) ~[?:?]
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at org.jboss.jandex.Indexer.updateTypeTarget(Indexer.java:903) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.jboss.jandex.Indexer.updateTypeTargets(Indexer.java:630) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.jboss.jandex.Indexer.index(Indexer.java:1698) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.toClassDescriptor(ClassFileArchiveEntryHandler.java:64) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.handleEntry(ClassFileArchiveEntryHandler.java:52) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor.visitArchive(JarFileBasedArchiveDescriptor.java:147) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.hibernate.boot.archive.scan.spi.AbstractScannerImpl.scan(AbstractScannerImpl.java:48) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.hibernate.boot.model.process.internal.ScanningCoordinator.coordinateScan(ScanningCoordinator.java:76) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.prepare(MetadataBuildingProcess.java:107) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:269) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:182) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:52) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[bukkit-1.0-SNAPSHOT-shaded.jar:?]
    ... 21 more

我的配置:


    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(true);
        vendorAdapter.setGenerateDdl(true); //Auto creating scheme when true
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("de.nebelniek.database.user");
        factory.setDataSource(dataSource());
        return factory;
    

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) 
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory);
        return txManager;
    

    private DataSource dataSource() 
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.mariadb.jdbc.Driver");
        dataSource.setUsername("");
        dataSource.setPassword("");
        dataSource.setUrl("jdbc:mariadb://example.com:3306/database?createDatabaseIfNotExist=true");
        return dataSource;
    

另外我认为在de.nebelniek.database.user 中提供实体会非常有用,我只有一个:

@Entity
@Getter
@Setter
@Builder
@AllArgsConstructor
@RequiredArgsConstructor
public class CloudUser 

    @Id
    @GeneratedValue
    private long id;
    private UUID uuid;
    private String lastUserName;
    private Date lastLogin;
    private String twitchId;
    private boolean subbed;


所以,话虽如此,感谢任何帮助我的人。

【问题讨论】:

可能与此有关?最好升级您的版本并尝试。 github.com/wildfly/jandex/issues/80 @KishoreBandi 我已经得到了版本 2.2.3.Final,因为他说应该在那里修复。 @JanHerzog 你能解决这个问题吗?我有同样的问题,由于某种原因只出现在 docker 中。 【参考方案1】:

我在番石榴库中发现了我的问题。 只是将番石榴的版本降级到 30.1.1-jre 并且它工作正常。

参考:https://githubmemory.com/repo/wildfly/jandex/issues/147

【讨论】:

以上是关于创建 EntityManagerFactory 时出现 ArrayIndexOutOfBoundsException的主要内容,如果未能解决你的问题,请参考以下文章

启动程序时,在类路径资源中定义名称为 'entityManagerFactory' 的 bean 创建时出错

: 创建类路径资源中定义的名称为“entityManagerFactory”的 bean 时出错

创建名为“entityManagerFactory”的bean时出错:[PersistenceUnit:默认]无法构建Hibernate SessionFactory

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“entityManagerFactory”的bean时出错

添加spring boot jpa依赖项时创建名称为“entityManagerFactory”的bean时出错

使用 JPA 进行 Spring 启动 - 创建名为“entityManagerFactory”的 bean 时出错 - driverClassName 需要 jdbcUrl