【问题标题】:JPA Entity : Unable to locate persisterJPA 实体:无法找到持久性
【发布时间】:2021-03-24 08:33:12
【问题描述】:

我收到此错误

Caused by: java.lang.IllegalArgumentException: Unable to locate persister: model.Author

尝试在 Spring Boot 应用程序中使用我的 EntityManager 时。我不知道为什么它不能识别它。

主类:

@SpringBootApplication
@ComponentScan(basePackages = {"config"})
@EntityScan(basePackages = {"model"})
public class SpielwieseJpaJdbcApplication implements CommandLineRunner {

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

    @Override
    public void run(String... args) throws Exception {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("sks");
        EntityManager em = emf.createEntityManager();
        Author author = em.find(Author.class,1);
    }
}

实体本身:

@Data
@NoArgsConstructor
@AllArgsConstructor

@Entity
@Table(name = "authors")
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;
}

最后是我定义持久性单元的 persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
             version="2.2">
    <persistence-unit name="sks" transaction-type="RESOURCE_LOCAL">
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sks?serverTimezone=Europe/Vienna" />
            <property name="javax.persistence.jdbc.user" value="sks" />
            <property name="javax.persistence.jdbc.password" value="technikum1" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

我的项目结构很简单

主/java// main/java//Author.java

有什么想法吗?

此时我基本绝望了。

【问题讨论】:

  • 格式似乎与我在预览中看到的格式不符...项目结构为/main/java/&lt;package with main class&gt;/&lt;Main class&gt;,模型位置为/main/java/model/Author.java
  • 有趣的是,我在一个 maven 项目中创建了完全相同的项目(我之前使用过 gradle)并且它工作了....当我使用 maven 框架时,控制台输出也是彩色的......这可能只是 gradle 中的一个错误吗?

标签: java spring-boot jpa spring-data-jpa entitymanager


【解决方案1】:

我几乎可以确认这是一个 gradle 问题。 我已经试了好几次了,通过gradle创建项目导致了这个问题。

也许有人看到了我需要添加到自动化(弹簧初始化程序)build.gradle 的东西:

plugins {
    id 'org.springframework.boot' version '2.4.1'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '15'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

否则我猜解决方案是......使用 Maven :/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2013-04-18
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多