[英]Which data format is suitable to transfer data (result of left join db query) from servlet to JSP?
[using JPA, MySQL, MVC, Servlets, JSP] If I read some data from database LEFT JOIN
-ing three tables (inside method of DAO object) how should I format that result, so i can set it as request attribute (in servlet) and forwards to JSP page? [使用JPA,MySQL,MVC,Servlet,JSP]如果我从数据库LEFT JOIN
读取一些数据-设置三个表(DAO对象的内部方法),应如何格式化该结果,因此可以将其设置为请求属性(在Servlet中) )并转发到JSP页面?
Entities(tables in db): 实体(db中的表):
Post
: Post
:
@Entity
@Table(name = "post")
public class Post implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "post_id", unique = true, nullable = false)
private Integer id;
@Column(name = "post_title", length=300, unique = false, nullable = false)
private String title;
@Column(name = "post_date", unique = false, nullable = false)
private Date date;
@Column(name = "post_summary", length=1000, unique = false, nullable = true)
private String summary;
@Column(name = "post_content", length=50000, unique = false, nullable = false)
private String content;
@Column(name = "post_visitors", unique = false, nullable = false)
private Integer visitors;
@ManyToOne
@JoinColumn (name = "user_id", referencedColumnName="user_id", nullable = false)
private User user;
@ManyToOne
@JoinColumn (name = "category_id", referencedColumnName="category_id", nullable = false)
private Category category;
@OneToMany(cascade = { ALL }, fetch = LAZY, mappedBy = "post")
private Set<Comment> comments = new HashSet<Comment>();
...
Entity Comment
: 实体Comment
:
@Entity
@Table(name = "comment")
public class Comment implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "comment_id", unique = true, nullable = false)
private Integer id;
@Column(name = "comment_title", length=300, unique = false, nullable = false)
private String title;
@Column(name = "comment_date", unique = false, nullable = false)
private Date date;
@Column(name = "comment_content", length=600, unique = false, nullable = false)
private String content;
@ManyToOne
@JoinColumn (name = "user_id", referencedColumnName="user_id", nullable = false)
private User user;
@ManyToOne
@JoinColumn (name = "post_id", referencedColumnName="post_id", nullable = false)
private Post post;
...
Entity User
: 实体User
:
@Entity
@Table(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
private Integer id;
@Column(name = "user_name", length=45, unique = false, nullable = false)
private String name; // "first_name" + ' ' + "last_name"
//URL address to user's image
@Column(name = "user_image", length=500, unique = false, nullable = true)
private String image;
@Column(name = "user_username", length=45, unique = false, nullable = false)
private String username;
@Column(name = "user_password", length=45, unique = false, nullable = false)
private String password;
...
So, I would like to make a method, probably inside PostDAO
object that will look something like this: 因此,我想制作一个方法,可能在PostDAO
对象内部,看起来像这样:
public <SomeDataTypeFormat???> getPostsSummaries(){
Query q = em.createNativeQuery("SELECT
post_title,
post_summary,
post_date,
COUNT(comment_id) AS comment_cnt,
user.user_name
FROM
post
LEFT JOIN user USING(user_id)
LEFT JOIN comment USING(post_id)
GROUP BY
post_id
ORDER BY
comment_cnt DESC");
...
}
Method returns some fields from all three tables in database. 方法从数据库中的所有三个表返回一些字段。 Do I need to make separate class and store those data in objects of that class? 我是否需要创建单独的类并将这些数据存储在该类的对象中? Or JSON (although I haven't worked with it yet)? 还是JSON(尽管我还没有使用它)?
What is the practice? 这是什么做法? What is easiest data format to use and forward from servlet to JSP, for some fields gotten as a result of joining couple tables? 对于由于联接几个表而获得的某些字段,最简单的数据格式是什么,该格式最容易使用并从servlet传递到JSP?
It depends on your objective; 这取决于您的目标; to get the data to the browser, JSON and AJAX isn't a bad choice. 为了将数据发送到浏览器,JSON和AJAX并不是一个不错的选择。 To get the data to the JSP (from the Servlet), you'll probably want a Data Transfer Object (or possibly an immutable Value Object ). 为了将数据从Servlet获取到JSP,您可能需要一个数据传输对象 (或者可能是不可变的Value Object )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.