2016-07-14 4 views
1

私はspring jpa/bootの新人です。 SpringブートアプリケーションでJPA/Hibernateを使用してStudentStudentCourseの間に1対多数の関係を作成しました。JPA /春のブートアプリケーションでは存在しないテーブルに挿入する休止状態

データベーステーブルはstudentとstudent_courseです。

SchoolApplication.javaを実行すると、エラーが発生して「テーブルまたはビューが存在しません」というエラーが発生します。

テーブルを作成していないので、student_course_liststudentstudent_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 
    ..... 
    ..... 

StudentCourse

@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.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; 
+0

エンティティがアプリケーションによってスキャンされていませんでした。 Spring Data JPAリポジトリを使用していますか? –

+0

@Rae、私は、JPAの依存関係を使用しています----------- \t \t \t org.springframework.boot \t \t \t 春ブート・スターター・データ-JPA \t \t ---------私が使用しなければならない------------ org.springframework.data 春-データ-JPA 1.10.2.RELEASE -------代わりに? – Muhammad

+0

あなたはhibernateに関係を永続化する方法を教えていないので、追加のテーブルが必要であると仮定しています。あなたの 'StudentCourse'には' Long studentId'だけが 'Long courseId'と同じです。それらは 'Student'と' Course'オブジェクトでなければなりません。そして '@ OneToMany'に' mappedBy = 'student'を追加することができます。 –

答えて

0

あなたの関係のためにJoinColumn(またはJoinTableManyToMany)を定義しませんでした。 Hibernateはstudent.courseList関係プロパティのテーブルを作成しようとしています。

変更あなたのStudentクラスは、次のように次のように

// You shouldn't need targetEntity, as your collection has the TargetEntity. 
// this parameter is only needed if the Generic type is not present 
// (e.g. private List courseList) 
@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "student") 
private List<StudentCourse> courseList; 

があなたのStudentCourseクラスを変更します。

// Replace studentId with a Relationship 
@JoinColumn(name="student_id") 
@ManyToOne 
private Student student; 

これは学生のために関係があることHibernateに教えてくれます - > StudentCourseは、学生が所有し、マッピングされていますstudent_courseテーブルのstudent_id列で指定します。

+0

student_courseテーブルのstudent_idには動作していません。 – Muhammad

+0

'StudentCourse 'に' Student'を設定しましたか?あなたは何をして見ているのかの例を提示できますか? – JudgingNotJudging

+0

はい、私は変更を提案しました。また、いくつかの追加属性(nullable = false、updatable = false、insertable = true)を追加しました。更新されたStudentCourse.javaを参照してください。 studentとstudent_courseのテーブルにはデータを挿入していますが、student_id(外部キー)は挿入されていません。 – Muhammad

0

あなたはエンティティをスキャンするようにアプリケーションを言っていません。メインクラスに@EntityScanアノテーションを追加します。例えば

@SpringBootApplication 
@EntityScan(basePackages={"com.example.model", "com.example.student.model"}) 
public class SchoolApplication implements CommandLineRunner { 
    ... 
} 

ところで、@ComponentScan@SpringBootApplicationが十分で、冗長です。 SpringBootApplicationクラスを見ると、@ComponentScanが表示されます。

+0

なぜEntityScanを追加する必要がありますか?彼は彼のrunメソッドでトランザクションを持っているので?または理由は何ですか? – Patrick

+0

@EntityScanを追加しましたが、同じエラーが発生しました – Muhammad

+1

'@ SpringBootApplication'がエンティティの親パッケージ内にあり、依存関係リストにSpring JPAライブラリがある限り、これは必須ではありません。 – JudgingNotJudging

関連する問題