我在SpringBoot中编写我的简单项目并使用Hibernate。我有三个实体,但是当我启动我的应用程序时,我有这个错误:
org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名称为'entityManagerFactory'的bean时出错[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]:init的调用方法失败;嵌套异常是org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany定位未映射的类:com.example.entity.User.tasks [javafx.concurrent.Task]
这是我的代码:
package com.example.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.List;
@Data
@NoArgsConstructor
@Entity
public class Role {
@Id
private String name;
@ManyToMany(mappedBy = "roles")
private List<User> users;
}
任务:
package com.example.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotEmpty;
@Data
@NoArgsConstructor
@Entity
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotEmpty
private String date;
@NotEmpty
private String startTime;
private String stopTime;
@NotEmpty
private String description;
@ManyToOne
@JoinColumn(name = "USER_EMIAL")
private User user;
}
USER:
package com.example.entity;
import javafx.concurrent.Task;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.util.List;
@Entity
@Data
@NoArgsConstructor
public class User {
@Id
@Email
@NotEmpty
@Column(unique = true)
private String email;
@NotEmpty
private String name;
@NotEmpty
@Size(min = 5)
private String password;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Task> tasks;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "USER_ROLE", joinColumns = {
@JoinColumn(name = "USER_EMAIL", referencedColumnName = "email")
}, inverseJoinColumns = {@JoinColumn(name = "ROLE_NAME", referencedColumnName = "name")})
private List<Role> roles;
}
和pom.xml依赖项:
<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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.12.Final</version>
</dependency>
</dependencies>
答案 0 :(得分:2)
在User
中,您导入了错误的Task
类。
只需删除import javafx.concurrent.Task;
(您无需导入com.example.entity.Task
,因为您的三个类位于同一个包中。)
请注意,错误消息提供了一个很好的提示:
定位未映射的类: com.example.entity.User.tasks [<强> javafx.concurrent.Task 强>
答案 1 :(得分:1)
就我而言,缺少@Entity注释已造成此问题。添加后,问题即可解决。