gpt4 book ai didi

java - Spring MVC 和 Hibernate : How to print out left joined fields?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:21 24 4
gpt4 key购买 nike

我是 Spring (MVC) 和 Hibernate 的新手...我遇到了以下问题:

我正在开发一个 RSS 应用程序,简而言之,它解析 RSS 提要,将字段存储在数据库中,并在网页上显示 RSS 信息。

一些 RSS 提要有图像。我在解析和存储它时没有问题,但在将它与 RSS 信息一起显示时遇到了问题。 RSS 信息存储在一个表中(字段:id、标题、描述、url、链接、语言、日期),而 RSS 图像信息存储在另一个表中(字段:id、标题、描述、url、高度、宽度、 rss_id)。应该是一对一的关系。

我用于检索所有 RSS 的 Hibernate 查询如下所示并且正在运行:

public List<RSS> getAllRSS() {
return getHibernateTemplate().find("from RSS as rss left join fetch rss.rssImage as image");
}

为了打印出 RSS 信息,我使用以下代码:

RSSService rssService = (RSSService) ctx.getBean("rssService");
RSS rss = new RSS();
List list = rssService.listAllRSS();

Iterator i = list.iterator();
while(i.hasNext()){
rss = (RSS) i.next();
System.out.println("rss title:" + rss.getTitle());
}

这是有效的。但是如何从图像中检索字段?

RSS 类:

public class RSS {

private Integer id;
private String title;
private Date dateCreated;
private String description;
private String link;
private String url;
private String language;
private String rating;
private Date dateModified;
private Set rssItems;
private Set rssImage;

/**
* @return the id
*/
public Integer getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}

public Date getDateCreated() {
return dateCreated;
}

public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Date getDateModified() {
return dateModified;
}

public void setDateModified(Date dateModified) {
this.dateModified = dateModified;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getLanguage() {
return language;
}

public void setLanguage(String language) {
this.language = language;
}

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getRating() {
return rating;
}

public void setRating(String rating) {
this.rating = rating;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

/**
* @return the rssItems
*/
public Set getRssItems() {
return rssItems;
}

/**
* @param rssItems the rssItems to set
*/
public void setRssItems(Set rssItems) {
this.rssItems = rssItems;
}

/**
* @return the rssImage
*/
public Set getRssImage() {
return rssImage;
}

/**
* @param rssImage the rssImage to set
*/
public void setRssImage(Set rssImage) {
this.rssImage = rssImage;
}
}

RSS 图片类:

public class RSSImage {

private Integer id;
private String title;
private String url;
private String description;
private String width;
private String height;
private Date dateCreated;
private Date dateModified;
private RSS rss;

/**
* @return the id
*/
public Integer getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}

/**
* @return the title
*/
public String getTitle() {
return title;
}

/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}

/**
* @return the url
*/
public String getUrl() {
return url;
}

/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}

/**
* @return the description
*/
public String getDescription() {
return description;
}

/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}

/**
* @return the width
*/
public String getWidth() {
return width;
}

/**
* @param width the width to set
*/
public void setWidth(String width) {
this.width = width;
}

/**
* @return the height
*/
public String getHeight() {
return height;
}

/**
* @param height the height to set
*/
public void setHeight(String height) {
this.height = height;
}

/**
* @return the dateCreated
*/
public Date getDateCreated() {
return dateCreated;
}

/**
* @param dateCreated the dateCreated to set
*/
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}

/**
* @return the dateModified
*/
public Date getDateModified() {
return dateModified;
}

/**
* @param dateModified the dateModified to set
*/
public void setDateModified(Date dateModified) {
this.dateModified = dateModified;
}

/**
* @return the rss
*/
public RSS getRss() {
return rss;
}

/**
* @param rss the rss to set
*/
public void setRss(RSS rss) {
this.rss = rss;
}
}

谢谢:)

最佳答案

您将 rssImage 映射为一个集合,而不是一对一的关系。因此,您必须遍历您的图像集来打印每个单独的图像。

否则,更新您的映射,使 rssImage 只是一张图像。

关于java - Spring MVC 和 Hibernate : How to print out left joined fields?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6330011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com