Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java EclipseLink EntityManager SQL在两个表上联接_Java_Jpa_Eclipselink - Fatal编程技术网

Java EclipseLink EntityManager SQL在两个表上联接

Java EclipseLink EntityManager SQL在两个表上联接,java,jpa,eclipselink,Java,Jpa,Eclipselink,我有两个不同的表:主题和问题,我需要对这两个表进行SQL联接。表主题有其属性:名称和快捷方式。表问题有其属性:问题编号,文本,主题-事实上,表问题中的主题是主题的一个快捷方式 我试过这样的方法,我在一个主题中看到: Query q = em.createNativeQuery("SELECT q.question_number, q.text, s.name, s.shortcut FROM " + "( questions

我有两个不同的表:
主题
问题
,我需要对这两个表进行SQL联接。表
主题
有其属性:
名称
快捷方式
。表
问题
有其属性:
问题编号
文本
主题
-事实上,表
问题
中的
主题
主题
的一个
快捷方式

我试过这样的方法,我在一个主题中看到:

Query q = em.createNativeQuery("SELECT q.question_number, q.text, s.name, s.shortcut FROM "
                                   + "( questions q INNER JOIN subjects s ON q.subject=s.shortcut );", QuestionSubject.class);
QuestionSubject.class
是一个
@实体
类,具有
问题
表和
主题
表的属性。调用此方法后,我看到在我的数据库中创建了一个名为
QUESTIONSUBJECT
的新表,而这正是我不想做的

有人能帮我解决其他问题吗

注意:我这样做是为了使用输出作为HTTP请求的响应,所以我需要将这两个集合在一起。我需要返回
列表
JSON
字符串

编辑:使用MySQL数据库

问题
表格实体类:

package model;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "questions")
@XmlRootElement
public class Question implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Basic(optional = false)
   @Column(name = "question_number")
    private Integer questionNumber;
   @Column(name = "text")
    private String text;
   @Column(name = "subject")
    private String subject;

    public Question() {
    }

    public Question(Integer questionNumber) {
        this.questionNumber = questionNumber;
    }

    public Question( String text, String subject) {
        this.text = text;
        this.subject = subject;
    }

    public Question(Integer questionNumber, String text, String subject) {
        this.questionNumber = questionNumber;
        this.text = text;
        this.subject = subject;
    }

    public Integer getQuestionNumber() {
        return questionNumber;
    }

    public void setQuestionNumber(Integer questionNumber) {
        this.questionNumber = questionNumber;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (questionNumber != null ? questionNumber.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Question)) {
            return false;
        }
        Question other = (Question) object;
        if ((this.questionNumber == null && other.questionNumber != null) || (this.questionNumber != null && !this.questionNumber.equals(other.questionNumber))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Rest.Questions[ questionNumber=" + questionNumber + " ]";
    }

}
package model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class QuestionSubject implements Serializable
{
    @Id
    @Column(name = "question_number")
    private Integer questionNumber;
    @Column(name = "text")
    private String text;

    @Column(name = "shortcut")
    private String shortcut;
    @Column(name = "name")
    private String name;

    public Integer getQuestionNumber() {
        return questionNumber;
    }

    public void setQuestionNumber(Integer questionNumber) {
        this.questionNumber = questionNumber;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getShortcut() {
        return shortcut;
    }

    public void setShortcut(String shortcut) {
        this.shortcut = shortcut;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
主题
表格实体类

package model;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "subjects")
@XmlRootElement
public class Subject implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
   @Basic(optional = false)
   @NotNull
   @Size(min = 1, max = 5)
   @Column(name = "shortcut")
    private String shortcut;
    @Basic(optional = false)
   @NotNull
   @Lob
   @Size(min = 1, max = 65535)
   @Column(name = "name")
    private String name;

    public Subject() {
    }

    public Subject(String shortcut) {
        this.shortcut = shortcut;
    }

    public Subject(String shortcut, String name) {
        this.shortcut = shortcut;
        this.name = name;
    }

    public String getShortcut() {
        return shortcut;
    }

    public void setShortcut(String shortcut) {
        this.shortcut = shortcut;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (shortcut != null ? shortcut.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Subject)) {
            return false;
        }
        Subject other = (Subject) object;
        if ((this.shortcut == null && other.shortcut != null) || (this.shortcut != null && !this.shortcut.equals(other.shortcut))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Rest.Subjects[ shortcut=" + shortcut + " ]";
    }

}
问题主题
实体类:

package model;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "questions")
@XmlRootElement
public class Question implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Basic(optional = false)
   @Column(name = "question_number")
    private Integer questionNumber;
   @Column(name = "text")
    private String text;
   @Column(name = "subject")
    private String subject;

    public Question() {
    }

    public Question(Integer questionNumber) {
        this.questionNumber = questionNumber;
    }

    public Question( String text, String subject) {
        this.text = text;
        this.subject = subject;
    }

    public Question(Integer questionNumber, String text, String subject) {
        this.questionNumber = questionNumber;
        this.text = text;
        this.subject = subject;
    }

    public Integer getQuestionNumber() {
        return questionNumber;
    }

    public void setQuestionNumber(Integer questionNumber) {
        this.questionNumber = questionNumber;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (questionNumber != null ? questionNumber.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Question)) {
            return false;
        }
        Question other = (Question) object;
        if ((this.questionNumber == null && other.questionNumber != null) || (this.questionNumber != null && !this.questionNumber.equals(other.questionNumber))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Rest.Questions[ questionNumber=" + questionNumber + " ]";
    }

}
package model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class QuestionSubject implements Serializable
{
    @Id
    @Column(name = "question_number")
    private Integer questionNumber;
    @Column(name = "text")
    private String text;

    @Column(name = "shortcut")
    private String shortcut;
    @Column(name = "name")
    private String name;

    public Integer getQuestionNumber() {
        return questionNumber;
    }

    public void setQuestionNumber(Integer questionNumber) {
        this.questionNumber = questionNumber;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getShortcut() {
        return shortcut;
    }

    public void setShortcut(String shortcut) {
        this.shortcut = shortcut;
    }

    public String getName() {
        return name;
    }

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

创建该表是因为您定义了一个名为
QuestionSubject
的类,该类被注释为
@Entity
。默认情况下,表名是类名

您可以使用
@Table(name=“Subjects”)

如果您在类
Question
Subject
之间的相关字段上定义一个
@manytomy
映射,而根本不定义
QuestionSubject
类,则会发生几乎相同的情况

我建议您在此处查看以获取更多信息:

编辑
如果需要多个映射,则需要此表。否则,您只能有一个单独的resp。manytone关系(使用外键)。

我从未使用EclipseLink作为JPA提供者(我不知道哪个数据库在后面),但我猜表不是在查询时创建的。通常,映射(类定义)定义表结构,并在启动时创建表。可以发布相关类的代码吗?这个命令在查询时创建了一个新表。当然,我会在2分钟内上传它事实上,当我在SQL中创建一个多对多关系时,是否真的会创建一个表示这个事实的新表?当然,否则该多对多关系应该如何持久化?它被称为“联接表”、“映射表”或类似的名称。