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 什么是唯一性约束? 唯一性约束指表中一个字段或 ...
随机推荐
- 工欲善其事,必先利其器 软件工具开发关键词 protractor自动化测试工具 RegexBuddy正则 CodeSmith,LightSwitch:代码生成 CheatEngine:玩游戏修改内存值必备神器 ApkIDE:Android反编译工具 Reflector:反编译dll动态链接库
工欲善其事,必先利其器 本文版权归翟士丹(Stan Zhai)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利. 原文地址:http ...
- 【javascript】js中的函数节流和函数防抖
一.概念解释 函数节流和函数防抖,两者都是优化高频率执行js代码的一种手段. 大家大概都知道旧款电视机的工作原理,就是一行行得扫描出色彩到屏幕上,然后组成一张张图片.由于肉眼只能分辨出一定频率的变 ...
- 关于tensorboard启动问题
我在学习过程中遇到了tensorboard无法启动的问题. 按照网上的教程,我无法正常启动tensorboard,全过程没有报错,但是打开tensorboard显示 No dashboards are ...
- oracle下导出某用户所有表的方法
oracle下导出某用户所有表的方法 scott/tiger是用户名和密码,handson是导出的实例名 按用户方式导出数据(owner当中写的是用户名) exp scott/tiger@handso ...
- JavaScript三种方式改变标签css
原文地址:https://www.cnblogs.com/xiangru0921/p/6514225.html <body> <div id="div">这 ...
- Eclipse插件的安装与配置
1.下载插件时注意要和Eclipse版本兼容. 2.安装Eclipse插件时注意是否要安装其他的插件,这一点很容易被忽视. 3.有时启动Eclipse未加载插件,解决方法很多,总结一下: a ...
- /proc 目录详细说明
/proc路径详细: Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...
- Transparent Huge Pages
在RHEL6中,透明大页功能是默认开启的. 开启该选项后,内核会尽可能地尝试分配大页,如果mmap区域是2mb,那么每个linux进程都会分配到2mb大小的页.如果大页不够用了(比如物理内存不够了), ...
- 第一篇:初识ASP.NET控件开发_第一节:控件类及其继承关系
1)System.Web.UI.Control(以下简称Control) Control 类是包括自定义控件.用户控件和页在内的所有 ASP.NET 服务器控件的基类..定义由所有 ASP.NET 服 ...
- redis transactions(事务)
MULTI MULTI:标记一个事务块的开始. 事务块内的多条命令会按照先后顺序被放进一个队列当中,最后由 EXEC 命令原子性(atomic)地执行. 可用版本:>= 1.2.0 时间复杂度: ...