SpringBoot“不是一个实体” [英] SpringBoot "not an entity"

查看:142
本文介绍了SpringBoot“不是一个实体”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate和SpringBoot的新手。我的项目涉及一个搜索引擎,它由两个独立的模块+两个共同的1个基本模块组成( IndexSetup 类所在的位置)。

I am new to Hibernate and SpringBoot. My projects deals with a search engine that is composed of 2 independent modules + 1 base module common to both (where the IndexSetup class resides).

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

There is one module for indexing (JavaFx) and the other one for searching via the web browser (Spring Boot).

索引模块涉及一个IndexSetup类,其中包含有关如何/应该索引的内容的详细信息:

The indexing module involves an "IndexSetup" class that has the details on how / what should be indexed :

@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

 }

因此效果很好,数据已编入索引并可通过索引模块中的搜索方法检索。

So it works great, the data is indexed and can be retrieved via a search method within the indexing module.

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

However when I run the Spring Boot server and do the same search I get java.lang.IllegalArgumentException: Not an entity: class my.package.IndexSetup

By没有构建错误的方式,并且在模块是父pom项目的一部分之前,它们与子文件夹中的服务器类位于同一项目中,并且它有效。为了方便起见,我决定将它们分开,并在生产中提供两个独立的模块。

By the way there is no build error, and before the modules were parts of a parent pom project, they were in the same project with the server class in a subfolder, and it worked. I decided to separate them for convenience during developpment and to offer two independent modules in production.

那么为什么当所有东西都在同一个Netbeans项目下时它才起作用模块在2个不同的子文件夹中(但在同一组ID包my.package中)我得到这个不是实体,我应该怎么做才能解决这个问题,我应该在哪里看看?

So why did it work when everything was under the same Netbeans project and now that the modules are in 2 different subfolders (but in the same group id package "my.package") I get this "Not an entity" and what should I do to solve this, where should I look at ?

请注意:我已经尝试过没有成功(空指针异常,无法加载数据库)。

Please note : I already tried this without success ("null pointer exception, cannot load the database").

编辑1:
我也尝试过添加 @EntityScan 以下但我仍然得到不是实体:class my.package.IndexSetup

Edit 1: I also tried to add @EntityScan following this but I still get 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:
项目的架构如下:

Edit 2 : The architecture of the project is like :

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

父pom.xml的内容如下:

The parent pom.xml reads like the following :

    <?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:
问题源于指定要查看的表时:

Edit 3: The problem originates from when the table to look at is specified:

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

查看 hibernate sources 不是实体 entityType == null ,就会抛出code>。 所以我不收集为什么实体类型在这里为null,而它在SpringBoot之外工作?

Looking at hibernate sources not an entity is thrown whenever entityType == null. So I don't gather why the entity type is null here whereas it works outside of SpringBoot ?

编辑4:
如果我从Server类的main方法中删除 SpringApplication.run(ServerApplication.class,args); ,那么导致问题的同一调用即:

Edit 4: If I remove SpringApplication.run(ServerApplication.class, args); from the Server class' main method then the same call which was causing the issue ie :

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

现在可以使用picobello。当然它没有解决任何问题,因为我仍然需要SpringBoot进行搜索!所以对我而言,这意味着Spring Boot不了解hibernate配置。我打开了一个新问题更准确地介绍问题。

now works picobello. Of course it does not solve anything since I still need SpringBoot for the search! So for me it means that Spring Boot does not understand the hibernate configuration. I opened a new question to introduce the problem more accurately.

任何帮助表示赞赏,

推荐答案

碰巧我没有正确使用SpringBoot功能。以下是我遵循的步骤。请记住项目的架构:

So it happened that I did not use correctly SpringBoot capabilities. Here are the steps I followed. Please remember the architecture of the project :

- 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不需要它),并且在索引模块中也删除了它(确实使用它)。

1) First I removed hibernate.cfg.xml from Base and dropped it into resources of the Indexing module. I did it because SpringBoot has its own configuration mechanism. I also removed LocalDatabase class from Base (since it would not be needed by SpringBoot) and dropped it too in the Indexing Module (where it is used indeed).

2)关注[this]( https: //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html] 我添加了 spring-boot-starter-data-jpa 到服务器模块pom.xml。

2) Following [this](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html] I added spring-boot-starter-data-jpa to the Server module pom.xml.

3)关注本教程我用几行代码创建了一个JPA Repository IndexSetupRepository

3) Following this tutorial I created a JPA Repository IndexSetupRepository with barely a single line of code :

public interface IndexSetupRepository extends CrudRepository<IndexSetup, Integer> {

}

4)在服务器 application.properties 我添加了这些行:

4) In the Server application.properties I added those lines :

# 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:

5) As SpringBoot was telling me it could not find table named index_setup (see camel case converted to _), I had to add this line to the application.properties :

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

6)现在,当我得到Entity not managed时,我最终将 @EntityScan 注释添加到Server主类中你建议我这样做。

6) Now as I got "Entity not managed", I eventually added @EntityScan annotation to the Server main class as many of you advised me to do.

@EntityScan("my.package.Entities")

请注意 @EntityScan 应该指向包含实体的文件夹class不是实体类本身,即 @EntityScan(my.package.Entities.IndexSetup)没有工作。

Please note that @EntityScan should point to the folder containing the entity class not the entity class itself ie @EntityScan("my.package.Entities.IndexSetup") did not work.

这篇关于SpringBoot“不是一个实体”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆