【发布时间】:2019-03-18 21:01:48
【问题描述】:
我是 Hibernate 和 SpringBoot 的新手。我的项目处理的搜索引擎由 2 个独立模块 + 1 个共同的基本模块(IndexSetup 类所在的位置)组成。
有一个模块用于索引(JavaFx),另一个模块用于通过网络浏览器进行搜索(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,但没有成功(“空指针异常,无法加载数据库”)。
编辑 1:
我还尝试在this 之后添加@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 sources not an entity 在entityType == null 时抛出。 所以我不知道为什么实体类型在这里为 null 而它在 SpringBoot 之外工作?
编辑 4:
如果我从 Server 类的 main 方法中删除 SpringApplication.run(ServerApplication.class, args);,那么导致问题的同一个调用即:
LocalDatabase.getInstance(false) // no GUI
.getAllIndexSetups();
现在可以使用 picobello。当然它不能解决任何问题,因为我仍然需要 SpringBoot 进行搜索!所以对我来说,这意味着 Spring Boot 不了解休眠配置。我开了new question更准确的介绍问题。
任何帮助表示赞赏,
【问题讨论】:
-
@Justas 你是对的,id 字段没有用@Id 标记。但是@Id 标记在
getId方法上,因为id是SimpleIntegerProperty(JavaFx)。因此,将 SimpleIntegerProperty 与 hibernate 一起使用是诀窍。您认为这是造成问题的原因吗? -
你试过删除@Entity吗?你为什么需要它?或者尝试在 id 上添加@Id。
-
每个实体都必须有标识符/主键:stackoverflow.com/questions/30712842/…
-
为什么需要实体和休眠进行索引? Hibernate是ORM,方便访问关系型数据库,而不是查询索引。
-
@Justas 用户可以添加 IndexSetup(带有索引名称、要索引的数据、已启用...)。我将这些IndexSetups存储在数据库中,然后用solr一一处理。在搜索时,我在每个索引上调用我的搜索功能(通过 SolrJ):
givenIndex.search(params)。这就是为什么我需要在搜索时获取 IndexSetups 列表。
标签: java hibernate spring-boot