延迟加载内容 [英] Lazy loading of the content

查看:110
本文介绍了延迟加载内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体:用户发布(关系一对多)。 发布字段: id creationDate title content user



数据存储在数据库中并通过Hibernate访问。

我有一个控制器将Post对象作为JSON传递给JavaScript。然后它会显示在网页上。但并不总是需要传递所有 Post 字段。例如,我只需要向用户显示标题和创建日期,如果用户按下显示内容按钮,则只需显示发布内容(其中我只想在需要显示时从服务器请求)。



所以这里有一个问题:我如何实现Post对象中的内容字段的初始化 lazy ?我应该在我的控制器中写两种方法:一种用于生成带有帖子列表的JSON并将内容字段设置为 null 或空字符串,另一个只传递内容字符串?

解决方案

在db中创建一个对象和一个表。 p>

它在java中看起来如下所示:

  public class Post {

...


PostContent postContent;
}


I have two entities : User and Post (relation one-to-many). Post fields: id, creationDate, title, content, user.

Data is stored in the database and accessed via Hibernate.

I have a controller to pass Post object as a JSON to JavaScript. Then it is shown on the web page. But it is not always necessary to pass all the Post fields. For ex., I need to show to the user only title and creation date, and if the user presses the button Show content, only then I need to show post content (which I want to request from server only when it is need to show).

So here is a problem: How can I implement lazy initialization of the content field in Post object? Should I write two methods in my controller: one for generating JSON with list of Posts and setting content field to null or empty String, and another to pass only content string?

解决方案

Make post content an object and a single table in db.

It looks like the following in java:

public class Post {

  ...


  PostContent postContent;
}

这篇关于延迟加载内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆