Spring MVC and Hibernate: how to print left merged fields?

I am new to Spring (MVC) and Hibernate ... I had the following problem:

I am developing an RSS application that briefly analyzes an RSS feed, stores fields in a database, and displays RSS information on a web page.

Some RSS feeds received an image. I have no problem parsing and saving it, but I had a problem displaying it along with the RSS information. RSS information is stored in one table (fields: identifier, title, description, URL, link, language, date), while RSS image information is stored in another (fields: id, title, description, url, height, rss_id). There should be a one-to-one relationship.

My Hibernate request to retrieve all of the RSS looks like this:

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

To print RSS information, I use the following code:

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());
}

And it works. But how can I extract fields from an image?

RSS class:

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 Image Class:

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;
}
}

Thank:)

+3
source share
2 answers

You have rssImage displayed as a collection, not a one-to-one relationship. Therefore, you need to iterate through your image collection in order to print each individual image.

Otherwise, update your mappings, so rssImage is just one image.

+2
source

In jsp:

 <table>
 <c:forEach var="rssItem" items="${rss}">
  <tr>
   <td>
     ${rssItem.name}
   </td> 
  </tr>
 </c:forEach>
</table>

then in your controller

@RequestMapping(value = "/something")
public ModelAndView getRSS () {
ModelAndView mv = new ModelAndView("someJSP.jsp");
List<RSS> rss = //get your RSS from Hibernate
mv.addObject("rss",rss);
return mv;   
}
+1
source

All Articles