Greendao examples and some problems

Source: Internet
Author: User
Tags create database

Recently in the use of Greendao, did a small demo, which encountered a number of most people will encounter problems, there are some pits.

Recorded, and spread to GitHub, convenient for everyone to learn and jump pits.

Now the current version of the GitHub has an unresolved null pointer problem, I hope someone encountered when the solution can tell why.

Beginners always meet some pits, sometimes a flash of light to understand the problem, sometimes for several days also do not understand.

Hope to make progress together and share together.

Application Greendao Way There are many posts on the Internet, here is not to repeat.

Write down some of the meaning of the annotation first

1.) Entity @entity Annotation

schema: tells Greendao which schema the current entity belongs to
Active: marks an entity is active, and the active entity has an update, delete, and Refresh method
nameindb: alias used in the data, using the entity's class name by default
indexes: defines indexes that can span multiple columns
createindb: Tag CREATE DATABASE table * * 2.) Basic attribute Annotation

@Id: primary key long, can be set by @Id (AutoIncrement = true) self-growth
@Property: Sets the name of the column corresponding to a non-default relationship map, by default, by using the field name for example: @Property (nameindb= "name")
@NotNul: Set the current column of the database table cannot be empty
@Transient: Column 3 of the database table is not generated after the secondary tag is added . Index Annotations

@Index: Create an index using @index as a property, set the index alias by name, or add a constraint to the index by unique
@Unique: added a unique constraint to the database column 4.) Relationship Annotation

@ToOne: defines a relationship to another entity (an entity object)
@ToMany: Defining relationships with multiple entity objects

(i) @Entity definition entity
@nameInDb the name in the database, such as the class name in the entity if it is not written
@indexes Index
@createInDb whether to create a table, True,false is not created by default
@schema Specify the schema name as an entity
@active whether update builds are refreshed
(b) @Id
(iii) @NotNull NOT NULL
(iv) @Unique UNIQUE constraint
(e) @ToMany One-to-many
(vi) @OrderBy sequencing
(vii) @ToOne one-on-one
(eight) @Transient not stored in the database
(ix) @generated a constructor or method produced by Greendao

Let's look at the three patterns of your own class

First, simple custom classes

@Entity public
class User {
    @Id (AutoIncrement = true)
    private Long Id;
    private String name;
    Private String age;
    Private String sex;
    Private String salary;
This is the simplest custom class, so you can generate a DAO file directly after writing it


The second custom class that contains subclasses

The parent class
@Entity public
class Student {
    @Id (AutoIncrement = true)
    private Long Id;
    private String name;
    Private String age;
    Private String sex;
    Private String salary;
    Private Long BookID;
    @ToOne (Joinproperty = "BookID")
    private books Booklist;

Subclass
@Entity Public
class books{
    @Id
    private Long Id;
    Private String bookname;
    Private String Bookprice;

You need to use @toone annotations to declare the corresponding relationship between the parent class and the subclass


The third custom class that contains the List<x> subclass

The parent class
@Entity (
        active = true,
        nameindb = "CLIENTS") public
class Client {
    @Id (AutoIncrement = true)
    private long ID;
    @NotNull
    private String nom;
    @NotNull
    private String prenom;
    @ToMany (Referencedjoinproperty = "clientId")
    private list<commande> listecommandes;

Subclass
@Entity (
        active = true,
        nameindb = "commandes") public
class Commande {
    @Id ( AutoIncrement = True)
    private long ID;
    @NotNull
    private String Libelle;
    @NotNull
    Private long clientId;
    @NotNull
    @ToOne (joinproperty = "clientId")
    private Client client;

You need to declare a one-to-many and many-to-many correspondence between a subclass and a parent class, respectively.


Additional questions can be seen on the GitHub case, address https://github.com/beibeiMary/GreenDaoDemo.git

But now there is a problem in the case, hope to have met, or resolved friends to leave a message, to find a solution

DAO =baseapplication.getapplication (). Getdaosession (). Getuserdao ();

The Getdaosession () method in the above code is a null pointer, which is

    private void Setdatabase () {
        mhelper = new Daomaster.devopenhelper (This, "users-db", null);
        DB =mhelper.getwritabledatabase ();
        Note: This database connection belongs to Daomaster, so multiple sessions refer to the same database connection.
        mdaomaster = new Daomaster (db);
        Mdaosession = Mdaomaster.newsession ();
        LOG.E ("Zhsy", "mdaosession==" +mdaosession);
    }

Mdaosession = Mdaomaster.newsession ();
This sentence gets a null pointer. The answer to the error message is as follows.

Baseapplication.getdaosession () ' On a null object reference   

Welcome to leave a message, follow-up will continue to find the solution to the problem.

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.