【发布时间】: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/<package with main class>/<Main class>,模型位置为/main/java/model/Author.java -
有趣的是,我在一个 maven 项目中创建了完全相同的项目(我之前使用过 gradle)并且它工作了....当我使用 maven 框架时,控制台输出也是彩色的......这可能只是 gradle 中的一个错误吗?
标签: java spring-boot jpa spring-data-jpa entitymanager