Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate, display join left results in Spring
    primarykey
    data
    text
    <p>I'm learning Spring and Hibernate and i have small but quite important problem.</p> <p>I want to get data from database:</p> <pre><code> Query query = session.createSQLQuery("SELECT * FROM pool a LEFT JOIN pool_question b on a.id = b.poolid"); </code></pre> <p>and then sent results as list to jsp file from my controller </p> <pre><code>model.addAttribute("pools", pool); </code></pre> <p>My database is quite simple:</p> <p>pool is:</p> <pre><code>id name slug date_create deactivation_date creator_id active </code></pre> <p>and poolquestion is</p> <pre><code>id poolid answer order question </code></pre> <p>The problem is, that when i'm trying to make a loop in jsp:</p> <pre><code>&lt;c:forEach items="${pools}" var="pool"&gt; &lt;td&gt;&lt;c:out value="${pool.name}" /&gt;&lt;/td&gt; &lt;/c:forEach&gt; </code></pre> <p>tomcat displays me error.</p> <p>When i check my query in phpmyadmin the result is ok.</p> <p>The trick is, that when i do query like this:</p> <pre><code>Query query = session.createQuery("FROM Pool"); </code></pre> <p>The results in jsp are displayed properly.</p> <p>Could enyone help me how to display result of this query in jsp?</p> <p>My Pool.java is:</p> <pre><code> package com.pool.app.domain; // Generated 2011-12-20 12:45:22 by Hibernate Tools 3.4.0.CR1 import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; /** * Pool generated by hbm2java */ @Entity @Table(name = "pool", catalog = "pool") public class Pool implements java.io.Serializable { private Integer id; private String name; private String slug; private Date dateCreate; private Date deactivationDate; private int creatorId; private int active; private Set&lt;PoolQuestion&gt; poolQuestions = new HashSet&lt;PoolQuestion&gt;(0); public Pool() { } public Pool(String name, String slug, Date dateCreate, Date deactivationDate, int creatorId, int active) { this.name = name; this.slug = slug; this.dateCreate = dateCreate; this.deactivationDate = deactivationDate; this.creatorId = creatorId; this.active = active; } public Pool(String name, String slug, Date dateCreate, Date deactivationDate, int creatorId, int active, Set&lt;PoolQuestion&gt; poolQuestions) { this.name = name; this.slug = slug; this.dateCreate = dateCreate; this.deactivationDate = deactivationDate; this.creatorId = creatorId; this.active = active; this.poolQuestions = poolQuestions; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "name", nullable = false, length = 200) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Column(name = "slug", nullable = false, length = 200) public String getSlug() { return this.slug; } public void setSlug(String slug) { this.slug = slug; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "date_create", nullable = false, length = 19) public Date getDateCreate() { return this.dateCreate; } public void setDateCreate(Date dateCreate) { this.dateCreate = dateCreate; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "deactivation_date", nullable = false, length = 19) public Date getDeactivationDate() { return this.deactivationDate; } public void setDeactivationDate(Date deactivationDate) { this.deactivationDate = deactivationDate; } @Column(name = "creator_id", nullable = false) public int getCreatorId() { return this.creatorId; } public void setCreatorId(int creatorId) { this.creatorId = creatorId; } @Column(name = "active", nullable = false) public int getActive() { return this.active; } public void setActive(int active) { this.active = active; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "pool") public Set&lt;PoolQuestion&gt; getPoolQuestions() { return this.poolQuestions; } public void setPoolQuestions(Set&lt;PoolQuestion&gt; poolQuestions) { this.poolQuestions = poolQuestions; } } </code></pre> <p>and PoolQuestion.java is:</p> <pre><code> package com.pool.app.domain; // Generated 2011-12-20 12:45:22 by Hibernate Tools 3.4.0.CR1 import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; /** * PoolQuestion generated by hbm2java */ @Entity @Table(name = "pool_question", catalog = "pool") public class PoolQuestion implements java.io.Serializable { private Integer id; private Pool pool; private String answer; private int order; private String question; public PoolQuestion() { } public PoolQuestion(Pool pool, String answer, int order) { this.pool = pool; this.answer = answer; this.order = order; } public PoolQuestion(Pool pool, String answer, int order, String question) { this.pool = pool; this.answer = answer; this.order = order; this.question = question; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "poolid", nullable = false) public Pool getPool() { return this.pool; } public void setPool(Pool pool) { this.pool = pool; } @Column(name = "answer", nullable = false, length = 500) public String getAnswer() { return this.answer; } public void setAnswer(String answer) { this.answer = answer; } @Column(name = "order", nullable = false) public int getOrder() { return this.order; } public void setOrder(int order) { this.order = order; } @Column(name = "question", length = 500) public String getQuestion() { return this.question; } public void setQuestion(String question) { this.question = question; } } </code></pre> <p>And the main question - how the HQL query should looks, becouse query like this:</p> <pre><code>Query query = session.createQuery("From Pool as p left join fetch p.id as s"); </code></pre> <p>doesn't works for me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload