在关系表中自动填充数据

时间:2019-05-08 22:15:33

标签: hibernate spring-boot jpa

我正在开发一个Spring Boot应用程序,该应用程序为给定的学生创建学生帐户。该结构具有由注释创建的一对多关系表。现在,每次创建新的学生帐户时,student_idstudent_account_id必须 (自动) 保存在此关系表中,该表仅包含student_idstudent_account_id作为外键。我不知道该怎么实现。这是代码:

学生班:

@Entity
@Table(name = "student")
public class Student implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "student_id")
    private int studentId;
    @Column(name = "name")
    private String name;

    @ManyToOne
    @JoinTable(name = "relationship_table",
               joinColumns =  @JoinColumn(name = "student_id"),
               inverseJoinColumns = @JoinColumn(name = "student_account_id"))
    private StudentAccount studentAccount;

StudentAccount类:

@Entity
@Table(name = "student_account")
public class StudentAccount implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "student_account_id", nullable = false)
private int studentAccountId;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "relationship_table",
           joinColumns =  @JoinColumn(name = "student_account_id"),
           inverseJoinColumns = @JoinColumn(name = "student_id"))
private Collection<Student> students;

编辑: 这是我如何构建student_account_object并保存它:

@Override
    public StudentAccount save(StudentAccount studentAccount) {
        return studentAccountRepository.save(studentAccount);
    }

public void createStudentAccount(List<Student> studentsWithoutAccount) {
        for (int i=0; i<studentsWithoutAccount.size(); i++){
            String name = studentsWithoutAccount.get(i).getName();

            String username = name+"student";
            String password = username;

            StudentAccount studentAccount= new StudentAccount(username, password);
            save(studentAccount);

        }
    }

我用来保存对象的存储库中的save方法是CrudRepository的内置保存方法。 我应该编写自己的保存方法并将数据手动插入关系表中吗?

1 个答案:

答案 0 :(得分:0)

首先感谢@JBNizet和@AlanHay的支持。

问题出在@OneToMany批注中,映射不正确。有关如何正确映射的详细信息,请转到this link。一旦我修复了错误的映射,该项目便开始工作,它会自动插入数据,但使用了错误的外键。

这是从我的项目中的data.sql文件创建数据库的结果。我所做的是擦除此文件。我正在使用Spring Boot Web MVC,因此从 templates 文件夹和 target / classes 中删除了data.sql文件。

然后,我按照实体Student student =new Student();StudentAccount studentAccount = new StudentAccount();的方式将实体持久保存到数据库中。

代码如下:

学生班:

@Entity
@Table(name = "student")
public class Student implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "student_id")
    private int studentId;
    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
    private Collection<StudentAccount> studentAccounts;

StudentAccount类:

@Entity
@Table(name = "student_account")
public class Users implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "student_account_id", nullable = false)
    private int studentAccountId;
    @Column(name = "username")
    private String username;
    @Column(name = "password")
    private String password;

    @ManyToOne
    @JoinTable(name = "relationship_table",
            joinColumns =  @JoinColumn(name = "student_id"),
            inverseJoinColumns = @JoinColumn(name = "student_account_id"))
    private Student student;

实施方法:

@Autowired
private StudentAccountRepository studentAccountRepository;
@Autowired
private StudentRepository studentRepository;
    public void createDatabase(){
            List<Student> students= new ArrayList<Student>();
            List<StudentAccount> studentAccounts= new ArrayList<StudentAccount>();

            Student student1  = new Student ("john");
            Student student2  = new Student ("david");
            Student student3  = new Student ("emma");
            Student student4  = new Student ("sophia");
            Student student5  = new Student ("mia");

            students.add(student1);
            students.add(student2);
            students.add(student3);
            students.add(student4);
            students.add(student5);

            studentRepository.saveAll(students);

            StudentAccount studentAccount1 = new StudentAccount("john", "john", student1);
            StudentAccount studentAccount2 = new StudentAccount("david", "david", student2);
            StudentAccount studentAccount3 = new StudentAccount("emma", "emma", student3);
            StudentAccount studentAccount4 = new StudentAccount("sophia", "sophia", student4);
            StudentAccount studentAccount5 = new StudentAccount("mia", "mia", student5);

            studentAccounts.add(studentAccount1);
            studentAccounts.add(studentAccount2);
            studentAccounts.add(studentAccount3);
            studentAccounts.add(studentAccount4);
            studentAccounts.add(studentAccount5);

            studentAccountRepository.saveAll(studentAccounts)
        }
**StudentRepository class:**

    @Repository
    public interface StudentRepository extends CrudRepository<Student, Integer> {

    }
**StudentAccountRepository class:**

    @Repository
    public interface StudentAccountRepository extends CrudRepository<StudentAccount, Integer> {
    }

就是这样。谢谢您的帮助!