Java @我的springboot应用程序中的OneToOne或@ManyToOne错误
这是我的课程。java类,一个主题可以有多个类。当我运行我的程序时,我得到两个异常,一个是BeanCreationException,另一个是AnnotationException。我在下面用spring控制台日志附加了我代码的所有类Java @我的springboot应用程序中的OneToOne或@ManyToOne错误,java,spring-boot,Java,Spring Boot,这是我的课程。java类,一个主题可以有多个类。当我运行我的程序时,我得到两个异常,一个是BeanCreationException,另一个是AnnotationException。我在下面用spring控制台日志附加了我代码的所有类 package com.shashank.courses; import topic.Topic; import javax.persistence.Entity; import javax.persistence.Id; import javax.persist
package com.shashank.courses;
import topic.Topic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Course {
@Id
private String id;
private String name;
private String description;
// There can be many courses under the topics
@ManyToOne
private Topic topic;
public Course() {
}
public Course(String id, String name, String description, String topicId) {
super();
this.id = id;
this.name = name;
this.description = description;
this.topic = new Topic(topicId, "", "");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic = topic;
}
}
这是我的CourseController.java
package com.shashank.courses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import topic.Topic;
import java.util.List;
import java.util.Optional;
@RestController
public class CourseController {
@Autowired
private CourseService courseService;
@RequestMapping("/topics/{id}/courses")
public List<Course> getAllCourses(@PathVariable String id) {
return courseService.getAllCourses(id);
}
// {} for variable portion
@RequestMapping("/topics/{topicId}/courses/{id}")
public Optional<Course> getCourse(@PathVariable String id) {
return courseService.getCourse(id);
}
@RequestMapping(method = RequestMethod.POST, value = "/topics/{topicId}/courses")
public void addCourse(@RequestBody Course course, @PathVariable String topicId) {
course.setTopic(new Topic(topicId, "",""));
courseService.addCourse(course);
}
@RequestMapping(method = RequestMethod.PUT, value = "/topics/{topicId}/courses/{id}")
public void updateCourse(@RequestBody Course course, @PathVariable String topicId, @PathVariable String id) {
course.setTopic(new Topic(topicId, "",""));
courseService.updateCourse( course);
}
@RequestMapping(method = RequestMethod.DELETE, value = "/topics/{topicId}/courses/{id}")
public void deleteCourse(@PathVariable String id) {
courseService.deleteCourse(id);
}
}
package com.shashank.courses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class CourseService {
@Autowired
private CourseRepository courseRepository;
public List<Course> getAllCourses(String topicId) {
List<Course> courses = new ArrayList<>();
courseRepository.findByTopicId(topicId).forEach(courses::add);
return courses;
}
public Optional<Course> getCourse(String id) {
return courseRepository.findById(id);
}
public void addCourse(Course course) {
courseRepository.save(course);
}
public void updateCourse(Course course) {
courseRepository.save(course);
}
public void deleteCourse(String id) {
courseRepository.deleteById(id);
}
}
这是我的Spring控制台日志
Error starting ApplicationContext. To display the conditions report re-run your application with
'debug' enabled.
2021-03-04 16:23:05.108 ERROR 14500 --- [main] o.s.boot.SpringApplication :
Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'entityManagerFactory' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init
method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on
com.shashank.courses.Course.topic references an unknown entity: topic.Topic
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on
com.shashank.courses.Course.topic references an unknown entity: topic.Topic
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:97) ~[hibernate-core-
5.4.28.Final.jar:5.4.28.Final]
你应该有一套关于你的主题课的
课程
:
@OneToMany(mappedBy = "topic")
private List<Course> courseList;
对于关系,您需要这样的sintax:
@OneToMany(mappedBy="cart")
private List<Items> items;
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
@OneToMany(mappedBy=“cart”)
私人清单项目;
@许多酮
@JoinColumn(name=“cart\u id”,nullable=false)
私家车;
com.shashank.courses.courses.topic引用了一个未知实体:topic.topic:这意味着Spring Boot找不到topic实体,因此它无法创建课程实体,因为存在多通关系,所以应用程序无法启动,因为存在BeanCreationException
查看您的代码,我可以看到Topic类在“Topic”包中,但其他所有类/接口都在“com.shashank.courses”包中
假设您使用“com.shashank.courses”包作为应用程序的主包,SpringBoot将扫描此包以创建它所需的bean。但是由于Topic类位于不同的包中,因此它无法创建topicbean
如果是这种情况,则有两种选择:
@OneToMany(mappedBy = "topic")
private List<Course> courseList;
@ManyToOne
@JoinColumn(name = "topic_id", nullable = false)
private Topic topic;
@OneToMany(mappedBy="cart")
private List<Items> items;
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;