Spring Boot“不是一个实体”

2024-02-24

我是 Hibernate 和 SpringBoot 的新手。我的项目涉及一个搜索引擎,该引擎由 2 个独立模块 + 1 个两者共用的基本模块组成(其中IndexSetup类驻留)。

有一个用于索引的模块 (JavaFx),另一个用于通过 Web 浏览器进行搜索 (Spring Boot)。

索引模块涉及一个“IndexSetup”类,其中包含有关如何/应索引什么的详细信息:

@Entity
@Table(name = "IndexSetups")
@Access(AccessType.PROPERTY)
public class IndexSetup {
  private final SimpleIntegerProperty id = new SimpleIntegerProperty();

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO) // For H2 AUTO is required to auto increment the id
  public int getId() {
      return id.get();
  }

  //... other properties, getters and setters

 }

所以它工作得很好,数据被索引并且可以通过索引模块内的搜索方法检索。

但是,当我运行 Spring Boot 服务器并执行相同的搜索时,我得到 java.lang.IllegalArgumentException:不是实体:类 my.package.IndexSetup

顺便说一句,没有构建错误,并且在模块成为父 pom 项目的一部分之前,它们与子文件夹中的服务器类位于同一项目中,并且它可以工作。为了方便开发过程,我决定将它们分开,并在生产中提供两个独立的模块。

那么,当所有内容都在同一个 Netbeans 项目下并且现在模块位于 2 个不同的子文件夹中(但在同一个组 id 包“my.package”中)时,为什么它还能工作?我得到这个“不是实体”,我应该做什么要解决这个问题,我应该看哪里?

请注意:我已经尝试过this https://stackoverflow.com/questions/1780341/do-i-need-class-elements-in-persistence-xml没有成功(“空指针异常,无法加载数据库”)。

Edit 1:我也尝试添加@EntityScan下列的this https://dzone.com/articles/spring-boot-entity-scan但我仍然得到Not an entity: class my.package.IndexSetup :

@SpringBootApplication
@EntityScan( basePackages = {"my.package"} )
public class ServerApplication {

public static void main(String[] args) {
    SpringApplication.run(ServerApplication.class, args);
 }
}

Edit 2 :该项目的架构如下:

- Parent project (my.package)
  -Module Base (with IndexSetup class)
  -Module Indexing (that depends on Base)
  -Module Server (that also depends on Base)

父 pom.xml 如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.package</groupId>
<artifactId>MyApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--According to https://stackoverflow.com/questions/10665936/maven-how-to-build-multiple-independent-maven-projects-from-one-project-->

<modules>
    <module>Base</module> <!-- Common resources which is a dependency in Indexer and Server -->
    <module>Indexer</module> <!-- Indexing part with JavaFx-->
    <module>Server</module> <!-- Server (spring boot) part of -->
</modules>
<name>MyApp</name>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArguments>
                    <bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>${java.home}/lib/jfxrt.jar</additionalClasspathElement>
                </additionalClasspathElements>
            </configuration>
        </plugin>
    </plugins>
</build>

Edit 3:问题源于指定要查看的表时:

Root<IndexSetup> from = criteriaQuery.from(IndexSetup.class);

看着休眠源 https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetamodelImpl.java not an entity每当entityType == null. 所以我不明白为什么实体类型在这里为 null 而它在 SpringBoot 之外工作?

Edit 4:如果我删除SpringApplication.run(ServerApplication.class, args);从服务器类的 main 方法中,然后是导致问题的相同调用,即:

LocalDatabase.getInstance(false) // no GUI
            .getAllIndexSetups();

现在在皮科贝洛工作。当然它并不能解决任何问题,因为我仍然需要 SpringBoot 来进行搜索!所以对我来说这意味着 Spring Boot 不理解 hibernate 配置。我开了一个新问题 https://stackoverflow.com/questions/52813610/why-does-my-entity-does-not-work-with-springboot-although-it-works-without更准确地介绍问题。

任何帮助表示赞赏,


我认为您应该添加到第二个项目/模块中实体的 @EntityScan 注释包

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot“不是一个实体” 的相关文章

随机推荐