봄 jpa/boot를 처음 사용합니다. 스프링 부트 애플리케이션에서 JPA/Hibernate를 사용하여 Student
과 StudentCourse
사이에 일대 다 관계를 만들었습니다.스프링 부트 응용 프로그램에서 JPA/Hibernate를 기존 테이블에 삽입하지 않음
데이터베이스 테이블은 student_course입니다.
SchoolApplication.java
을 실행하면 오류가 발생하여 '테이블 또는 뷰가 존재하지 않습니다'라는 메시지가 표시됩니다.
student_course_list
테이블을 만들지 않았으므로 student
및 student_course
테이블에만 데이터를 삽입하고 싶기 때문에 유효한 오류입니다.
왜 최대 절전 모드가이 추가 테이블을 찾고이 문제를 해결할 수 있는지 조언 해 주실 수 있습니까?
로그인
Hibernate:
insert
into
student_course_list
(student, course_list)
values
(?, ?)
Caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:242) ~[spring-orm-4.2.6.RELEASE.jar:4.2.6.RELEASE]
SchoolApplication.java
package com.school;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.Transactional;
import com.school.domain.Student;
import com.school.domain.StudentCourse;
import com.school.service.StudentServiceInterface;
@EntityScan(basePackages={"com.school.domain"})
@SpringBootApplication
public class SchoolApplication implements CommandLineRunner {
@Autowired
protected StudentServiceInterface studentService;
public static void main(String[] args) {
SpringApplication.run(SchoolApplication.class, args);
}
@Override
@Transactional
public void run(String... strings) throws Exception {
StudentCourse sc1 = new StudentCourse();
sc1.setCourseId(new Long(1));
sc1.setEndDate(null);
StudentCourse sc2 = new StudentCourse();
sc2.setCourseId(new Long(2));
sc2.setEndDate(null);
//Course List
List<StudentCourse> studentCourse = new ArrayList();
studentCourse.add(sc1);
studentCourse.add(sc2);
Student student = new Student();
student.setFirstName("test first");
student.setLastName("test last");
student.setCourseList(studentCourse);
studentService.saveStudent(student);
}
}
학생
@Entity
@Table(name="student")
public class Student {
@Id
@Column(name="student_id")
@GeneratedValue(strategy=GenerationType.AUTO, generator="student_seq_gen")
@SequenceGenerator(name="student_seq_gen", sequenceName="SEQ_STUDENT")
private Long studentId;
@OneToMany(targetEntity=StudentCourse.class, cascade = {CascadeType.ALL})
private List<StudentCourse> courseList;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="gender")
private char gender;
@Column(name="date_of_birth")
private Date dateOfBirth;
@Column(name="email")
private String email;
@Column(name="city")
private String city;
// getter and setters
.....
.....
는
@Entity
@Table(name="student_course")
public class StudentCourse {
@Id
@Column(name="student_course_id")
@GeneratedValue(strategy=GenerationType.AUTO, generator="student_course_seq_gen")
@SequenceGenerator(name="student_course_seq_gen", sequenceName="SEQ_STUDENT_COURSE")
private Long studentCourseId;
@Column(name="student_id")
private Long studentId;
@Column(name="course_id")
private Long courseId;
@Column(name="end_date")
private Date endDate;
// getter and setters
.....
.....
UPDATE를 StudentCourse :
StudentCourse.java :
@Entity
@Table(name="student_course")
public class StudentCourse {
@Id
@Column(name="student_course_id")
@GeneratedValue(strategy=GenerationType.AUTO, generator="student_course_seq_gen")
@SequenceGenerator(name="student_course_seq_gen", sequenceName="SEQ_STUDENT_COURSE")
private Long studentCourseId;
@JoinColumn(name="student_id", nullable = false, updatable = false, insertable = true)
@ManyToOne
private Student student;
@Column(name="course_id")
private Long courseId;
@Column(name="end_date")
private Date endDate;
엔티티가 응용 프로그램에서 스캔하지 못했습니다. Spring Data JPA 저장소를 사용하고 있습니까? –
@Rae, 나는 \t \t \t org.springframework.boot -----------, \t \t \t 스프링 부팅 스타터 데이터 JPA \t을 JPA 의존성을 사용하고 있습니다 \t는 --------- ------------ 제가 org.springframework.data 스프링 데이터 JPA 1.10.2.RELEASE를 사용한다 ------- 대신? –
Muhammad
당신은 hibernate에게 관계를 유지하는 방법을 알려주지 않았으므로 추가 테이블이 필요하다고 가정했다. 'Long courseId'에 대해서만'StudentCourse'에 'Long studentId'가 있습니다. 그것들은'Student'와'Course' 객체 여야하고'@ OneToMany'에'mappedBy ='student'를 추가 할 수 있습니다. –