简体   繁体   中英

can't import @Entity annotation from javax.persistence using Spring Data JPA

So I'm trying to solve this problem for hours and it's really annoying. I used Spring Initializr to generate a spring boot project: java 19, spring 3.0.0, maven, with mariadb driver, spring web, spring data jpa dependencies. The problem is that after I create a model class, Student, I can't import @Entity annotation from javax.persistence. All the tutorials and videos I watched, they imported from javax.persistence but I got only jakarta.persistence. Why? I tried to add myself dependency for javax.persistency but after I create the model with all annotation (@Entity, @Table, @column) the table is not created.

Here Are my files:

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>19</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Student.class

package com.example.demo;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;


@Entity(name = "student") // CAN'T GET FROM JAVAX.PERSISTENCE, SAME FOR ANNOTATIONS BELOW
public class Student {
    @Id
    @Column(name="id")
    private int id;
    @Column(name="name")
    private String name;
}

application.properties locates in src/main/resources

spring.datasource.url=jdbc:mariadb://localhost:3307/db1
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop

The entry point

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

What am I Doing wrong??

In the newest version of Spring Boot, javax library is replaced by jakarta (see more details here https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0.0-M1-Release-Notes ) and here https://spring.io/blog/2022/01/20/spring-boot-3-0-0-m1-is-now-available , so it's ok that you can only find jakarta . You may go ahead and use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM