java - 使用额外列休眠多对多 xml 映射
问题描述
我正在尝试对休眠和与 xml 的关系进行一些练习,并且一切正常,但现在我想知道如何在我的连接表上实现额外的列,但我找不到方法。
这是我的代码:
成绩 hbm xml
<hibernate-mapping>
<class name="Grade" table="Grades" catalog ="test">
<id name="id">
<column name = "id"/>
<generator class="increment"/>
</id>
<property name ="name"></property>
<property name ="code"></property>
</class>
</hibernate-mapping>
学生 hbm xml
<hibernate-mapping>
<class name="Student" table="Students" catalog ="test">
<id name="id">
<column name = "student_id"/>
<generator class="increment"/>
</id>
<property name ="name"></property>
<property name="years" type ="integer"></property>
<set name="grade" table="student_grades" cascade="all">
<key column="student_id" not-null="true" />
<many-to-many column="grade_id" class="Grade"/>
</set>
</class>
</hibernate-mapping>
等级.java
public class Grade implements Serializable{
private Long id;
private String name;
private String code;
public Grade () {
}
public Grade (String name, String code) {
this.name= name;
this.code= code;
}
getters and setters
学生.java
public class Student implements Serializable{
private Long id;
private String name;
private int years;
private Set<Grade> grade;
public Student() {
}
public Student(String name, int years, Set<Grade> grade) {
super();
this.name= name;
this.years= years;
this.grade= grade;
}
主要的
public class Main {
public static void main(String[] args) {
Configuration cfg =new Configuration().configure();
SessionFactory sessionFactory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder().configure().build());
Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
Grade m1 = new Grade("Test A", "01");
session.save(m1);
HashSet<Grade> set1 = new HashSet<Grade>();
set1.add(m1);
Student a = new Student("Richard", 26, set1);
session.save(a);
...
有了这个,我有一个名为“student_grades”的表,其中包含 student_id 和grade_id,每个学生的成绩都在其中,但我也想在该表上显示成绩名称和学生姓名。有什么办法吗?
谢谢
解决方案
据我所知,连接表上的附加列对于多对多关系是不可能的。我猜你需要两个一对多的关系,并在你的数据库结构中手动添加 student_grades。
另外,也许看看这里:Mapping many-to-many association table with extra column(s)
推荐阅读
- reactjs - 如何在表单提交时重置下拉值,其他输入值在 React Hooks 中被清除
- python - 无法从scrapy中的所有页面获取数据
- javascript - 如何将ajax响应的输出放在codeigniter的模态iframe中
- c# - 如何在 Blazor 中绑定单选按钮?
- r - R中是否有一个函数可以将大混淆矩阵减少为R中的2x2正负级混淆矩阵?
- themes - Shopify 主题编辑器在为主页创建部分后崩溃
- java - 是否可以跨多个显示器打开 Jgrasp 文件?
- pytorch - Huggingface 预训练的 BERT 模型不起作用
- android - 在 Android Studio 4.1 中构建失败
- mapping - 如何在 Dyalog APL 中操作多个嵌套数组?