两个表上的EclipseLink EntityManager SQL JOIN

时间:2017-01-07 12:43:02

标签: java jpa eclipselink

我有两个不同的表:subjectsquestions,我需要在这两个表上进行SQL JOIN。表格subjects的属性为:nameshortcut。表格questions有其属性:question_numbertextsubject - 事实上,表subject中的questionsshortcut一个subject

我尝试了类似这样的东西,我在一个stackoverflow主题中看到了:

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@Entity类,具有questions表和subjects表的属性。调用此方法后,我看到在我的数据库中创建了一个名为QUESTIONSUBJECT的新表,这是我不想做的。

任何人都可以帮我解决其他问题吗?

P.S。:我这样做是为了使用输出作为HTTP请求的响应,所以我需要将这两者合二为一。我需要返回ListJSON字符串。

编辑:使用MySQL数据库。

questions表实体类:

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 + " ]";
    }

}

subjects表实体类。

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 + " ]";
    }

}

QuestionSubject实体类:

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

1 个答案:

答案 0 :(得分:1)

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

您可以使用Subjects

覆盖@Table(name = "subjects")中的名称

如果要在类@ManyToManyQuestion之间的相关字段上定义Subject映射而不定义QuestionSubject类,则会发生几乎相同的情况。

我建议您在这里查看以获取更多信息:

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/ManyToMany

修改 如果您需要manyToMany映射,则需要此表。否则你只能拥有oneToMany。 manyToOne关系(使用外键)。