我得到这个错误
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
有什么想法吗?
在这一点上我基本上绝望了。
1条答案
按热度按时间hjqgdpho1#
我几乎可以肯定这是一个渐变的问题。我已经尝试过好几次了,通过gradle创建这个项目会导致这个问题。
也许有人看到了我需要添加到automated(spring initializer)build.gradle的东西:
否则我想解决办法是。。。使用maven:/