Hibernate之关于多对多单向关联映射


【Hibernate】之关于多对多单向关联映射


老师和学生,最典型的多对多关联,

Teacher和Student,所谓单向意思就是说,老师知道自己的教的是哪些学生而学生不知道是哪些老师教。也可以这么说,在查询的时候,通过老师可以级联查询出学生,但是通过学生不可以级联查询出老师!


而多对多最麻烦的是怎么自定义我们中间表的,表名和列名,这个是重要的!


Annotations配置

@Entity
@Table(name="t_teacher")
publicclass Teacher {
    private Integer id;
    private String name;
    private Set<Student> students=new HashSet<Student>();//set不允许重复,最适合数据库模型
    @Id
    @GeneratedValue
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    @Column(name="t_name")
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
    @ManyToMany
    @JoinTable(name="t_s_two",//自定义表名
           joinColumns={@JoinColumn(name="teacher_id")},//自定义列名
           inverseJoinColumns={@JoinColumn(name="student_id")})//反转,和Teacher对应的那个表的列名,也是自定义
    public Set<Student>getStudents() {
       returnstudents;
    }
    publicvoidsetStudents(Set<Student> students) {
       this.students = students;
    }
}

@Entity
@Table(name="t_student")
publicclass Student {
    private Integer id;
    private String name;
    private Integer age;
    @Id
    @GeneratedValue
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    @Column(name="s_name")
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
    @Column(name="s_age")
    public Integer getAge() {
       returnage;
    }
    publicvoid setAge(Integerage) {
       this.age = age;
    }
}

XML配置


publicclass Teacher {
    private Integer id;
    private String name;
    private Set<Student> students=newHashSet<Student>();//set不允许重复,最适合数据库模型
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
    public Set<Student>getStudents() {
       returnstudents;
    }
    publicvoidsetStudents(Set<Student> students) {
       this.students = students;
    }
}

publicclass Student {
    private Integer id;
    private String name;
    private Integer age;
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
        this.id = id;
    }
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
    public Integer getAge() {
       returnage;
    }
    publicvoid setAge(Integerage) {
       this.age = age;
    }
}

<?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 package="csg.hibernate.entity">
    <class name="Teacher" table="t_teacher">
       <id name="id">
           <column name="id"/>
           <generator class="native" />
       </id>
       <property name="name" />
       <set name="students" table="t_s_two"><!—-自定义表名-->
           <key column="teacher_id"/><!—自定义列名-->
           <many-to-many class="csg.hibernate.entity.Student" column="student_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 package="csg.hibernate.entity">
    <class name="Student" table="t_student">
       <id name="id">
           <column name="id"/>
           <generator class="native" />
       </id>
       <property name="name" />
       <property name="age" />
    </class>
</hibernate-mapping>


以上就是XML的many-to-many(单向)的配置方法,ok!





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值