为什么我看不到oneToOne的数据?

时间:2015-12-07 14:08:53

标签: java jpa

我是JPA的新手

我创建了两个实体问卷和患者

我希望调查问卷能有患者的数据。 在我使用patientId创建调查问卷之前,但现在我想添加新结构以获取所有患者数据。

我添加了PatientData的新字段,并添加了新的连接(我希望我做得好)。

当我打电话询问AllQuestionnaires时的问题我没有得到patientData的数据。

你可以告诉我吗?

@Entity
@NamedQueries({
        @NamedQuery(name = "AllQuestionnaires", query = "select q from Questionnaire q order by q.patientId") })
public class Questionnaire implements Serializable {

    private static final long serialVersionUID = 1L;

    public Questionnaire() {
    }

    @Id
    @GeneratedValue
    private long id;
    private long patientId;
    private Timestamp created;





    @OneToOne
    @JoinColumn(name = "patientId" ,referencedColumnName = "id" ,  insertable = false, updatable = false)
    private Patient patientData;


    public Patient getPatient() {
        return patientData;
    }

    public void setPatient(Patient patient) {
        this.patientData = patient;
    }

    @OneToMany(cascade = CascadeType.ALL)
    private List<QuestionnaireAnswer> answers;

    public long getId() {
        return id;
    }

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

    public long getPatientId() {
        return patientId;
    }

    public void setPatientId(long patientId) {
        this.patientId = patientId;
    }

    public Timestamp getCreated() {
        return created;
    }

    public void setCreated(Timestamp created) {
        this.created = created;
    }

    public void setCreated(long created) {
        this.created = new Timestamp(created);
    }

    public List<QuestionnaireAnswer> getAnswers() {
        return answers;
    }

    public void setAnswers(List<QuestionnaireAnswer> answers) {
        this.answers = answers;
    }

}

班级病人

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Transient;


@Entity
@NamedQueries({
        @NamedQuery(name = "AllPatients", query = "select p from Patient p"),
        @NamedQuery(name = "PatientsByUserType", query = "select p from Patient p where p.userType = :userType"),
        @NamedQuery(name = "PatientByEmail", query = "select p from Patient p where p.email = :email"),
        @NamedQuery(name = "PatientById", query = "select p from Patient p where p.id = :id") })
    public class Patient implements Serializable {
        public enum UserType {
            PATIENT,
            CONTROL
        }
        private static final long serialVersionUID = 1L;

        public Patient() {
        }

        @Id
        @GeneratedValue
        private long id;
        private String firstName;
        private String lastName;
        private String email;
        private String password;
        private int gender;
        private Timestamp birthday;
        private Timestamp diagnoseDate;
        private Timestamp created;
        private UserType userType;

        @Transient
        private ArrayList<Task> lastSubmittedTasks;

        public UserType getUserType() {
            return userType;
        }

        public void setUserType(UserType userType) {
            this.userType = userType;
        }

        public long getId() {
            return id;
        }

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

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public int getGender() {
            return gender;
        }

        public void setGender(int gender) {
            this.gender = gender;
        }

        public Timestamp getBirthday() {
            return birthday;
        }

        public void setBirthday(Timestamp birthday) {
            this.birthday = birthday;
        }

        public void setBirthday(long birthday) {
            this.birthday = new Timestamp(birthday);
        }

        public Timestamp getDiagnoseDate() {
            return diagnoseDate;
        }

        public void setDiagnoseDate(Timestamp diagnoseDate) {
            this.diagnoseDate = diagnoseDate;
        }

        public void setDiagnoseDate(long diagnoseDate) {
            this.diagnoseDate = new Timestamp(diagnoseDate);
        }

        public Timestamp getCreated() {
            return created;
        }

        public void setCreated(Timestamp created) {
            this.created = created;
        }

        public void setCreated(long created) {
            this.created = new Timestamp(created);
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public ArrayList<Task> getLastSubmittedTask() {
            return lastSubmittedTasks;
        }

        public void setLastSubmittedTasks(ArrayList<Task> lastSubmittedTasks) {
            this.lastSubmittedTasks = lastSubmittedTasks;
        }

    }


@RequestMapping(value = "/questionnaires", method = RequestMethod.POST)
    @ResponseBody
    public String createQuestionnaires(@RequestBody String body, HttpSession session) {
        EntityManager em = emf.createEntityManager();
        long patientId = (long) session.getAttribute("patientId");

        try {
            Questionnaire questionnaire = gson.fromJson(body, Questionnaire.class);
            questionnaire.setPatientId(patientId);
            questionnaire.setCreated(new Timestamp(System.currentTimeMillis()));
            em.getTransaction().begin();
            em.persist(questionnaire);
            em.getTransaction().commit();

            return "{\"status\": \"OK\", \"id\": " + questionnaire.getId() + "}";
        } finally {
            em.close();
        }
    }

0 个答案:

没有答案