我对以下实体有疑问:Forest、Tree、Leaf。正如您可以想象的那样,一片森林可以有很多树,而一棵树有很多叶子。
我想延迟加载森林中的所有树木并急切加载树中的所有叶子。我的带 hibernate 注释的代码如下所示:
Forest.java
@Entity
@Table(name = "Forests")
public class Forest implements Comparable<Forest> {
@Id
@Column(name = "forestnumber", length=10, nullable=false)
private String number;
@OneToMany(fetch=FetchType.LAZY, mappedBy="forest")
private Set<Tree> trees = null;
// some other attributes and methods
树.java
@Entity
@Table(name = "Trees")
public class Tree implements Comparable<Tree> {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="tree_id", nullable=false)
private int id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "forestnumber", nullable = false)
@Fetch(FetchMode.JOIN)
private Forest forest;
@OneToMany(fetch=FetchType.EAGER, mappedBy="tree")
@Fetch(FetchMode.JOIN)
private Set<Leaf> leafs = null;
// some other attributes and methods
Leaf.java
@Entity
@Table(name = "Leafs")
public class Leaf implements Comparable<Leaf> {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="leaf_id", nullable=false)
private int id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "tree_id", nullable = false)
@Fetch(FetchMode.JOIN)
private Tree tree;
// some other attributes and methods
我的问题:加载森林并调用 getTrees() 方法会产生一组选择语句。 Hibernate 执行一个语句来获取所有的树,并为每棵树执行第二个语句以收集所有的叶子。 在我看来,hibernate 应该只生成一条语句,使用连接,同时收集树和叶子。
有人可以告诉我我的问题的原因以及我该如何解决吗? 非常感谢!
顺便说一下:如果我将林木的获取策略更改为 EAGER,一切正常,只有一个语句。
最佳答案
可以尝试初始化
Hibernate.initialize(forestInstance);
或者编写一个带有连接的查询,提供 fetch 来急切地检索所有的 child 。
另见
关于java - 延迟加载子项,其中包含急切的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30301311/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
鉴于我有以下迁移: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("
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我一直致力于让我们的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个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/