延迟加载内容 [英] Lazy loading of the content
问题描述
我有两个实体:用户
和发布
(关系一对多)。 发布
字段: 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屋!