Java 如何与3个表进行通信

Java 如何与3个表进行通信,java,mysql,hibernate,jpa,hql,Java,Mysql,Hibernate,Jpa,Hql,嗨,我有一个表父项,它的字段是 mysql> select * from parent; +----+----------+------------+-----------------------------------+---------+------+ | id | category | is_deleted | name | version | cid | +----+----------+------------+

嗨,我有一个表父项,它的字段是

    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 |
我还有一个问题

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                                                        |

----+----------+------------+------------------------------------------------------------+--
我所知道的我有家长问题表的pid

我想要什么问题表问题

例如,如果让我找到pid为18的问题。 因此,从父_问题表中,我可以知道qid是39和40,从问题表中,39指BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

我尝试的

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




}
父实体类

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




}

注释者会被您的类搞糊涂,这很好地表明您可能需要考虑您的设计。suninsky有一个很好的观点,您可能不需要有一个实体类调用ParentQuestion(当然,除非ParentQuestion有关于关系的额外数据)。下面是我要问的一些典型问题

  • 每个问题都有一个家长吗?如果是这样,那么您的问题类上可能会有一个
    parent
    属性,映射为@ManyToOne

  • 每个父对象都有一组问题吗?如果是,那么父对象可能应该有一个名为
    questions
    的属性,该属性的类型是Question对象的某种集合


  • 你为什么要做pq.pid.id而不是仅仅做pq。pid@HithamS.AlQadheeb因为pid是一个对象,
    问题
    &
    父项
    ,和单向或双向之间有什么关系?您能为我们展示该类的实体类详细信息吗?我认为可能不需要
    ParentQuestion
    实体。@suninsky我添加了问题和父实体classes@suninsky你能帮我什么忙吗