SpringBoot "not an entity"

HelloWorld :

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).

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

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.

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 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.

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").

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);
 }
}

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)

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>

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

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

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 ?

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();

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.

Any help appreciated,

HelloWorld :

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) 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) 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) Following this tutorial I created a JPA Repository IndexSetupRepository with barely a single line of code :

public interface IndexSetupRepository extends CrudRepository<IndexSetup, Integer> {

}

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) 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) 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")

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103442&siteId=1