首页 > 代码库 > hibernate之关于多对一单向关联映射

hibernate之关于多对一单向关联映射


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


在项目的开发中多对一的单向关联映射是最常见的关联映射!这个着重详细讲解一下!

例如,我们现在一个组(Group)和人(Person)

idname
1张三
2李四
(person表)

idnamep_id
1财务组1
2财务组2

(Group表)                                  

由上表我们发现,是不是出现数据的重复?财务组在重复!

所以,在多对一的单向映射中,我们通常是在多的一方加上外键来关联少的一方。

那么在这个关系中(Group)是少的一方,(Person)是多的一方,一个组里面可以有多个人!

idname
1财务组
2开发组

(person表)

idnameG_id
1张三1
2李四1

(Group表)  

上面这种关系才是正常的业务逻辑!

ok,下面来看我们的Annotations配置!

@Entity
@Table(name="t_user")
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="name")
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
    @Column(name="age")
    public Integer getAge() {
        returnage;
    }
    publicvoid setAge(Integerage) {
       this.age = age;
    }
}

@Entity
@Table(name="t_group")
publicclass Group {
    private Integer id;
    private String name;
     
    @Id
    @GeneratedValue
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    @Column(name="name")
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
}

XML配置方法

package csg.hibernate.entity;
  
publicclass Person {
    private Integer id;
    private String name;
    private Integer age;
    private Group group;
     
    public Group getGroup() {
       returngroup;
    }
    publicvoid setGroup(Groupgroup) {
       this.group = group;
    }
    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;
    }
}

package csg.hibernate.entity;
  
publicclass Group {
    private Integer id;
    private String name;
     
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
}

<?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_user">
       <id name="id">
           <generator class="native" />
       </id>
       <property name="name" />
       <property name="age"/>
       <many-to-one name="Group" column="group_id"/>
    </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="Group" table="t_group">
       <id name="id">
           <generator class="native"/>
       </id>
       <property name="name" />
    </class>
</hibernate-mapping>

到这里我们使用Junit测试一下!


publicclass JuniTest {
privatestatic SessionFactory sessionFactory;
     
    @BeforeClass
    publicstaticvoid beforeClass() {
       Configuration cfg=new Configuration();
       cfg.configure();
       sessionFactory=cfg.buildSessionFactory();
    }
    
    @Test
    public  void add(){
       try {
           Group group=new Group();
           Session session = sessionFactory.getCurrentSession();
           session.beginTransaction();
           group.setName("部门");
           session.save(group);
           Person person=new Person();
           person.setAge(12);
           person.setName("张三");
           person.setGroup(group);
           session.save(person);
           session.getTransaction().commit();
       } catch (HibernateException e){
           e.printStackTrace();
       }
        
    }
     
    @AfterClass
    publicstaticvoid afterClass() {
       sessionFactory.close();
    }
}

ok!


hibernate之关于多对一单向关联映射