• hibernate 多对多单向


    多对多会利用中间表做外键的形式关联

     

    package com.bjsxt.hibernate;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;

    @Entity
    public class Student {
        private int id;
        private String name;
       
        @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>
        </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>

  • 相关阅读:
    js生成的字符串渲染到页面中如何添加换行符
    markdown文档插入emoji表情
    Notepad++ 运行.py文件
    git如何修改已提交的commit
    03_三维空间刚体运动(下)
    02_三维空间刚体运动(上)
    PointNet++: Deep Hierarchical Feature Learning on Point Sets in a Metric Space
    PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation
    12_视觉里程计1_ICP算法
    安全计算环境(四)终端设备5
  • 原文地址:https://www.cnblogs.com/flying607/p/3478410.html
Copyright © 2020-2023  润新知