Android ormlite foreigncollection Associated External collection

Source: Internet
Author: User

??

"Android ormlite foreigncollection Associated External collections"

Android Ormlite Foreigncollection The ability to correlate external collections to data structures or collections that are deeper and more hierarchical, and that data models penetrate and intersect each other. It is especially convenient to solve complex data models. During the simple period, we take the class <-> student data model as an example to illustrate. There are several students in a class (one to many, 1<-n). Conversely, a number of students are assembled into a class (n->1). In Android Ormlite. This structural model can be used @foreigncollectionfield. Foreigncollection modeling. We define a class aclass (the reason for adding a ' a ' before ' class ' is because of the Java language. ' Class ' is a reserved word, but it is just the class we want to use English words, it is unfortunate, has been added a ' a ' evasion. AClass includes ID (primary key, easy to query and update), name, and a collection of external Student (foreigncollection<student>).

Same. We define student class student. A field is embedded in the student AClass a aclass that points to the outside.

Note:
Android Ormlite Simple Introduction article: http://blog.csdn.net/zhangphil/article/details/46878075

The demo sample code has a total of 4 files: Mainactivity.java, main activity, for testing. Ormlitedatabasehelper.java, Aclass.java, Student.java is a database-related code file that is used for modeling.

Structure level



Test the Mainactivity.java:

Package Zhangphil.ormlitetest;import Java.sql.sqlexception;import Com.j256.ormlite.dao.dao;import Com.j256.ormlite.dao.foreigncollection;import Zhangphil.ormlitetest.database.aclass;import Zhangphil.ormlitetest.database.ormlitedatabasehelper;import Zhangphil.ormlitetest.database.student;import Android.support.v7.app.actionbaractivity;import Android.widget.toast;import Android.os.bundle;public Class Mainactivity extends Actionbaractivity {private Dao<aclass, integer> mclassdao;private dao<student, Integer > Mstudentdao; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); O Rmlitedatabasehelper Mdatabasehelper = Ormlitedatabasehelper.getinstance (this); Mclassdao = Mdatabasehelper.getclassdao (); Mstudentdao = Mdatabasehelper.getstudentdao ();//Create 5 classes to demonstrate. for (int i = 1; i < 6; i++) {AClass aclass = new AClass (); aclass.id = I;aclass.name = i + "class"; try {mclassdao.createifno Texists (AClass);} catch (SQLException e) {e.printstacktrace ();}} //Find 1 classes in id=1. AClass Class1 = null;try {Class1 = Mclassdao.queryforid (1);} catch (SQLException e) {e.printstacktrace ();} 19 students were created and the 19 students belonged to 1 classes. for (int i = 1; i <; i++) {Student s = new Student (); s.id = I;s.name = "Student" + i;//points the newly created students ' class pointers to Class 1. Data Model: Id=1 1 Classes and these 19 students are 1 to many students, in other words, the 19 students are Class 1 students.

S.aclass = class1;try {mstudentdao.createifnotexists (s);} catch (SQLException e) {e.printstacktrace ();}}} @Overridepublic void OnStart () {Super.onstart ();/** * Below we demonstrate updating a student of information, and then up, depending on the foreign key, update class information from student-> class. * * *///If we update the name of the class where id=1 is located, it becomes "class one". Student S1 = null;try {S1 = Mstudentdao.queryforid (1); s1.aclass.name = "one shift";//Update 1 class name from "1 Shifts" into "class" Mclassdao.update ( S1.aclass);} catch (SQLException e) {e.printstacktrace ();} Show us the updated results.

AClass Class1 = null;try {Class1 = Mclassdao.queryforid (1);} catch (SQLException E1) {e1.printstacktrace ();} foreigncollection<student> students = class1.students;for (Student s:students) {Toast.maketext (this, s.toString (), Toast.length_short). Show ();}}}




The following are database-related modeling:

Aclass.java:

Package Zhangphil.ormlitetest.database;import Com.j256.ormlite.dao.foreigncollection;import Com.j256.ormlite.field.datatype;import Com.j256.ormlite.field.databasefield;import Com.j256.ormlite.field.foreigncollectionfield;import com.j256.ormlite.table.DatabaseTable; @DatabaseTable ( TableName = "Classes") public class AClass {@DatabaseField (Canbenull = false, DataType = Datatype.long, id = true) public lo ng ID; @DatabaseField (canbenull = false, DefaultValue = "A Class", DataType = datatype.string) public STRING name; @ForeignCo Llectionfield (eager = false) public foreigncollection<student> students = Null;public AClass () {}}


Student.java:

Package Zhangphil.ormlitetest.database;import Com.j256.ormlite.field.datatype;import Com.j256.ormlite.field.databasefield;import com.j256.ormlite.table.DatabaseTable; @DatabaseTable (tableName = " Students ") public class Student {public Student (String name, int student_id) {this.name = Name;this.id = student_id;} @DatabaseField (Canbenull = false, DataType = Datatype.integer, id = true) public int id; @DatabaseField (canbenull = False, D Atatype = datatype.string) public STRING name; @DatabaseField (Canbenull = false, Foreign = true, Foreignautorefresh = True) p Ublic aclass aclass;public Student () {} @Overridepublic String toString () {return "ID:" + ID + "Name:" + name + "class:" + A Class.name;}}


Ormlitedatabasehelper.java:

Package Zhangphil.ormlitetest.database;import Java.sql.sqlexception;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.util.log;import Com.j256.ormlite.android.apptools.ormlitesqliteopenhelper;import Com.j256.ormlite.dao.dao;import Com.j256.ormlite.support.connectionsource;import Com.j256.ormlite.table.tableutils;public class Ormlitedatabasehelper extends Ormlitesqliteopenhelper {private static Ormlitedatabasehelper mdatabasehelper = Null;private Dao<aclass, integer> Mclassdao = Null;private Dao<Student , integer> Mstudentdao = null;private final static String database_name = "School.db";p rivate final static int DataBase _version = 1;public Ormlitedatabasehelper (context context, String databasename,cursorfactory factory, int databaseversion) {Super (context, database_name, factory, database_version);} public static Ormlitedatabasehelper getinstance (context context) {if (MdatabasEhelper = = null) {Mdatabasehelper = new Ormlitedatabasehelper (context, database_name,null, database_version);} return mdatabasehelper;} @Overridepublic void OnCreate (Sqlitedatabase arg0, Connectionsource connectionsource) {log.d (this.getclass). GetName (), "Ormlite database, onCreate"); try {tableutils.createtableifnotexists (Connectionsource, Aclass.class); Tableutils.createtableifnotexists (Connectionsource, student.class);} catch (Exception e) {e.printstacktrace ();}} @Overridepublic void Onupgrade (sqlitedatabase database,connectionsource connectionsource, int oldversion, int NewVersion) {log.i (This.getclass (). GetName (), "Database--Onupgrade"); try {//delete old database tables. Tableutils.droptable (Connectionsource, Aclass.class, true); Tableutils.droptable (Connectionsource, Student.class, true);//Create a new version of the database again. OnCreate (database, connectionsource);} catch (SQLException e) {e.printstacktrace ();}} Public dao<student, Integer> Getstudentdao () {if (Mstudentdao = = null) {try {Mstudentdao = Getdao (Student.class);} catch (Java.sql.SQLException e) {e.printstacktrace ();}} return Mstudentdao;} Public Dao<aclass, Integer> Getclassdao () {if (Mclassdao = = null) {try {Mclassdao = Getdao (Aclass.class);} catch (ja Va.sql.SQLException e) {e.printstacktrace ();}} return Mclassdao;} @Overridepublic void Close () {super.close (); Mclassdao = Null;mstudentdao = null;}}

Android ormlite foreigncollection Associated External collection

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.