GreenDao之Identity scope and session “cache”

本文详细解释了如何在使用GreenDAO ORM时,通过`DaoSession`的`clear()`方法或选择`IdentityScopeType.None`来优化查询过程,确保数据的一致性和准确性。重点在于理解内存缓存对查询的影响及如何正确清空缓存,避免因缓存导致的数据错误。

原文http://greendao-orm.com/documentation/sessions/

        问题:

DaoMaster dm = AppContext.getDaoMaster(ctx);
DaoSession ds = dm.newSession(IdentityScopeType.Session);

        如果传入的是IdentityScopeType.Session,同样的查询只会执行一次,查询结果对象List保存在内存里会重复使用。会导致的问题是如果List对象属性改变,未持久化它,再次做query,不会从数据库查询,只是缓存中的结果。会导致与数据库表数据不一致。

       解决办法,再次执行查询之前执行ds.clear();方法。或者在这种情况下之不保留缓存,用IdentityScopeType.None。

<span style="white-space:pre">	</span>public List<Label> queryLabels(){
		DaoSession ds = AppContext.getDaoSession(ctx);
//		//clear the cache,requery the data from database table, not from the memory.
		ds.clear();
		
		QueryBuilder<Label> qb = ds.getLabelDao().queryBuilder();
		qb.where(LabelDao.Properties.Deleted.eq(false));
		List<Label> labels = qb.list();
		if(labels.size()<showNum){
			Label entity = new Label(null, "添加标签", null, false, null, null, true, false);
			labels.add(entity);
		}

		
		return labels;
	}

或者用IdentityScopeType.None创建DaoSession


<span style="white-space:pre">	</span>private DaoSession getDaoSession(IdentityScopeType scope){
		if(scope == IdentityScopeType.Session){
			DaoSession ds = AppContext.getDaoSession(ctx);
			return ds;
		}else{
			DaoMaster dm = AppContext.getDaoMaster(ctx);
			DaoSession ds = dm.newSession(IdentityScopeType.None);
			return ds;
		}
	}


```sql -- ---------------------------- -- 4、角色信息表 -- ---------------------------- drop table if exists "SYS_ROLE"; create table "SYS_ROLE" ( "ROLE_ID" bigint not null identity(1,1) comment '角色ID', "ROLE_NAME" varchar(30) not null comment '角色名称', "ROLE_KEY" varchar(100) not null comment '角色权限字符串', "ROLE_SORT" int not null comment '显示顺序', "DATA_SCOPE" char(1) default '1' comment '数据范围(1:全部数据权限 2:自定数据权限 3:本部门数据权限 4:本部门及以下数据权限)', "MENU_CHECK_STRICTLY" tinyint default 1 comment '菜单树选择项是否关联显示', "DEPT_CHECK_STRICTLY" tinyint default 1 comment '部门树选择项是否关联显示', "STATUS" char(1) not null comment '角色状态(0正常 1停用)', "DEL_FLAG" char(1) default '0' comment '删除标志(0代表存在 2代表删除)', "CREATE_BY" varchar(64) default '' comment '创建者', "CREATE_TIME" datetime comment '创建时间', "UPDATE_BY" varchar(64) default '' comment '更新者', "UPDATE_TIME" datetime comment '更新时间', "REMARK" varchar(500) default null comment '备注', primary key ("ROLE_ID") ) comment = '角色信息表'; -- ---------------------------- -- 初始化-角色信息表数据 -- ---------------------------- insert into "SYS_ROLE" values(1, '超级管理员', 'admin', 1, 1, 1, 1, '0', '0', 'admin', sysdate, '', null, '超级管理员'); insert into "SYS_ROLE" values(2, '普通角色', 'common', 2, 2, 1, 1, '0', '0', 'admin', sysdate, '', null, '普通角色'); -- ---------------------------- -- 5、菜单权限表 -- ---------------------------- drop table if exists "SYS_MENU"; create table "SYS_MENU" ( "MENU_ID" bigint not null identity(1,1) comment '菜单ID', "MENU_NAME" varchar(50) not null comment '菜单名称', "PARENT_ID" bigint default 0 comment '父菜单ID', "ORDER_NUM" int default 0 comment '显示顺序', "PATH" varchar(200) default '' comment '路由地址', "COMPONENT" varchar(255) default null comment '组件路径', "QUERY" varchar(255) default null comment '路由参数', "ROUTE_NAME" varchar(50) default '' comment '路由名称', "IS_FRAME" int default 1 comment '是否为外链(0是 1否)', "IS_CACHE" int default 0 comment '是否缓存(0缓存 1不缓存)', "MENU_TYPE" char(1) default '' comment '菜单类型(M目录 C菜单 F按钮)', "VISIBLE" char(1) default 0 comment '菜单状态(0显示 1隐藏)', "STATUS" char(1) default 0 comment '菜单状态(0正常 1停用)', "PERMS" varchar(100) default null comment '权限标识', "ICON" varchar(100) default '#' comment '菜单图标', "CREATE_BY" varchar(64) default '' comment '创建者', "CREATE_TIME" datetime comment '创建时间', "UPDATE_BY" varchar(64) default '' comment '更新者', "UPDATE_TIME" datetime comment '更新时间', "REMARK" varchar(500) default '' comment '备注', primary key ("MENU_ID") ) comment = '菜单权限表'; -- 初始化菜单数据(示例部分,完整数据请参照原MySQL脚本) insert into "SYS_MENU" values(1, '系统管理', 0, 1, 'system', null, '', '', 1, 0, 'M', '0', '0', '', 'system', 'admin', sysdate, '', null, '系统管理目录'); -- 其他菜单插入语句类似,需将 sysdate() 改为 sysdate -- ---------------------------- -- 6、用户和角色关联表 用户N-1角色 -- ---------------------------- drop table if exists "SYS_USER_ROLE"; create table "SYS_USER_ROLE" ( "USER_ID" bigint not null comment '用户ID', "ROLE_ID" bigint not null comment '角色ID', primary key("USER_ID", "ROLE_ID") ) comment = '用户和角色关联表'; insert into "SYS_USER_ROLE" values (1, 1); insert into "SYS_USER_ROLE" values (2, 2); -- ---------------------------- -- 7、角色和菜单关联表 角色1-N菜单 -- ---------------------------- drop table if exists "SYS_ROLE_MENU"; create table "SYS_ROLE_MENU" ( "ROLE_ID" bigint not null comment '角色ID', "MENU_ID" bigint not null comment '菜单ID', primary key("ROLE_ID", "MENU_ID") ) comment = '角色和菜单关联表'; java 项目中使用的持久层框架是 hibenate ,帮我根据以上表,实现访问某个接口前判断当前用户有无该接口的权限功能
最新发布
09-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值