zoukankan      html  css  js  c++  java
  • Android ORM对象关系映射之GreenDAO建立多表关联

    https://blog.csdn.net/u010687392/article/details/48496299

    利用GreenDAO可以非常方便的建立多张表之间的关联

    一对一关联

    通常我们在操作数据库的时候,我们往往不是单独的对一张表进行操作,而是对这张表的操作会联动的影响另外一张表或者多张表,比如:现在有两张表,一张是用户User表(有name、age、sex三个字段),一张是头像Picture表(有pictureId、pictureName、width、height四个字段)。假如用户表和头像表是一对一关系,一个用户只有一个头像,一个头像只能有一个用户,所以要建立这两张表之间的联系,这两张表肯定是需要关联的,这样就可以通过用户的信息得到它的头像信息。我们知道在数据库中,关联两张表(暂且不说多对多关系的)一般都是把一张表的主键作为另外一张表的外键来做的,所以在Android中也同样,如果我们要关联User表和Picture表,那么只需要把Picture表的主键(假如是pictureId)作为User表的外键即可,另外一个亦是如此,如: 
    这里写图片描述 
    假设还是以上面的场景为例,则利用GreenDAO建立User表和Picture表一对一的关联可以这样建立:

    //把User表的主键name作为Picture表的外键,把Picture的主键pictureId作为User表的外键,这样得到任何一个实体的信息都可以得到关联的另外一个实体的信息
            Property property =  user.addLongProperty("pictureId").getProperty();
            user.addToOne(picture,property);
    
            Property propertyName = picture.addStringProperty("name").getProperty();
            picture.addToOne(user,propertyName);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在为Schema添加实体的时候,我们在相应的实体中添加另外一个表的主键即可:

            Schema schema = new Schema(1,"com.sunzxyong.greendao2");
            //User
            Entity user = schema.addEntity("User");
            user.addStringProperty("name").notNull().primaryKey();
            user.addStringProperty("sex");
            //Picture
            Entity picture =  schema.addEntity("Picture");
            picture.addLongProperty("pictureId").primaryKey();
            picture.addStringProperty("pictureName").notNull();
            picture.addIntProperty("width");
            picture.addIntProperty("height");
            //建立一对一关联
            Property property =  user.addLongProperty("pictureId").getProperty();
            user.addToOne(picture,property);
    
            Property propertyName = picture.addStringProperty("name").getProperty();
            picture.addToOne(user,propertyName);
    
            new DaoGenerator().generateAll(schema, "../GreenDAODemo/app/src/main/java-gen");
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    【注意】:目前多表关联只能支持关联的表只能有一个主键,所以我们不能加入user.addIdProperty();或者在表中设置了两个主键,因为user.addIdProperty();默认会把id作为主键。所以当你的某张表存在与其它表关联时,你需要检查所关联的那张表是否只设置了一个主键,否则将会报错。

    当右键运行生成相应的实体后,我们可以打开User类: 
    发现多了一个pictureId属性,这正是User表的外键,Picture的主键,然后构造方法也需要我们传入pictureId的值: 
    这里写图片描述 
    这里写图片描述 
    User类中还提供了一个getPicture()方法,供我们直接得到当前User的Picture对象而得到相应信息,实际上它内部已经帮我们封装好了相应的查询方法,我们只需直接调用即可: 
    这里写图片描述 
    同样Picture类中也是这样。

    一对多关联

    大家都知道在超市购物时候,一位顾客可以有很多订单,而一个订单只能属于一位顾客,所以这就成了一对多的关系,假设顾客Customer表有customerId(primaryKey)、name两个属性,订单Order表有orderId(primaryKey)、money两个属性。 
    这里写图片描述 
    所以建立顾客和订单之间的一对多关联为:

            Schema schema = new Schema(1,"com.sunzxyong.greendao3");
            //顾客
            Entity customer = schema.addEntity("Customer");
            customer.addLongProperty("customerId").primaryKey();
            customer.addStringProperty("name").notNull();
    
            //订单
            Entity order = schema.addEntity("Order");
            order.addLongProperty("orderId").primaryKey();
            order.addDoubleProperty("money").notNull();
    
            //建立一对多关联(顾客对订单为一对多)
            Property property = order.addLongProperty("customerId").getProperty();
            order.addToOne(customer,property);
            customer.addToMany(order,property).setName("orders");
    
            new DaoGenerator().generateAll(schema, "../GreenDAODemo/app/src/main/java-gen");
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    当设置了顾客对订单一对多关联后,Order实体(和表)中会多一个属性为customerId,所以通过订单我们可以得到该顾客信息,而Customer实体(和表)中会多一个List集合变量:List<Order> orders,表示该顾客的所有订单,其中orders其实是我们自定义的名字,在刚刚setName("orders")就是给这个变量设置了“orders“名称,而Customer实体中还提供了一个方法getOrders()表示得到该顾客所有订单:

    List<Order> orders = customer.getOrders();
    • 1

    这里写图片描述 
    事实上它也是封装好了查询Order中顾客id为customerId的所有订单。

    多对多关联

    通常来说,在建立多对多关联上,我们都会采用新建一张中间表,利用中间表把多对多这种复杂关系简单化,在通常的选课系统上,一个学生可以选择多门课,一门课可以被多个学生选,这就是多对多关系了,假设Student有studentId、name两个属性,Course有courseId、courseName两个属性,则建立多对多关系为: 
    这里写图片描述

            Schema schema = new Schema(1,"com.sunzxyong.greendao4");
            //学生
            Entity student = schema.addEntity("Student");
            student.addLongProperty("studentId").primaryKey();
            student.addStringProperty("name").notNull();
    
            //课程
            Entity course = schema.addEntity("Course");
            course.addLongProperty("courseId").primaryKey();
            course.addStringProperty("courseName").notNull();
    
            //建立多对多关联
            Entity studentCourse = schema.addEntity("StudentCourse");
            Property studentId =  studentCourse.addLongProperty("studentId").getProperty();
            Property courseId =  studentCourse.addLongProperty("courseId").getProperty();
            studentCourse.addToOne(student,studentId);
            studentCourse.addToOne(course,courseId);
            student.addToMany(studentCourse, studentId);
            course.addToMany(studentCourse,courseId);
    
            new DaoGenerator().generateAll(schema, "../GreenDAODemo/app/src/main/java-gen");
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    这样就建立学生和课程表多对多的关联,学生实体和课程实体中都有这么一个方法:

    public List<StudentCourse> getStudentCourseList(){
        //...
    }
    • 1
    • 2
    • 3

    意思就是得到一个StudentCourse的集合,而StudentCourse实体中又有这么两个方法:

     public Student getStudent(){//...}
     public Course getCourse(){//...}
    • 1
    • 2

    所以当我们得到了StudentCourse的List集合,我们可以通过StudentCourse中的这两个方法来得到对应的学生或者课程信息。

  • 相关阅读:
    一个简短的yahoo YUI介绍
    备忘录
    海量数据库查询
    asp.net webshell 解决 方案
    IE=EmulateIE7 标签的作用
    WINDOWS 2003 IIS网站防木马权限设置安全配置整理
    C语言各种函数
    虚函数的各种情况
    equals与==关于Object覆盖和重载问题
    类型转换函数转换为构造类型
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/9078252.html
Copyright © 2011-2022 走看看