gpt4 book ai didi

java - Spring 启动错误: Use of @OneToMany or @ManyToMany targeting an unmapped class

转载 作者:行者123 更新时间:2023-12-02 02:16:54 25 4
gpt4 key购买 nike

我在 SpringBoot 中编写了我的简单项目并使用 Hibernate。我有三个实体,但是当我启动应用程序时出现此错误:

org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义的名为“entityManagerFactory”的bean时出错:调用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;

}

用户:

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>

最佳答案

就我而言,缺少 @Entity 注释导致了问题。添加后,问题就解决了。

关于java - Spring 启动错误: Use of @OneToMany or @ManyToMany targeting an unmapped class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49174164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com