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 什么是唯一性约束? 唯一性约束指表中一个字段或 ...
随机推荐
- Xshell连接Ubuntu失败问题
转自:https://www.linuxidc.com/Linux/2017-08/146222.htm Xshell是一个安全终端模拟软件,可以进行远程登录.我使用XShell的主要目的是在Wind ...
- keras的训练引擎:train_array.py和train_generator.py
keras的Model支持两种模式的训练: 直接传入数组,最终会调用train_array.py中的fit_loop()函数 直接传入生成器,最终会调用train_generator.py中的fit_ ...
- [转]nonlocal和global
在Python中,当引用一个变量的时候,对这个变量的搜索是按找本地作用域(Local).嵌套作用域(Enclosing function locals).全局作用域(Global).内置作用域(bui ...
- 【虚拟化系列】VMware vSphere 5.1 网络管理
网络是VMware vSphere 5.1的基础,所有虚拟机都需要网络来进行通信.如果将所有的虚拟机都看成是物理机,则在网络拓扑上,需要网卡和交换机等不同的网络连接设备和方式.而在虚拟化中,这些 ...
- 自动化部署必备技能—搭建YUM仓库
导言: YUM主要用于自动安装.升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系.要成功的使用YUM工具安装更新软件或系统,就需要有一个包含各种rpm软件包的repository(软件仓库) ...
- React(0.13) 定义一个动态的组件(函数作为动态的值)
<!DOCTYPE html> <html> <head> <title>React JS</title> <script src=& ...
- 【MySQL】mysql中any,in,some,all的区别
子查询就是指在一个select语句中嵌套另一个select语句. any,in,some,all分别是子查询关键词之一, any 可以与=.>.>=.<.<=.<> ...
- 【MySQL】乐观锁和悲观锁
最近学习了一下数据库的悲观锁和乐观锁,根据自己的理解和网上参考资料总结如下: 悲观锁介绍(百科): 悲观锁,正如其名,它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处理)修改持 ...
- javascript 中 split 函数分割字符串成数组
分割字符串成数组的方法有很多,不过使用最多的还是split函数 <script language="javascript"> str="2,2,3,5,6,6 ...
- 主流磁盘接口比较(SATA/SCSI/SAS/FC)[转]
数据越来越多,用户对存储容量的要求是越来越高.作为数据存储最基本的介质——硬盘,其种类也越来越多.面对市场上纷繁复杂的硬盘,用户又该如何选择呢?本文就对SATA.FC.SAS三种硬盘进行了比较,希望能 ...