[英]Lazy loading of the content
I have two entities : User
and Post
(relation one-to-many). 我有两个实体: User
和Post
(一对多关系)。 Post
fields: id
, creationDate
, title
, content
, user
. Post
字段: id
, creationDate
, title
, content
, user
。
Data is stored in the database and accessed via Hibernate. 数据存储在数据库中并通过Hibernate访问。
I have a controller to pass Post object as a JSON to JavaScript. 我有一个控制器,用于将Post对象作为JSON传递给JavaScript。 Then it is shown on the web page. 然后将其显示在网页上。 But it is not always necessary to pass all the Post
fields. 但是,并非总是必须传递所有Post
字段。 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). 例如,我只需要向用户显示标题和创建日期,并且如果用户按下“ Show content
”按钮,则仅需要显示帖子内容(仅在需要显示时才需要从服务器请求该内容) )。
So here is a problem: How can I implement lazy initialization of the content field in Post object? 所以这是一个问题:如何在Post对象中实现content字段的延迟初始化? 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? 我是否应该在控制器中编写两个方法:一个方法用于生成带有Posts列表的JSON,并将content字段设置为null
或空String
,另一个方法仅传递内容字符串?
Make post content an object and a single table in db. 使发布内容成为对象,并在db中创建一个表。
It looks like the following in java: 在Java中如下所示:
public class Post {
...
PostContent postContent;
}
First you may try to initialize the lazy collection at the DAO via Hibernate.initialize(lazyCollection)
. 首先,您可以尝试通过Hibernate.initialize(lazyCollection)
在DAO上初始化惰性集合。 If it didn't work then either use FetchType.EAGER
or keep the session open during request and the collection should be fetched when needed. 如果不起作用,则可以使用FetchType.EAGER
或在请求期间保持会话打开,并且应在需要时提取集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.