jpa实体:找不到持久器

aelbi1ox  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(416)

我得到这个错误

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

尝试在spring启动应用程序中使用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>

我的项目结构是直截了当的
main/java//main/java//author.java
有什么想法吗?
在这一点上我基本上绝望了。

hjqgdpho

hjqgdpho1#

我几乎可以肯定这是一个渐变的问题。我已经尝试过好几次了,通过gradle创建这个项目会导致这个问题。
也许有人看到了我需要添加到automated(spring initializer)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:/

相关问题