• Hibernate在关于一对多,多对一双向关联映射




    【Hibernate】之关于一对多,多对一双向关联映射


    因为一对多。和多对一的双向关联映射基本上一样,所以这里就一起写下来!

    Annotations配置

    @Entity
    @Table(name="t_group")
    publicclass Group {
        private Integer id;
        private String name;
        private Set<Person> persons=newHashSet<Person>();//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;
        }
        @OneToMany(mappedBy="group")//意识是告诉Hibernate的关联关系应该设在Person,多的一方
    //  仅仅有OneToOne,OneToMany,ManyToMany上才有mappedBy属性,ManyToOne不存在该属性;
        public Set<Person>getPersons() {
           returnpersons;
        }
        publicvoidsetPersons(Set<Person> persons) {
           this.persons = persons;
        }
    }

    @Entity
    @Table(name="t_person")
    publicclass Person {
        private Integer id;
        private String name;
        private Integer age;
        private Group group;
         
        @ManyToOne
        public Group getGroup() {
           returngroup;
        }
        publicvoid setGroup(Groupgroup) {
           this.group = group;
        }
        @Id
        @GeneratedValue
        public Integer getId() {
           returnid;
        }
        publicvoid setId(Integerid) {
           this.id = id;
        }
        @Column(name="p_name")
        public String getName() {
           returnname;
        }
        publicvoid setName(Stringname) {
           this.name = name;
        }
        @Column(name="p_age")
        public Integer getAge() {
           returnage;
        }
        publicvoid setAge(Integerage) {
           this.age = age;
        }
    }

    XML配置


    <?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="Group" table="t_group">
           <id name="id">
               <column name="id"/>
               <generator class="native" />
           </id>
           <property name="name" />
           <set name="persons">
               <key column="groupId"/>
               <one-to-many class="csg.hibernate.entity.Person"/>
           </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="Person" table="t_person"> <id name="id"> <column name="id"/> <generator class="native" /> </id> <property name="name" /> <property name="age" /> <many-to-one name="group" column="groupId"/> </class> </hibernate-mapping>


    由上大家都能够得出疑问?

    为什么双向关联在Groupkey须要指定column而在Person中也须要指定column呢?

    假设当中一个不指定会有什么后果?

    假设指定的column不一致又会有什么后果?

    大家能够试一下!





    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    C#连接、访问MySQL数据库
    对列表遍历同时增删操作时注意事项 c
    Linux 操作命令 c
    Memcached 入门 转载与ferreousbox
    转载http://zxiaofei168.iteye.com/blog/1044843 spring事务管理
    spring mvc 学习 转载
    自定义Annotation
    Java的路径问题 转载
    设置火狐调试时间
    vi/vim 基本使用方法(转载)
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4835947.html
Copyright © 2020-2023  润新知