zoukankan      html  css  js  c++  java
  • hibernate之多对一单向关联

            一个工作组(Group)里能够有多个用户(User),一个User仅仅属于一个Group,这是典型的多对一的关系。

    在多对一的关系中正确的数据库设计是在多的这方(在这里是User这方)加一个Group的外键。假设数据库设计的与之相反就会产生冗余。请看以下这个样例:

    友情提示:这是错误的设计方法:

    GroupId

    GroupName

    UserId

    1

    Group_1

    1

    1

    Group_1

    2

     

    UserId

    UserName

    1

    moluo

    2

    xingzhe

            这样在一的这方(也就是Group这方)设置外键关联,就会产生冗余(一个Group有N名User,这样设计就得反复N次冗余的GroupId和GroupName),因此在多对一时,数据库的设计是必须在多的这方加一的那方的外键!

    先写Annotation版本号的多对一单向关联:

    先建Group类:

    package com.hibernate.model;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="_Group")
    public class Group {
    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;
    }
    
    }
    
    再建User类:

    package com.hibernate.model;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    
    
    @Entity
    @Table(name="_User")
    public class User {
    private int id;
    private String name;
    private Group group;
    @ManyToOne
    public Group getGroup() {
    	return group;
    }
    public void setGroup(Group group) {
    	this.group = group;
    }
    @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;
    }
    
    }
    
    在多的这方加group的外键,并在get方法上注明:@manyToOne
    配置文件:

    <mapping class="com.hibernate.model.User"/> 
    <mapping class="com.hibernate.model.Group"/>
    測试用例:

    package com.hibernate.model;
    
    import static org.junit.Assert.*;
    
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    public class ORMappingTest {
    	public static SessionFactory sf = null;
    	//@BeforeClass
    	public static void beforeClass(){
    		try{
    			sf = new AnnotationConfiguration().configure().buildSessionFactory();
    		}
    		catch(Exception e) {
    			e.printStackTrace();
    		}
    		finally{	
    		}
    	}
    
    	@Test
    	public void testSchemaExport(){
    		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
    	}
    	
    	//@AfterClass
    	public static void afterClass(){
    		sf.close();
    	}
    
    
    }
    
    先写xml版本号的多对一单向关联:
    Group和User类以及測试用例同上。不再赘述

    我把映射文件贴出来:

    User.hbm.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="com.hibernate.model"> <class name="User" table="_User"> <id name="id"> <generator class="native"/> </id> <property name="name"></property> <many-to-one name="group" column="groupId"></many-to-one> </class> </hibernate-mapping>

    Group.hbm.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="com.hibernate.model"> <class name="Group" table="_Group"> <id name="id"> <generator class="native"/> </id> <property name="name"></property> </class> </hibernate-mapping>


    尊重版权,转载请注明本文链接

                                           欢迎关注行者摩罗微信公众号(xingzhemoluo),共同交流编程经验,扫描下方二维码就可以;





  • 相关阅读:
    pig中将两列合并为一列:concat
    最小二乘法拟合二元多次曲线
    动态重新加载Class机制之代码测试
    從 Windows Form ComboBox、ListBox 或 CheckedListBox 控制項加入或移除項目
    C#控件一览表
    C#中combobox 和TreeView控件属性、事件、方法收集
    PHP 分页类 潇湘博客
    一个房屋中介业务建模的实例分析
    使用Limit参数优化MySQL查询 潇湘博客
    word中的字号与实际的字体大小一一对应的关系
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6881323.html
Copyright © 2011-2022 走看看