Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot";不是一个实体”;_Java_Hibernate_Spring Boot - Fatal编程技术网

Java SpringBoot";不是一个实体”;

Java SpringBoot";不是一个实体”;,java,hibernate,spring-boot,Java,Hibernate,Spring Boot,我不熟悉冬眠和春靴。我的项目处理的是一个搜索引擎,它由两个独立的模块+1个基本模块组成(其中IndexSetup类驻留) 有一个模块用于索引(JavaFx),另一个模块用于通过web浏览器进行搜索(springboot) 索引模块涉及一个“IndexSetup”类,该类详细说明了如何/应该索引什么: @Entity @Table(name = "IndexSetups") @Access(AccessType.PROPERTY) public class IndexSetup { priva

我不熟悉冬眠和春靴。我的项目处理的是一个搜索引擎,它由两个独立的模块+1个基本模块组成(其中
IndexSetup
类驻留)

有一个模块用于索引(JavaFx),另一个模块用于通过web浏览器进行搜索(springboot)

索引模块涉及一个“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启动服务器并执行相同的搜索时,我得到 java.lang.IllegalArgumentException:不是实体:类my.package.IndexSetup

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

那么,当所有的东西都在同一个Netbeans项目下,而现在模块在两个不同的子文件夹中(但在同一个组id包“my.package”中),为什么它能工作呢?我得到的是“不是一个实体”,我应该怎么解决这个问题,我应该看哪里

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

编辑1: 我还尝试在下面添加
@EntityScan
,但仍然得到
不是实体: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>
现在是皮科贝罗。当然,这并不能解决任何问题,因为我仍然需要SpringBoot进行搜索!所以对我来说,这意味着Spring Boot不理解hibernate配置。为了更准确地介绍这个问题,我打开了一个对话框


非常感谢您的帮助,

我认为您应该在第二个项目/模块内的@EntityScan实体注释包中添加一些检查:

  • 您的所有配置都是在ServerApplication或任何Java类中使用注释构建的,还是在XML/YML文件中有其他外部配置?也许要寻找冲突。如果可能的话,我们不希望将XML与注释配置混合使用
  • 尝试删除
    @Serializable
    (并非强制要求)
  • 尝试在根包中移动实体(就像测试一样)
  • 检查导出
    @Entity
    的包是否正确
问:你称之为“模块”是什么,它是一个子包、Maven模块还是其他东西?我们可以知道关于这个的包名的配置吗

编辑

  • 对于多模块项目,您是否遵循了关于多模块项目的建议?您是否在子模块中导入了Spring BOM(或starter),并对其进行了测试
  • 您能否为数据源配置提供
    应用程序.properties
    (或
    应用程序.yml
    任何内容)?您应该检查您的数据源(以及JPA、驱动程序类等)是否定义正确;看

所以碰巧我没有正确使用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
,并将其放入索引模块的资源中。我这么做是因为SpringBoot有自己的配置机制。我还从基中删除了LocalDatabase类(因为SpringBoot不需要它),并在索引模块中也删除了它(实际上在索引模块中使用了它)

2) 在此之后(我向服务器模块pom.xml添加了
springbootstarter数据jpa

3) 接下来,我创建了一个JPA存储库
IndexSetupRepository
,几乎没有一行代码:

public interface IndexSetupRepository extends CrudRepository<IndexSetup, Integer> {
5) 当SpringBoot告诉我它找不到名为
index\u setup
(请参阅)的表时,我不得不将这一行添加到应用程序中。属性:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
6) 现在,当我得到“Entity not managed”时,我最终向服务器主类添加了
@EntityScan
注释,正如许多人建议我做的那样

@EntityScan("my.package.Entities")

请注意,
@EntityScan
应指向包含实体类的文件夹,而不是实体类本身,即
@EntityScan(“my.package.Entities.IndexSetup”)
不起作用。

谢谢@Emil Hotkowski,但它不起作用(请参阅我的编辑)在我改变了许多事情之后,它确实起了作用,其中包括我在回答中详述的
@EntityScan
。无论如何,谢谢。很高兴它能帮上忙:)谢谢@cactuschibre,我会试试你的建议。模块是Maven模块(请参阅我在编辑中添加的父pom.xml)。我删除了
implements serializable
,正如前面解释的一样,没有任何更改。我将IndexSetup类移动到名为
Entities
的子文件夹中,相应地更新了hibernate.cfg.xml(否则我会得到NPE),但我仍然得到
而不是一个entity
。索引器模块没有问题,它可以完美地使用这个实体。伟大的链接@cactuschibre!不,我还没有关注这些链接,但我现在会做,然后重试!在
application.properties
中只有
server.port=8081
。我将写下我是如何做到的,这是你的提示,其他人的提示和许多其他问题的混合!我学到了很多,谢谢你@正如您所说的那样,id字段没有用@id标记。但是@id标记在
getId
方法上,因为
id
SimpleIntegerProperty
(JavaFx)。因此,将SimpleIntegerProperty与hibernate结合使用是一个诀窍。您认为这是问题的原因吗?您是否尝试过删除@Entity?你为什么需要它?或者尝试在Id上添加@Id。每个实体都必须有标识符/主键:为什么需要实体和hibern
# 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
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
@EntityScan("my.package.Entities")