如何与3个表进行通信

时间:2014-05-28 06:13:22

标签: java mysql hibernate jpa hql

嗨我有一个表父,其字段是

    mysql> select * from parent;
+----+----------+------------+-----------------------------------+---------+------+
| id | category | is_deleted | name                              | version | cid  |
+----+----------+------------+-----------------------------------+---------+------+
|  1 | default  |            | Front Office                      |       0 | NULL |
|  2 | default  |            | Food And Beverage                 |       0 | NULL |
|  3 | default  |            | House Keeping                     |       0 | NULL |
|  4 | default  |            | General                           |       0 | NULL |
|  5 | client   |            | SPA                               |       0 | NULL |
|  7 | client   |            | house                             |       0 | NULL |
|  8 | client   |            | test                              |       0 | NULL |
|  9 | client   |            | ggg                               |       0 |    1 |
| 10 | client   |            | dddd                              |       0 |    1 |
| 11 | client   |            | test1                             |       0 |    1 |
| 12 | client   |            | java                              |       0 |    1 |
| 13 | client   |            | dcfdcddd                          |       0 |    1 |
| 14 | client   |            | qqqq                              |       0 |    1 |
| 15 | client   |            | nnnnnn                            |       0 |    1 |
| 16 | client   |            | category                          |       0 |    1 |
| 17 | client   |            | sukant                            |       0 |    1 |
| 18 | client   |            | bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb |       0 |    1 |

我有另一个表parent_question

mysql> select * from parent_question;
+----+------------+---------+-----+------+
| id | is_deleted | version | pid | qid  |
+----+------------+---------+-----+------+
|  1 |            |       0 |   1 |    1 |
|  2 |            |       0 |   1 |    2 |
|  3 |            |       0 |   1 |    3 |
|  4 |            |       0 |   1 |    4 |
|  5 |            |       0 |   1 |    5 |
|  6 |            |       0 |   1 |    6 |
|  7 |            |       0 |   2 |    7 |
|  8 |            |       0 |   2 |    1 |
|  9 |            |       0 |   2 |    2 |
| 10 |            |       0 |   2 |    8 |
| 11 |            |       0 |   3 |    9 |
| 12 |            |       0 |   3 |    1 |
| 13 |            |       0 |   3 |   10 |
| 14 |            |       0 |   3 |   11 |
| 15 |            |       0 |   4 |   12 |
| 16 |            |       0 |   1 |    1 |
| 17 |            |       0 |   1 |    2 |
| 18 |            |       0 |   1 |    3 |
| 19 |            |       0 |   5 |   13 |
| 20 |            |       0 |   2 |    7 |
| 21 |            |       0 |   2 |    2 |
| 22 |            |       0 |   1 |   14 |
| 23 |            |       0 |   1 |   15 |
| 24 |            |       0 |   1 |   16 |
| 25 |            |       1 |   1 |   17 |
| 26 |            |       0 |   1 |   21 |
| 27 |            |       0 |   2 |   22 |
| 28 |            |       0 |  13 |   23 |
| 29 |            |       0 |   9 |   24 |
| 30 |            |       0 |  12 |   25 |
| 31 |            |       0 |  12 |   26 |
| 32 |            |       0 |  12 |   27 |
| 33 |            |       0 |  12 |   28 |
| 34 |            |       0 |  14 |   29 |
| 35 |            |       0 |  15 |   30 |
| 36 |            |       0 |  10 |   31 |
| 37 |            |       0 |   4 |   32 |
| 38 |            |       0 |  16 |   33 |
| 39 |            |       0 |  10 |   34 |
| 40 |            |       0 |   3 |   35 |
| 41 |            |       0 |  17 |   36 |
| 42 |            |       0 |   1 |   37 |
| 43 |            |       0 |   1 |   38 |
| 44 |            |       0 |  18 |   39 |
| 45 |            |       0 |  18 |   40 |
+----+------------+---------+-----+------+
45 rows in set (0.00 sec)

这是我的问题表

ysql> select * from question;
----+----------+------------+------------------------------------------------------------+--

 id | category | is_deleted | question                                                   | v

----+----------+------------+------------------------------------------------------------+--

  1 | default  |            | Staff Courtesy                                             |

  2 | default  |            | Staff Response                                             |

  3 | default  |            | Check In                                                   |

  4 | default  |            | Check Out                                                  |

  5 | default  |            | Travel Desk                                                |

  6 | default  |            | Door Man                                                   |

  7 | default  |            | Restaurant Ambiance                                        |

  8 | default  |            | Quality Of Food                                            |

  9 | default  |            | Cleanliness Of The Room                                    |

 10 | default  |            | Room Size                                                  |

 11 | default  |            | Room Amenities                                             |

 12 | default  |            | Any Other Comments ?                                       |

 13 | client   |            | How is Food?                                               |

 14 | client   |            | test question                                              |

 15 | client   |            | test1                                                      |

 16 | client   |            | test2                                                      |

 17 | client   |            | test2                                                      |

 18 | client   |            | test2                                                      |

 19 | client   |            | working                                                    |

 20 | client   |            | sss                                                        |

 21 | client   |            | ggggg                                                      |

 22 | client   |            | this is new question                                       |

 23 | client   |            | dddddddddddd                                               |

 24 | client   |            | ggggggggggggggggg                                          |

 25 | client   |            | what is a class?                                           |

 26 | client   |            | what is inheritance                                        |

 27 | client   |            | what is an object                                          |

 28 | client   |            | what is an abstract class?                                 |

 29 | client   |            | qqqq                                                       |

 30 | client   |            | nnnn question                                              |

 31 | client   |            | add some                                                   |

 32 | client   |            | general question                                           |

 33 | client   |            | category question                                          |

 34 | client   |            | hhhhhhhh                                                   |

 35 | client   |            | this is hos                                                |

 36 | client   |            | gggg                                                       |

 37 | client   |            | dddd                                                       |

 38 | client   |            | ddddd                                                      |

 39 | client   |            | bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb |

 40 | client   |            | ggg                                                        |

----+----------+------------+------------------------------------------------------------+--

我所知道的我有一个parent_question表的pid;

我想要什么问题表的问题;

例如。如果我找到了pid为18的问题。 所以从parent_question表我可以知道qid是39和40并且从问题表39中可以看出bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

我尝试了什么

String queryString="SELECT distinct q FROM Question q , ParentQuestion pq ,Parent p where pq.qid.id = q.id and  p.id = pq.pid.id and p.category = 'default' AND p.id = "+pid;
    Query query=entityManagerUtil.getQuery(queryString);
    List questionsList =    query.getResultList();
    return questionsList;

但它没有用。我的意思是我没有在列表中得到任何东西。任何人都可以指出我的错误。

问题实体类

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import javax.validation.constraints.Size;

import org.springframework.beans.factory.annotation.Configurable;

@Configurable
@Entity
public class Question {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    private String question;

    private String category;
    private boolean isDeleted; 
    public boolean isDeleted() {
        return isDeleted;
    }

    public void setDeleted(boolean isDeleted) {
        this.isDeleted = isDeleted;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }



}

父实体类

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.beans.factory.annotation.Configurable;

@Configurable
@Entity
public class Parent {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;


    @ManyToOne
    private Client cid;
    public Client getCid() {
        return cid;
    }

    public void setCid(Client cid) {
        this.cid = cid;
    }



    private boolean isDeleted; 

   /* @ManyToOne
    private Client cid;

    public Client getCid() {
        return cid;
    }

    public void setCid(Client cid) {
        this.cid = cid;
    }*/

    public boolean isDeleted() {
        return isDeleted;
    }

    public void setDeleted(boolean isDeleted) {
        this.isDeleted = isDeleted;
    }

    private String name;

     private String category;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }




}

1 个答案:

答案 0 :(得分:0)

评论员对您的课程感到困惑,这表明您可能需要考虑您的设计。 suninsky有一个很好的观点,你可能不需要实体类调用ParentQuestion(当然,除非ParentQuestion有关于关系的额外数据)。以下是我要问的一些典型问题。

  1. 每个问题都有父母吗?如果是这样,那么你的Question类可能应该有一个parent属性,映射为@ManyToOne

  2. 每个Parent对象都有一组问题吗?如果是,那么Parent对象应该有一个名为questions的属性,其类型是某种Question对象的集合。