- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我认为我缺少一些有关 Hibernate 工作原理的基本知识,特别是延迟加载。我的问题是调试,因为我不确定这是 Hibernate 问题还是变相的 Spring 问题。我想在进行一些重大重构之前我应该在这里问一下。
我有两个实体。一个以 OneToMany 关系保存另一个的集合。对于我的网页,我希望获取所有第一个实体,然后获取每个实体的关联实体集并显示它们。
我相信我的问题是这样的:我使用 JpaTemplate 来查找所有实体。这工作正常,但是由于延迟加载,我没有获得关联的相关实体集。在我看来(jsp)我想访问这个集合,但当然它是空的,因为它是延迟加载的。现在,我收到一个 LazyInitialization 异常,表明事务已结束。对我来说这是有道理的,当然交易现在应该结束了。问题是,如果事务结束,关联集如何延迟加载?
实体类:
@Entity
public class LearningEntry implements Serializable {
private Long id;
String imagePath = "";
Set<Sample> samples = null;
//------------------------------
// Constructors
//------------------------------
public LearningEntry(){
imagePath = "";
samples = new HashSet<Sample>();
}
//------------------------------
// Instance Methods
//------------------------------
public void addSample(Sample s){
samples.add(s);
}
public void removeSample(Sample s){
samples.remove(s);
}
//------------------------------
// Setters and Getters
//------------------------------
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
//@Column(name = "wisi_LE_IMAGEPATH", length = 100, nullable = false)
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
// TODO - ONly works with fetch type EAGER
//@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
public Set<Sample> getSamples() {
return samples;
}
public void setSamples(Set<Sample> samples) {
this.samples = samples;
}
}
示例实体
@Entity
public class Sample implements Serializable {
private Long id;
Date creationDate;
String audioFileLocation;
Integer votes;
String description;
public Sample(){
creationDate = new Date();
audioFileLocation = "";
votes = 0;
description = "";
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAudioFileLocation() {
return audioFileLocation;
}
public void setAudioFileLocation(String audioFileLocation) {
this.audioFileLocation = audioFileLocation;
}
@Temporal(TemporalType.DATE)
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getVotes() {
return votes;
}
public void setVotes(Integer votes) {
this.votes = votes;
}
}
DAO 类:学习EntryDAO
@Transactional
public class JpaLearningEntryDAO implements LearningEntryDAO{
private JpaTemplate jpaTemplate;
public JpaLearningEntryDAO(){
}
public void setJpaTemplate(JpaTemplate jpaTemplate){
this.jpaTemplate = jpaTemplate;
}
@Override
//@Transactional
public void delete(Long leId) {
LearningEntry dp = jpaTemplate.find(LearningEntry.class, leId);
jpaTemplate.remove(dp);
}
@Override
@SuppressWarnings("unchecked")
//@Transactional
public List<LearningEntry> findAll() {
return jpaTemplate.find("from LearningEntry");
}
@Override
//@Transactional
public LearningEntry findById(Long leId) {
return jpaTemplate.find(LearningEntry.class, leId);
}
@Override
//@Transactional
public LearningEntry store(LearningEntry dp) {
return jpaTemplate.merge(dp);
}
@Override
@SuppressWarnings("unchecked")
//@Transactional
public void deleteAll(){
throw new RuntimeException("deleteAll not implemented");
}
}
示例 DAO
@Transactional
public class JpaSampleDAO implements SampleDAO{
private JpaTemplate jpaTemplate;
public JpaSampleDAO(){}
public void setJpaTemplate(JpaTemplate jpaTemplate){
this.jpaTemplate = jpaTemplate;
}
@Override
//@Transactional
public void delete(Long sampleId) {
Sample dp = jpaTemplate.find(Sample.class, sampleId);
jpaTemplate.remove(dp);
}
@Override
@SuppressWarnings("unchecked")
public List<Sample> findAll() {
return jpaTemplate.find("from Sample");
}
@Override
public Sample findById(Long sampleId) {
return jpaTemplate.find(Sample.class, sampleId);
}
@Override
public Sample store(Sample dp) {
return jpaTemplate.merge(dp);
}
@Override
@SuppressWarnings("unchecked")
public void deleteAll(){
throw new RuntimeException("deleteAll not implemented");
}
}
Controller
@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
public ModelAndView sayHello(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Map<String, Object> model = new HashMap<String, Object>();
List<LearningEntry> le = learningEntryService.getLearningEntries();
model.put("learningEntries", le);
return new ModelAndView("main", model);
}
查看
<section id="content" class="body">
<ol id="posts-list" class="hfeed">
<c:forEach items="${learningEntries}" var="learningEntry">
<li>
<table class="wisiEntry">
<tr>
<td class="pictureCell">
<img class="wisiEntry-pic" src="${learningEntry.imagePath}" />
</td>
<td class="previousNextCell"
<div class="wisiEntry-nextSampleButton">Next</div>
<div class="wisiEntry-previousSampleButton">Previous</div>
<br />
<div class="wisiEntry-addTagButton">Tag</div>
<div class="wisiEntry-addCommentButton">Comment</div>
<br />
<div class="wisiEntry-uploadButton">Upload</div>
</td>
<td>
<!-- ERROR HAPPENS HERE. Samples should not be null -->
<c:forEach items="${learningEntry.samples}" var="sample" varStatus = "status">
<table class="sampleEntry" ${status.first ? '' : 'style = "display:none"'}>
<tr>
<td class="sampleCell">
<p class="description">
${sample.description}
</p>
<audio src="${sample.audioFileLocation}" controls>
Your browser does not support the <code>audio</code> element.
</audio>
</td>
<td class="voteCell">
<img class="upVote" src="/images/upArrow.jpeg" />
<span class="voteNumber">${sample.votes}</span>
<img class="downVote" src="/images/downArrow.jpeg" />
</td>
</tr>
</table>
</c:forEach>
</td>
</tr>
</table>
</li>
</c:forEach>
</ol><!-- /#posts-list -->
</section><!-- /#content -->
最佳答案
我希望您在调用过程中使用 findAll()
方法。您可以通过修改您的方法来加载所有关联的示例,如下所示。
public List<LearningEntry> findAll() {
List<LearningEntry> entries = jpaTemplate.find("from LearningEntry");
for(LearningEntry entry : entries){
entry.getSamples().size();
}
return entries;
}
或者,正如您所知,您也可以通过将 fetch
更改为 FetchType.EAGER
来实现此目的。但这可能并不适合所有情况。因此,前一种方式更好。
或者您可能不想在任何地方进行任何更改,并定义另一种方法来获取基于 LearningEntry
的所有示例,这样您就可以在某些事件上启动 AJAX 调用。但在这种情况下,这可能不适合。
关于java - Spring 和 Hibernate 的 LazyInitialization 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2040013/
我有 3 个名为Discipline、DisciplineMembership、DisciplineManagement 的实体: 一个学科有多个学科成员 一个学科有多个学科管理 Discipline
我在EJB2.0层上使用事务,并从那里调用DAO层,其中对db进行hibernate调用。 我现在遇到的问题是,hibernate 正在对数据库进行多次调用,因为我设置了 lazy="false",现
我在一个简单的网络应用程序上使用 wicket 和 hibernate (jpa)。 Task.java与Load.java的关系如下: 任务.java: @ManyToOne( targetEnti
我在我的 java 项目中使用 Hibernate。我有以下实体结构: @Entity @Table(name = "car") public class Car implements Seriali
我正在查看 Reflector 中的 LazyInitializer.EnsureInitialized(ref T, Func{T}),该方法中似乎有一个 volatile 局部变量 volatil
我认为我缺少一些有关 Hibernate 工作原理的基本知识,特别是延迟加载。我的问题是调试,因为我不确定这是 Hibernate 问题还是变相的 Spring 问题。我想在进行一些重大重构之前我应该
LazyInitializer 之间有什么区别?和 Lazy类?我知道他们都只会按需初始化对象。我什么时候需要使用它们中的每一个? 最佳答案 我不确定你是否还在研究这个,但我不得不深入研究这两个 La
我使用 Hibernate 5.2.11 作为 JPA 提供程序。我有一个带注释的类 (PurchaseOrder) 和另一个带注释的类 (Customer) 作为具有多对一关系的字段。但因为遗留代码
我正在尝试实现一个 Hibernate session /事务拦截器,以避免在 Struts2 中对 json 结果产生 LazyInitializationException,但无论如何我都会遇到此
我的应用程序使用 Struts2(mvc)、Spring(依赖注入(inject))、带有 Hibernate 的 JPA、JUnit 以及 struts2-junit 插件和 struts2 spr
我是一名优秀的程序员,十分优秀!