Unique constraint on single String column with GreenDao2
转:http://software.techassistbox.com/unique-constraint-on-single-string-column-with-greendao_384521.html
I want to make unique constraint on String value with GreenDao. I have following piece of code:
Entity category = schema.addEntity('Category');
category.addIdProperty();
category.addStringProperty('Name').notNull();
How can I make sure that Name will be unique?
My tries:
I have found methods PropertyBuilder.unique() and PropertyBuilder.index() but none of this works.
index() doesn't work at all and allows to make duplication. unique() throws exception in Robolectric test but don't do anything when app runs on device Robolectric exception:
android.database.sqlite.SQLiteException: Cannot execute for last inserted row ID, base error code: 19
at org.robolectric.shadows.ShadowSQLiteConnection.rethrow(ShadowSQLiteConnection.java:48)
at org.robolectric.shadows.ShadowSQLiteConnection.access$500(ShadowSQLiteConnection.java:26)
at org.robolectric.shadows.ShadowSQLiteConnection$Connections.execute(ShadowSQLiteConnection.java:436)
at org.robolectric.shadows.ShadowSQLiteConnection.nativeExecuteForLastInsertedRowId(ShadowSQLiteConnection.java:239)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at de.greenrobot.dao.AbstractDao.executeInsert(AbstractDao.java:348)
at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293)
So far I have made something like this but I don't think it's the best solution.
However if this correct, is it good idea to add index() to name Property to get better performance?
public Long insert(Category category) {
if (getByName(category.getName()) != null) {
throw new DuplicateCategoryNameException(category.getName());
}
...
} public Category getByName(String name) {
return categoryDao.queryBuilder().where(CategoryDao.Properties.Name.eq(name)).build().unique();
}
Unique constraint on single String column with GreenDao2的更多相关文章
- Unique constraint on single String column with GreenDao
转:http://stackoverflow.com/questions/22070281/greendao-support-for-unique-constraint-on-multiple-col ...
- SQL UNIQUE Constraint
SQL UNIQUE Constraint The UNIQUE constraint uniquely identifies each record in a database table. The ...
- ORA-00001: unique constraint (string.string) violated 违反唯一约束条件(.)
ORA-00001: unique constraint (string.string) violated ORA-00001: 违反唯一约束条件(.) Cause: An UPDATE or I ...
- Oracle 唯一 约束(unique constraint) 与 索引(index) 关系说明
一. 官网对Unique Constraints说明 http://download.oracle.com/docs/cd/E11882_01/server.112/e16508/datainte.h ...
- Sqlite - constraint failed[0x1555]: UNIQUE constraint failed
执行插入操作时,出现异常constraint failed[0x1555]: UNIQUE constraint failed 意思是:sqlite 唯一约束失败 定位于某个表字段上,该字段是表的主键 ...
- ORA-00001: unique constraint (...) violated解决方案
ORA-00001: unique constraint (...) violated 的解决方案 今天往Oracle数据库里插入数据一条记录的时候,报错了, 控制台抛出异常:违反唯一性约定, 我以为 ...
- sqlite3, IntegrityError: UNIQUE constraint failed when inserting a value
sqlite报错: sqlite3.IntegrityError: UNIQUE constraint failed: IMAGESTATUE.ID 解决方案: Change INSERT to IN ...
- ORA-00001: unique constraint (...) violated并不一定是数据冲突
原文链接:http://blog.163.com/jet_it_life/blog/static/205097083201301410303931/ 收到一位测试人员RAISE的JIRA,说在某张表上 ...
- Oracle之唯一性约束(UNIQUE Constraint)使用方法具体解释
Oracle | PL/SQL唯一索引(Unique Constraint)使用方法 1 目标 用演示样例演示怎样创建.删除.禁用和使用唯一性约束. 2 什么是唯一性约束? 唯一性约束指表中一个字段或 ...
随机推荐
- git的几个操作
git reference https://git-scm.com/docs 克隆 从远程仓库克隆一个项目到本地文件夹,命令如下:$ git clone https://github.com/libg ...
- 手动搭建latex公式渲染服务器
latex公式渲染有两种类型,一种是HTML形式展示公式,另一种是图片形式展示公式.如果是HTML形式展示公式,渲染是在前端完成的,一般会比较缓慢.知乎采取的方式是以图片形式展示公式.codecogs ...
- 修改的select
慢慢修改: #include <winsock.h> #include <stdio.h> #include <string> #define PORT 5010 ...
- rabbitMQ概念详细介绍
1. 历史 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有 ...
- Linux下DIR,dirent,stat等结构体详解(转)
最近在看Linux下文件操作相关章节,遇到了这么几个结构体,被搞的晕乎乎的,今日有空,仔细研究了一下,受益匪浅. 首先说说DIR这一结构体,以下为DIR结构体的定义: struct __dirstre ...
- VS与SQL资源
经常在网上看到有同学费尽心思的找SQL server数据库各版本的下载地址,看到别人的求助贴就不自觉的想去帮助他们,但是一个一个去帮助又不太现实,毕竟个人精力有限,既然大家有需求,那么笔者就本着乐于分 ...
- Hadoop相关项目Hive-Pig-Spark-Storm-HBase-Sqoop
Hadoop相关项目Hive-Pig-Spark-Storm-HBase-Sqoop的相关介绍. Hive Pig和Hive的对比 摘要: Pig Pig是一种编程语言,它简化了Hadoop常见的工作 ...
- 页面生命周期里面还有很东西,如PageHandlerFactory等等这些东东也够吃一壶的,发现每走到一个领域,发现要学的东西实在是太多太多啦,总感觉自己所学的东西只是沧海一粟,走过了这道坎,又是一片海洋,我只能呐喊:生命永不止息,学海无涯----够用就好。
页面生命周期里面还有很东西,如PageHandlerFactory等等这些东东也够吃一壶的,发现每走到一个领域,发现要学的东西实在是太多太多啦,总感觉自己所学的东西只是沧海一粟,走过了这道坎,又是一片 ...
- please verify the preference field with the prompt:Tomcat JDK name
使用MyEclipse的Tomcat的时候出现下面的问题: a configuration error occurred during startup. please ve ...
- easyui combobox 动态加载的两种方法
reload 方法 javascript代码 //指定id 和 text 否则始终选择第一个 $('#contact_city').combobox({ valueField:'id', textFi ...