博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate 多对多双向关联
阅读量:5322 次
发布时间:2019-06-14

本文共 2573 字,大约阅读时间需要 8 分钟。

package com.bjsxt.hibernate;

import java.util.HashSet;

import java.util.Set;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity

public class Student {
    private int id;
    private String name;
    private Set<Teacher> teachers = new HashSet<Teacher>();
    @ManyToMany(mappedBy="students")
    public Set<Teacher> getTeachers() {
        return teachers;
    }
    public void setTeachers(Set<Teacher> teachers) {
        this.teachers = teachers;
    }
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 

 

package com.bjsxt.hibernate;

import java.util.HashSet;

import java.util.Set;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity

public class Teacher {
    private int id;
    private String name;
    private Set<Student> students = new HashSet<Student>();
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @ManyToMany
    @JoinTable(name="t_s",
        joinColumns={@JoinColumn(name="teacher_id")},
        inverseJoinColumns={@JoinColumn(name="student_id")}
        )
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }
}

 

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        ">

<hibernate-mapping>

    <class name="com.bjsxt.hibernate.Student">
        <id name="id">
            <generator class="native"></generator>
        </id>
       
        <property name="name"></property>
        <set name="teachers" table="t_s">
            <key column="student_id"></key>
            <many-to-many class="com.bjsxt.hibernate.Teacher" column="teacher_id"/>
        </set>
    </class>
   
</hibernate-mapping>

 

 

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        ">

<hibernate-mapping>

    <class name="com.bjsxt.hibernate.Teacher">
        <id name="id">
            <generator class="native"></generator>
        </id>
       
        <property name="name"></property>
        <set name="students" table="t_s">
            <key column="teacher_id"></key>
            <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/>
        </set>
    </class>
   
</hibernate-mapping>

转载于:https://www.cnblogs.com/flying607/p/3478455.html

你可能感兴趣的文章
redis 数据库架构安装部署及操作
查看>>
mysql 架构安装部署及操作
查看>>
shell
查看>>
路由器怎么设置无线上网
查看>>
使用MBROSTool 工具制作U盘多启动盘的方法总结
查看>>
使用的组件:Jcrop
查看>>
itnba试做
查看>>
HDU-5776 Sum
查看>>
Hadoop初期学习和集群搭建
查看>>
《必然》五、屏幕只把我们变成近视眼吗,远远不止
查看>>
Hadoop-2.2.0集群安装配置实践
查看>>
cache max-age tags context
查看>>
VMware虚拟机上centos安装
查看>>
maven setting.xml
查看>>
Java资源大全中文版(Awesome最新版)
查看>>
Dom4j的使用(全而好的文章)
查看>>
Vivatesting
查看>>
JDBC自动生成主键值
查看>>
Delphi XE8帮助中的REST相关内容。
查看>>
结构和其他数据形式
查看>>