hibernate - 在关系表中自动填充数据
问题描述
我正在开发一个 spring boot 应用程序,它为给定的学生创建学生帐户。该结构具有由注释创建的一对多关系表。现在,每次我创建一个新的学生帐户时,student_id
andstudent_account_id
必须(自动)保存在这个关系表中,它只包含student_id
andstudent_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;
学生账户类:
@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);
}
}
我用来保存对象的存储库中的保存方法是来自 CrudRepository 的内置保存方法。我应该编写自己的保存方法并手动将数据插入到关系表中吗?
解决方案
首先感谢@JBNizet 和@AlanHay 的支持。
问题出在@OneToMany
注释中,映射错误。有关如何以正确方式进行映射的良好来源,请访问此链接。一旦我修复了项目工作的错误映射,它就会自动插入数据但使用错误的外键。
这是从我的项目中的 data.sql 文件创建数据库的结果。我所做的是删除这个文件。我正在使用 spring boot web mvc,所以我从模板文件夹和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;
学生账户类:
@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> {
}
就是这样。谢谢您的帮助!!
推荐阅读
- node.js - 如何配置释放切换服务器以在 Azure 应用服务中通过 HTTPS 工作?
- javascript - 获取 ESLint:不必要的转义字符
- java - Java - 定义几个自定义异常而不重写每个异常(干)
- java - @ManyToOne 没有在 Child 表中保留 Parent FKey 列
- javascript - 如何处理对客户端反应应用程序的外部重定向?
- javascript - 为什么 .indexof() 返回 -1;当项目存在于给定数组中时?| 谷歌脚本
- mysql - MySQL 在另一个查询中使用查询
- symfony4 - 如何将 hydra apiplatform 文档转换为 Swagger 规范 (OAS3)?
- sql-server - 我的数据库中的数据显示为问号
- flutter - 在构建函数之外使用 BuildContext