GreenDao2.2 how to upgrade GreenDao3.0 and greendao Database

Source: Internet
Author: User

GreenDao2.2 how to upgrade GreenDao3.0 and greendao Database
Preface. Why should I upgrade to Greendao3.0? 1. multi-person development

In the past, operations such as creating a table and creating a Dao in a database were required to create a module and manage the database to create a table in a unified place. Now, you can directly write the Entity. When developers develop their own Entity, they do not need to conflict with the dog as before.

2. Simple and Convenient Structure

Previously, I was writing CreateTable addEntity ("") and other methods to create a table. Now I only need to use the 3.0 annotation syntax in Entity.

3. annotation usage

The previous Entity is often generated and overwritten, so it is not recommended to modify the Entity code, so it is difficult to add annotations in the Entity, this also causes gson's @ SerializedName ("id") returned field and self-writing segment different annotations. 3.0 and later, Greendao will all use annotations, so we can use them.

4. Database Security

GreenDao of database encryption 3.0 provides the built-in encryption function, which is very practical.

 

Upgrade path

The official website says there are two ways to upgrade here: one is to retain the module that generated the code before, and the other is to migrate the entity class and delete other generated files before. The second method is recommended here. The first method always feels that the upgrade will not be complete.

 

1. Keep the previous Entity

Here, you can choose to migrate the Entity in the folder and directly remove the previous generation configuration.

The include module in settings. gradle is removed, and the preBuild. dependsOn module in the main build. gradle is also removed.

2. Upgrade gradle

Upgrade the version number in build. gradle dependence to the following:

compile 'org.greenrobot:greendao-generator:3.0.0'compile 'org.greenrobot:greendao:3.0.1'  

 

3. Change the configuration address

Because the previously used Module is discarded, you need to add some previously configured items to the build. gradle of the project.

Add

apply plugin: 'org.greenrobot.greendao'

Under dependencies {}, add

Greendao {targetGenDir 'src/main/Java' // generate the code path daoPackage 'com. XXX. platform. dao '}

 

4. Delete the derived part with annotations, primary keys, dependencies, and entity.

Delete the derived code in the previous code, such as the getter setter method, and some comments wrapped in the code/* ToOne.

    /** To-one relationship, resolved on first access. */    public OrderBase getOrderBase() {        Long __key = this.orderId;        if (orderBase__resolvedKey == null || !orderBase__resolvedKey.equals(__key)) {            if (daoSession == null) {                throw new DaoException("Entity is detached from DAO context");            }            OrderBaseDao targetDao = daoSession.getOrderBaseDao();            OrderBase orderBaseNew = targetDao.load(__key);            synchronized (this) {                orderBase = orderBaseNew;            orderBase__resolvedKey = __key;            }        }        return orderBase;

Delete the attribute of the comment package and add @ Transient

// Keep includes-put your custom includes here // Property Code // keep includes end // keep fields-put your custom fields here // property getter setter method // KEEP FIELDS END

The following getset method can be retained or deleted to regenerate it. However, if you add the @ Transient annotation attribute, the getset method will not be automatically generated. Therefore, you can run it once and then add the @ Transient annotation.

The next step is to change some special writing methods of the old code used to create a table to the New Annotation writing method.

During this process, you may often change the Run. In this case, you can ignore the various red lines in the project, because the Dao will be compiled during the build, and then the Business Code will be compiled. So when you see that the following error has nothing to do with greendao, it indicates that the error has passed. You don't have to worry about the Errors generated by greendao. The descriptions of these errors are accurate and easy to understand.

List several errors:

Error:Execution failed for task ':platform:greendao'.> Currently only single FK columns are supported: ToOne 'orderBase' from OrderXXX to OrderBase

No primary key is added.

Error: Execution failed for task': platform: greendao '.> can't replace field in/Users/dsx/Documents/XXX/platform/src/main/java/com/XXX/platform/bean/OrderXXX. java: 43 with generated version. // Dong baoran blog park If you wowould like to keep it, it shoshould be explicitly marked with @ Keep annotation. otherwise please mark it with @ Generated annotation

Add @ keep

Error: Execution failed for task': platform: greendao '.> can't add field 'variable (type = VariableType (name = com. xxx. platform. dao. orderBase, isPrimitive = false, originalName = OrderBase, typeArguments = null), name = orderBase) '// Dong Shang Xian for entity OrderXXX due to: Unsupported type com. xxx. platform. dao. orderBase

Set the table relationship above. ToOne or ToOne is missing.

 

5. Syntax comparison

Generally, you should have an understanding of the 3.0 syntax before performing this upgrade operation. If you are not familiar with it, you can refer to the annotations in this article. That is, the previous Code of 2.2 should be changed to an annotation of 3.0.

Http://www.cnblogs.com/dsxniubility/p/5699543.html

 

6. Replace the package name file

Replace the full-text search package name with the following

// old package nameimport de.greenrobot.dao.database.Database;... // new package nameimport org.greenrobot.greendao.database.Database;...

 

7. Finally solve some compilation errors

1. Change DaoOpenHelper to DevOpenHelper.

2. Change SQL database to database

3. Replace getWritableDatabase () with getWritableDb ()

 

So far

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.