litepal数据表关联

最近用litepal操作数据很方便,记录下表关联
1. 一对一
song和introduction 即音乐和简介 Song中持有introduction 的引用,introduction 持有Song的引用,就建立了一对一关系,
- 增 如下面代码所示
- 删 删除song一行,对应的introduction 自动删除,反之亦然。
- 改 修改introduction 某行,对应的song自动被修改

Introduction introduction = new Introduction();
introduction.setTitle("这是song1111 介绍");
Song song1 = new Song();
song1.setTitle("song111");
song1.setIntroduction(introduction);
introduction.save();
song1.save();
Song songData = DataSupport.findLast(Song.class, true);
Introduction introductionData = DataSupport.findLast(Introduction.class, true);
Log.e(TAG, "onCreate:songData " + songData);
Log.e(TAG, "onCreate:introductionData " + introductionData);
  1. 多对一
    歌曲和评论 多对一

歌曲持有评论的集合即可
- 增 见代码
- 删 删多的一方,一的一方自动修改
- 改 同上

    Song song1 = new Song();
        song1.setTitle("song111");
        Commment comment1 = new Commment();
        comment1.setTitle("111");
        Commment comment2 = new Commment();
        comment2.setTitle("222");
        song1.getCommentList().add(comment1);
        song1.getCommentList().add(comment2);
        comment1.save();
        song1.save();
  1. 多对多
Song song1 = new Song();
        song1.setTitle("song111");
        song1.save();


        Song song2 = new Song();
        song2.setTitle("song222");
        song2.save();


        Category category1 = new Category();
        category1.setTitle("111");
        category1.getSongArrayList().add(song1);
        category1.getSongArrayList().add(song2);
        category1.save();

        Category category2 = new Category();
        category2.setTitle("222");
        category2.getSongArrayList().add(song1);
        category2.getSongArrayList().add(song2);
        category2.save();

你可能感兴趣的:(android,app)