Hel*_*rld 5 java hibernate spring-boot
我是 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
}
Run Code Online (Sandbox Code Playgroud)
所以它工作得很好,数据被索引并且可以通过索引模块内的搜索方法检索。
但是,当我运行 Spring Boot 服务器并执行相同的搜索时,我得到 java.lang.IllegalArgumentException: Not anEntity: 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);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2: 该项目的架构如下:
- Parent project (my.package)
-Module Base (with IndexSetup class)
-Module Indexing (that depends on Base)
-Module Server (that also depends on Base)
Run Code Online (Sandbox Code Playgroud)
父 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 /sf/ask/746615551/>
<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>
Run Code Online (Sandbox Code Playgroud)
编辑3: 问题源于指定要查看的表时:
Root<IndexSetup> from = criteriaQuery.from(IndexSetup.class);
Run Code Online (Sandbox Code Playgroud)
每当 时都会抛出查看休眠源 。所以我不明白为什么实体类型在这里为 null 而它在 SpringBoot 之外工作?not an entity
entityType == null
编辑4:
如果我SpringApplication.run(ServerApplication.class, args);
从服务器类的主方法中删除,则导致问题的相同调用即:
LocalDatabase.getInstance(false) // no GUI
.getAllIndexSetups();
Run Code Online (Sandbox Code Playgroud)
现在在皮科贝洛工作。当然它并不能解决任何问题,因为我仍然需要 SpringBoot 来进行搜索!所以对我来说这意味着 Spring Boot 不理解 hibernate 配置。我提出了一个新问题来更准确地介绍问题。
任何帮助表示赞赏,
所以偏偏是我没有正确使用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)
Run Code Online (Sandbox Code Playgroud)
1)首先,我hibernate.cfg.xml
从 Base 中删除并将其放入索引模块的资源中。我这样做是因为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)按照本教程,我IndexSetupRepository
几乎只用一行代码创建了一个 JPA 存储库:
public interface IndexSetupRepository extends CrudRepository<IndexSetup, Integer> {
Run Code Online (Sandbox Code Playgroud)
}
4)在服务器中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
Run Code Online (Sandbox Code Playgroud)
5)由于 SpringBoot 告诉我它找不到名为的表index_setup
(请参阅驼峰式大小写转换为 _),我必须将此行添加到 application.properties :
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Run Code Online (Sandbox Code Playgroud)
6)现在,当我得到“实体不受管理”时,我最终@EntityScan
按照许多人的建议向服务器主类添加了注释。
@EntityScan("my.package.Entities")
Run Code Online (Sandbox Code Playgroud)
请注意,@EntityScan
应该指向包含实体类的文件夹而不是实体类本身,即@EntityScan("my.package.Entities.IndexSetup")
不起作用。
归档时间: |
|
查看次数: |
8003 次 |
最近记录: |