Home > Net >  JavaFX Desktop App with Hibernate - @Entity and other annotations are not recognized
JavaFX Desktop App with Hibernate - @Entity and other annotations are not recognized

Time:10-14

So, I've decided to make a project with Hibernate and JavaFX. I have designed the UI and tested it in a project I've created with Maven, using the archetype called "javafx-archetype-fxml" which works just fine. Now I'm trying to add Hibernate to this project to manage the persistence layer. I added the dependency in Maven, "hibernate-core" v.6.0.0 and proceeded to map the model classes with "@Entity" type annotations, but they aren't recognized by VSCode. Maybe I need to add a module to module-info.java?

@Entity Annotation not recognized.

This is my pom.xml file:

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.0.0.Beta1</version>
        <type>pom</type>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.26</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>13</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>13</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <mainClass>com.ignaciocassi.App</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

This is my module-info.java file:

module com.ignaciocassi {
    requires javafx.controls;
    requires transitive javafx.graphics;
    requires javafx.fxml;
    requires java.sql;
    opens controllers to javafx.fxml;
    exports com.ignaciocassi;
 }

I'm new to Maven and modular projects, and I want to learn. Any suggestions or help would be much appreciated.

CodePudding user response:

For anyone having the same problem as me in the future, I recommend reading Understanding Java 9 Modules - What they are and how to use them as suggested by @jewelsea.

What actually solved it for me, was adding the "requires jakarta.persistence" to Module-info.java, which provides JPA annotations.

CodePudding user response:

Try adding Spring data JPA maven dependency, as @Entity annotation requires javax.persistence package.

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.5.5</version>
</dependency>
  • Related