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 什么是唯一性约束? 唯一性约束指表中一个字段或 ...
随机推荐
- 在 Linux 平台中调试 C/C++ 内存泄漏方法(转)
由于 C 和 C++ 程序中完全由程序员自主申请和释放内存,稍不注意,就会在系统中导入内存错误.同时,内存错误往往非常严重,一般会带来诸如系统崩溃,内存耗尽这样严重的后果.本文将从静态分析和动态检测两 ...
- Python学习笔记(八)—— 使用dict和set
一.dict 1.定义: Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度 2.优势: di ...
- WCF异步调用
添加引用服务--高级--选中 生产异步操作 服务端接口操作 [OperationContract]int Add(int a, int b); 客户端: 引用服务:在引用服务时,左下角点击“高级”按钮 ...
- 【java】解析JToolBar类的使用
1.简介 在大部分的Look and Feels下,用户都可以把该工具条拖离原Window组件(除非floatable属性设置为false).为了能够正常的实现拖动效果,该类的实例被建议添加到Bord ...
- 编码 GBK 的不可映射字符
一般做项目公司都会统一要求文件编码类型,很多为了实现应用国际化和本地化和更高的性能,而选用UTF-8而非GBK. 但在开发过程中我们都用的是IDE,只要更改了配置就不用操心了,但有时我们也会用命令行来 ...
- SQL 中的 UNION 和UNION ALL 的区别
UNION表示“并”,当用的时候,系统会自动将重复的元组去掉,如果要保留重复元组则就用UNION ALL UNION 会合并重复数据,(由于要合并重复,该操所 隐藏着一个 排序的操作.)UNION A ...
- 订单状态 Mark
) { ) { ) { ) { ) { ) { ) { ) { ) { ) { ) { ) { ) { ) { ))) { ))) { ))) { ))) { )); } else { Assert. ...
- StringBoot集成Rabbit Redis和ack机制双重保险,保障消息一定能够正确的消费
转: StringBoot集成Rabbit,根据业务返回ACK 原文链接 : http://www.jianshu.com/p/baed9ec92410 为了维护消息的有效性,当消费消息时候处理失败时 ...
- yml配置文件
1 yaml配置文件中是以空格来区分是否是同一层级.在键值对的冒号之后要有空格. 2 属性值:数值.字符串.bool 值,直接写即可.双引号会将特殊字符进行转义,例如:"nice \n go ...
- Visual F# Power Tools 简单介绍
Visual F# Power Tools 简单介绍 Auto-generating XmlDoc 当在函数定义的前面输入 ///< 以后.会自己主动生成 XML 文档.并会自己主动提取函数中的 ...