簡體   English   中英

SpringBoot“不是一個實體”

[英]SpringBoot “not an entity”

我是Hibernate和SpringBoot的新手。 我的項目涉及一個搜索引擎,它由兩個獨立的模塊+兩個共同的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:不是實體:class my.package.IndexSetup

順便說一下,沒有構建錯誤,並且在模塊是父pom項目的一部分之前,它們與子文件夾中的服務器類位於同一項目中,並且它起作用。 為了方便起見,我決定將它們分開,並在生產中提供兩個獨立的模塊。

那么為什么當所有東西都在同一個Netbeans項目下並且現在模塊在2個不同的子文件夾中(但是在同一組ID包“my.package”中)時它才起作用我得到這個“不是實體”我應該怎么做要解決這個問題,我應該在哪里看看?

請注意:我已經嘗試過這個沒有成功(“空指針異常,無法加載數據庫”)。

編輯1:我也嘗試添加@EntityScan下面這個 ,但我仍然可以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);
 }
}

編輯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>

編輯3:問題源自指定要查看的表時:

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

查看hibernate源,只要entityType == null not an entity拋出not an entity 所以我不收集為什么實體類型在這里為null,而它在SpringBoot之外工作?

編輯4:如果我刪除SpringApplication.run(ServerApplication.class, args); 從Server類的main方法然后導致問題的同一調用,即:

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

現在工作picobello。 當然它沒有解決任何問題,因為我仍然需要SpringBoot進行搜索! 所以對我而言,這意味着Spring Boot不了解hibernate配置。 我開了一個新問題來更准確地介紹問題。

任何幫助,贊賞,

我認為你應該在第二個項目/模塊中添加你的實體的@EntityScan注釋包

首先,一些檢查:

  • 您的所有配置是僅在ServerApplication或任何Java類中使用注釋構建的,還是XML / YML文件中還有其他外部配置? 也許尋找沖突。 如果可能,我們寧願不將XML與注釋配置混合使用。
  • 嘗試刪除@Serializable (不是非常強制)。
  • 嘗試在您的根包中移動您的實體(就像測試一樣)。
  • 檢查導出@Entity的包是否正確。

問題:你叫什么“模塊”,它是一個子包或Maven模塊還是其他的東西? 我們可以為此配置包名嗎?

編輯

  • 對於多模塊項目,您是否遵循spring.io關於多模塊項目的建議? 您是否在子模塊中導入Spring BOM(或啟動器)並測試了Spring Boot Maven插件
  • 你能用你的數據源配置提供你的application.properties (或者application.yml什么)嗎? 您應該檢查您的數據源(和JPA,驅動程序類,...)是否正確定義; spring.io

所以我沒有正確使用SpringBoot功能。 以下是我遵循的步驟。 請記住項目的架構:

- Parent maven project (my.package)
 |-Module Base (with IndexSetup class and [initialy] hibernate.cfg.xml in /resources. It also had in the beginning LocalDatabase class to access to the local db via hibernate)
 |-Module Indexing (that depends on Base)
 |-Module Server (that also depends on Base)
 |-Database file (myLocalDB)

1)首先,我從Base中刪除了hibernate.cfg.xml ,並將其放入Indexing模塊的資源中。 我這樣做是因為SpringBoot有自己的配置機制。 我還從Base中刪除了LocalDatabase類(因為SpringBoot不需要它),並且在索引模塊中也刪除它(確實使用它)。

2)關注[this]( https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html]我將spring-boot-starter-data-jpa到服務器模塊pom.xml。

3)在本教程之后,我創建了一個JPA Repository IndexSetupRepository ,只有一行代碼:

public interface IndexSetupRepository extends CrudRepository<IndexSetup, Integer> {

}

4)在Server application.properties我添加了以下行:

# Embedded database configuration
# The embedded database file is one level above the Server folder (ie directly within the parent project)
# we use the auto server mode to be able to use the database simultaneously in the indexer and the server
spring.datasource.url=jdbc:h2:file:../myLocalDB;AUTO_SERVER=TRUE
spring.datasource.username=myName
# This parameter helped me discover that SpringBoot was not targetting the right table name.
spring.jpa.hibernate.ddl-auto=validate

5)由於SpringBoot告訴我它找不到名為index_setup表(參見camel case轉換為_ ),我不得不將此行添加到application.properties:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

6)現在當我得到“Entity not managed”時,我最終將@EntityScan注釋添加到Server主類中,因為很多人建議我這樣做。

@EntityScan("my.package.Entities")

請注意, @EntityScan應該指向包含實體類而不是實體類本身的文件夾,即@EntityScan("my.package.Entities.IndexSetup")不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM