我有这个实体 User,它有一个角色集合 (Set),我想对其进行缓存,因此每当它调用 getRoles() 时,它都会返回一个缓存的副本。
目前的结构如下:
映射信息:
<set
name="roles"
table="user_role"
lazy="true"
cascade="none"
access="field"
>
<key
column="user_id"
>
</key>
<many-to-many
class="com.resolution.scheduler.model.Role"
column="role_id"
outer-join="auto"
/>
</set>
这是 GetRoles():
public Set getRoles() {
if(!rolesUpdated && this.id!=null){
ApplicationContextProviderNonManageBean.getApplicationContext().publishEvent(
new com.resolution.scheduler.dao.event.UserRoleGetEvent(this));
}
return roles;
}
这是 UserRoleGetEvent 事件触发时调用的内容:
@Cacheable(value="userRoleByUserId" , key="#userId", condition="#userId!=null")
public PersistentSet getUserRoleByUserId(final Long userId){
if(userId==null){
return new PersistentSet();
}
log.info("USer Role Getting from Database for User Id : "+userId);
//final List<Role> roles=(List<Role>)getHibernateTemplate().find("Select u.roles from User u where u.id=?",userId);
final PersistentSet[] set= new PersistentSet[1];
getHibernateTemplate().executeWithNativeSession(new HibernateCallback<Object>(){
public Object doInHibernate(Session session) throws HibernateException{
List roles=session.createQuery("Select u.roles from User u where u.id=:userId").setParameter("userId",userId).list();
set[0]= new PersistentSet((SessionImpl)session, new HashSet(roles));
return null;
}
});
log.info(set);
return set[0];
}
UserRoleGetEvent 的工作原理如下:
public void onApplicationEvent(UserRoleGetEvent event) {
PersistentSet roles = userManager.getUserRoleByUserId(event.getUser().getId());
event.getUser().setRoles((roles)); //**THIS_SET_MAKES_IT_DIRTY**
}
问题:
当我执行
有任何解决方法或建议吗?user.getRole() 时,因为它为 user.roles 设置了一个新集合并使 session 变脏。 在这个肮脏的 session 中,每当调用 flush 时,它都会删除所有角色并再次插入所有角色,即使没有实际更改。我正在寻找的是如何让 hibernate 状态认为我的新集合就像检索到的 session (实际上不是)并且不认为它是脏的。
最佳答案
Hibernate 不喜欢它的集合被改变。可能有用的是:
event.getUser().getRoles().clear();
event.getUser().getRoles().addAll(roles);
但是,我对您要尝试做的事情感到有点困惑。 User 上的 getRoles 会触发一个事件,然后更改角色?
作为旁注,我在这里是凭内存工作的,但您的代码可以重构为:
@Cacheable(value="userRoleByUserId" , key="#userId", condition="#userId!=null")
public List<Role> getUserRoleByUserId(final Long userId){
if(userId==null){
return Collections.emptyList();
}
log.info("USer Role Getting from Database for User Id : "+userId);
@SuppressWarning("unchecked")
List<Role> result = (List<Role>) getHibernateTemplate().executeWithNativeSession(new HibernateCallback<Object>(){
public Object doInHibernate(Session session) throws HibernateException{
return session.createQuery("Select u.roles from User u where u.id=:userId").setParameter("userId",userId).list();
}
});
log.info(set);
return result;
}
public void onApplicationEvent(UserRoleGetEvent event) {
List<Role> roles = userManager.getUserRoleByUserId(event.getUser().getId());
event.getUser().getRoles().clear(); // yes, I know, getRoles will throw an event and send you in an infinite loop... Fixing that just means having another method
event.getUser().getRoles().addAll(roles);
}
关于java - 显式加载实体的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42695954/
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty