Android Official ORM Database: @Embedded inline object (ii)

Source: Internet
Author: User

Android Official ORM Database: @Embedded inline Object (ii)

(i) Appendix 1 provides an overview of the basic use of the Android bedroom. In the Appendix 1 example, the user object elements are common Java basic data types, but in real development, persistent storage objects that are often built are complex and often structured Java objects that have a reference or inline relationship with each other.

The Android Support database table Java object @embedded a Java object inline with the callout character. This is like a previous ORM database, such as constructing a Java object called info, which is added to the user as a member variable:

Package Zhangphil.demo;import Android.arch.persistence.room.columninfo;import Android.arch.persistence.room.embedded;import Android.arch.persistence.room.entity;import android.arch.persistence.room.primarykey;/** * Created by Phil on 2017/11/22. */@Entity (tableName = "user_table") public class User {    @PrimaryKey (autoGenerate = true) public    int userId;    @ColumnInfo (name = "UserName") public    String name;    @ColumnInfo (name = "Userage") public    int age;    @ColumnInfo (name = "UpdateTime") public    long updatetime;    @Embedded public    info info;}

(b) The Info object itself is also an Android @entity. Also has its own column name and primary key, such as the complete Android data table features, Info.java:
Package Zhangphil.demo;import Android.arch.persistence.room.columninfo;import Android.arch.persistence.room.entity;import android.arch.persistence.room.primarykey;/** * Created by Phil on 2017/11 /23. */@Entity (tableName = "info_table") public class Info {    @PrimaryKey (autoGenerate = true) public    int infoid;    @ColumnInfo (name = "blog") public    String blog;    @ColumnInfo (name = "Content") public    String content;}

(c) It is important to note that the list name is automatically added to the host object two times by using the data table embedded in the @embedded symbol of the android bedroom. In this case, the user clock is embedded with info, then the user database table user_table will be automatically added to the Android room in the column Name field in info. User's user_table has userid,name,age,updatetime four columns, because @embedded the info, then the blog,content in info will be automatically added to the User data table. However, the primary key infoid of info will be ignored in user and is no longer used as the primary key.

(d) Write a Mainactivity.java test:
Package Zhangphil.demo;import Android.arch.persistence.room.room;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import Android.util.log;import Java.util.list;public class Mainactivity extends    appcompatactivity {private String TAG = "Output";        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);            New Thread (New Runnable () {@Override public void run () {databaseoperation ();    }}). Start (); } private void Databaseoperation () {Userdatabase muserdatabase = Room.databasebuilder (Getapplicationcontext (),        Userdatabase.class, "users"). Build ();        Userdao Muserdao = Muserdatabase.getuserdao ();        Write Database Writeuserdatabase (Muserdao, "Zhangphil", 18);        Readdatabase (Muserdao);    Close database Muserdatabase.close ();        } private void Readdatabase (Userdao dao) {log.d (TAG, "read database ..."); list<user> USers = Dao.getallusers ();        for (User u:users) {log.d (TAG, U.userid + "," + U.name + "," + U.age + "," +u.info.blog+ "," +u.info.content ");    } log.d (TAG, "read database completed.");        } private void Writeuserdatabase (Userdao dao, String name, int age) {Info info = new Info ();        Info.blog = "Http://blog.csdn.net/zhangphil";        Info.content = "Android";        User user = new user ();        User.Name = name;        User.age = age;        User.updatetime = System.currenttimemillis ();        User.info = info;    Dao.insertuser (user); }}


Code run result, Logcat output:

11-24 09:20:00.716 30805-30851/zhangphil.demo d/output: Read database ... 11-24 09:20:00.723 30805-30851/zhangphil.demo d/output: 1,zhangphil,18,http://blog.csdn.net/zhangphil,android11-24 09:20:00.723 30805-30851/zhangphil.demo d/Output: Read database completed.



Appendix:
1, "Android official ORM Database," Technical solution introduction (i) Link: http://blog.csdn.net/zhangphil/article/details/78611632
2, "Introduction to Android Ormlite database" link: http://blog.csdn.net/zhangphil/article/details/46878075
3, "Android ormlite foreigncollection Associated External Collection" Link: http://blog.csdn.net/zhangphil/article/details/46891021

Android Official ORM Database: @Embedded inline object (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.