zoukankan      html  css  js  c++  java
  • hibernate 多对多双向关联

    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"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <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"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <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>

  • 相关阅读:
    react-webpack
    react
    重置手机过程
    运用 Node-RED 开发 LoRa 应用
    IBM Node-RED 安装与使用教程
    Node-RED 入门教程:简介
    Thingsboard 3.0 修改 Title、默认语言、主题颜色
    Thingsboard 3.0 通过 tb-gateway 网关接入 MQTT 设备教程
    Thingsboard 入门学习笔记:属性
    ThingsBoard 3.0 版本发布
  • 原文地址:https://www.cnblogs.com/flying607/p/3478455.html
Copyright © 2011-2022 走看看