Jeg er temmelig ny i programmering, så bær over med mig...
Programmer: Netbeans 7.2.1 samt en embedded Derby database.
Jeg har problemer med at få forespørgslerne til at virke mellem to tabeller.
Netbeans har lavet to Entity klasser for mig. Entity klassen Senge indeholder en Collection: Collection<Patient> patientCollection, der - så vidt jeg har forstået, er den der skal tage sig af mine join-forespørgsler
Problemet er at den collection virker ikke. Når jeg debugger kan jeg se den, og den viser sig med en exception der ser sådan her ud:
>Exception occurred in target VM:
Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'SENGE_PATIENT' does not exist.
Error Code: 30000
Call: SELECT t1.ID_PATIENT, t1.EFTERNAVN, t1.FOEDSELSDAG, t1.NAME, t1.TIMESTAMP, t1.SPECIALE, t1.SENG FROM SENGE_PATIENT t0, PATIENT t1 WHERE ((t0.Senge_ID_SENG = ?) AND (t1.ID_PATIENT = t0.patientCollection_ID_PATIENT))
bind => [1 parameter bound]
Query: ReadAllQuery(name="file:/C:/Users/kath/Documents/NetBeansProjects/TavleManager/build/cluster/modules/ext/TavleLibrary.jar_TavleLibraryPU" referenceClass=Patient sql="SELECT t1.ID_PATIENT, t1.EFTERNAVN, t1.FOEDSELSDAG, t1.NAME, t1.TIMESTAMP, t1.SPECIALE, t1.SENG FROM SENGE_PATIENT t0, PATIENT t1 WHERE ((t0.Senge_ID_SENG = ?) AND (t1.ID_PATIENT = t0.patientCollection_ID_PATIENT))")
.... og meget mere.
Det ser ud til at den forventer at se en tabel ved navn 'SENGE_PATIENT', men sådan en findes der jo ikke.... Hvor kommer det fra?
Jeg kan ikke overskue om det er min databaseopsætning der er forkert, eller om det er den (autogenererede) kode i entity klasserne, der er forkert.
Tabellerne er lavet sådan her:
CREATE TABLE SENGE (
ID_SENG INTEGER NOT NULL PRIMARY KEY
GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
NAME VARCHAR (10) UNIQUE
);
CREATE TABLE PATIENT (
ID_PATIENT INTEGER NOT NULL PRIMARY KEY
GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
NAME VARCHAR(30) NOT NULL,
EFTERNAVN VARCHAR(30) NOT NULL,
FOEDSELSDAG DATE NOT NULL,
SENG INTEGER UNIQUE CONSTRAINT FK_SENG
REFERENCES SENGE(ID_SENG) ON DELETE NO ACTION,
TIMESTAMP DATE
);
Entity klasserne:
Jeg har ændret Senge joint til OneToOne, da der ikke kan ligge flere patienter i en seng og kommenteret nogle andre joints ud mhp fejlfinding. Ud over det er det Netbeans autogenerede klasser.
- package db;
-
- import java.io.Serializable;
- import java.util.Collection;
- import javax.persistence.Basic;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.NamedQueries;
- import javax.persistence.NamedQuery;
- import javax.persistence.OneToMany;
- import javax.persistence.OneToOne;
- import javax.persistence.Table;
- import javax.xml.bind.annotation.XmlRootElement;
- import javax.xml.bind.annotation.XmlTransient;
-
- @Entity
- @Table(name = "SENGE")
- @XmlRootElement
- @NamedQueries({
- @NamedQuery(name = "Senge.findAll", query = "SELECT s FROM Senge s"),
- @NamedQuery(name = "Senge.findByIdSeng", query = "SELECT s FROM Senge s WHERE s.idSeng = :idSeng"),
- @NamedQuery(name = "Senge.findByName", query = "SELECT s FROM Senge s WHERE s.name = :name")})
- public class Senge implements Serializable {
- @OneToMany(cascade = CascadeType.ALL, mappedBy = "senge", fetch = FetchType.EAGER)
- private Collection<Tavle> tavleCollection;
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Basic(optional = false)
- @Column(name = "ID_SENG")
- private Integer idSeng;
- @Column(name = "NAME")
- private String name;
- @OneToOne(mappedBy = "seng")
- private Collection<Patient> patientCollection;
- // @OneToMany(cascade = CascadeType.ALL, mappedBy = "senge")
- // private Collection<Tavle> tavleCollection;
-
- public Senge() {
- }
-
- public Senge(Integer idSeng) {
- this.idSeng = idSeng;
- }
-
- public Integer getIdSeng() {
- return idSeng;
- }
-
- public void setIdSeng(Integer idSeng) {
- this.idSeng = idSeng;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @XmlTransient
- public Collection<Patient> getPatientCollection() {
- return patientCollection;
- }
-
- public void setPatientCollection(Collection<Patient> patientCollection) {
- this.patientCollection = patientCollection;
- }
-
- // @XmlTransient
- // public Collection<Tavle> getTavleCollection() {
- // return tavleCollection;
- // }
- //
- // public void setTavleCollection(Collection<Tavle> tavleCollection) {
- // this.tavleCollection = tavleCollection;
- // }
-
- @Override
- public int hashCode() {
- int hash = 0;
- hash += (idSeng != null ? idSeng.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 Senge)) {
- return false;
- }
- Senge other = (Senge) object;
- if ((this.idSeng == null && other.idSeng != null) || (this.idSeng != null && !this.idSeng.equals(other.idSeng))) {
- return false;
- }
- return true;
- }
-
- @Override
- public String toString() {
- return "db.Senge[ idSeng=" + idSeng + " ]";
- }
-
- @XmlTransient
- public Collection<Tavle> getTavleCollection() {
- return tavleCollection;
- }
-
- public void setTavleCollection(Collection<Tavle> tavleCollection) {
- this.tavleCollection = tavleCollection;
- }
-
- }
- package db;
-
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.Date;
- import javax.persistence.Basic;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.NamedQueries;
- import javax.persistence.NamedQuery;
- import javax.persistence.OneToMany;
- import javax.persistence.OneToOne;
- import javax.persistence.Table;
- import javax.persistence.Temporal;
- import javax.persistence.TemporalType;
- import javax.xml.bind.annotation.XmlRootElement;
- import javax.xml.bind.annotation.XmlTransient;
-
- @Entity
- @Table(name = "PATIENT")
- @XmlRootElement
- @NamedQueries({
- @NamedQuery(name = "Patient.findAll", query = "SELECT p FROM Patient p"),
- @NamedQuery(name = "Patient.findAllAdmitted", query = "SELECT p FROM Patient p WHERE p.seng IS NOT NULL"),
- @NamedQuery(name = "Patient.findByIdPatient", query = "SELECT p FROM Patient p WHERE p.idPatient = :idPatient"),
- @NamedQuery(name = "Patient.findByName", query = "SELECT p FROM Patient p WHERE p.name = :name"),
- @NamedQuery(name = "Patient.findByEfternavn", query = "SELECT p FROM Patient p WHERE p.efternavn = :efternavn"),
- @NamedQuery(name = "Patient.findByFoedselsdag", query = "SELECT p FROM Patient p WHERE p.foedselsdag = :foedselsdag"),
- @NamedQuery(name = "Patient.findByTimestamp", query = "SELECT p FROM Patient p WHERE p.timestamp = :timestamp")})
- public class Patient implements Serializable {
- @JoinColumn(name = "SPECIALE", referencedColumnName = "ID_SPECIALE")
- @ManyToOne(fetch = FetchType.EAGER)
- private Speciale speciale;
- @OneToMany(mappedBy = "patient", fetch = FetchType.EAGER)
- private Collection<Plejeteam> plejeteamCollection;
- @OneToMany(mappedBy = "patient", fetch = FetchType.EAGER)
- private Collection<Tavle> tavleCollection;
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Basic(optional = false)
- @Column(name = "ID_PATIENT")
- private Integer idPatient;
- @Basic(optional = false)
- @Column(name = "NAME")
- private String name;
- @Basic(optional = false)
- @Column(name = "EFTERNAVN")
- private String efternavn;
- @Basic(optional = false)
- @Column(name = "FOEDSELSDAG")
- @Temporal(TemporalType.DATE)
- private Date foedselsdag;
- @Column(name = "TIMESTAMP")
- @Temporal(TemporalType.DATE)
- private Date timestamp;
- // @JoinColumn(name = "SPECIALE", referencedColumnName = "ID_SPECIALE")
- // @ManyToOne
- // private Speciale speciale;
- @JoinColumn(name = "SENG", referencedColumnName = "ID_SENG")
- @OneToOne
- private Senge seng;
- // @OneToMany(mappedBy = "patient")
- // private Collection<Plejeteam> plejeteamCollection;
- // @OneToMany(mappedBy = "patient")
- // private Collection<Tavle> tavleCollection;
-
- public Patient() {
- }
-
- public Patient(Integer idPatient) {
- this.idPatient = idPatient;
- }
-
- public Patient(Integer idPatient, String name, String efternavn, Date foedselsdag) {
- this.idPatient = idPatient;
- this.name = name;
- this.efternavn = efternavn;
- this.foedselsdag = foedselsdag;
- }
-
- public Integer getIdPatient() {
- return idPatient;
- }
-
- public void setIdPatient(Integer idPatient) {
- this.idPatient = idPatient;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getEfternavn() {
- return efternavn;
- }
-
- public void setEfternavn(String efternavn) {
- this.efternavn = efternavn;
- }
-
- public Date getFoedselsdag() {
- return foedselsdag;
- }
-
- public void setFoedselsdag(Date foedselsdag) {
- this.foedselsdag = foedselsdag;
- }
-
- public Date getTimestamp() {
- return timestamp;
- }
-
- public void setTimestamp(Date timestamp) {
- this.timestamp = timestamp;
- }
-
- // public Speciale getSpeciale() {
- // return speciale;
- // }
- //
- // public void setSpeciale(Speciale speciale) {
- // this.speciale = speciale;
- // }
-
- public Senge getSeng() {
- return seng;
- }
-
- public void setSeng(Senge seng) {
- this.seng = seng;
- }
-
- // @XmlTransient
- // public Collection<Plejeteam> getPlejeteamCollection() {
- // return plejeteamCollection;
- // }
- //
- // public void setPlejeteamCollection(Collection<Plejeteam> plejeteamCollection) {
- // this.plejeteamCollection = plejeteamCollection;
- // }
- //
- // @XmlTransient
- // public Collection<Tavle> getTavleCollection() {
- // return tavleCollection;
- // }
- //
- // public void setTavleCollection(Collection<Tavle> tavleCollection) {
- // this.tavleCollection = tavleCollection;
- // }
-
- @Override
- public int hashCode() {
- int hash = 0;
- hash += (idPatient != null ? idPatient.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 Patient)) {
- return false;
- }
- Patient other = (Patient) object;
- if ((this.idPatient == null && other.idPatient != null) || (this.idPatient != null && !this.idPatient.equals(other.idPatient))) {
- return false;
- }
- return true;
- }
-
- @Override
- public String toString() {
- return "db.Patient[ idPatient=" + idPatient + " ]";
- }
-
- public Speciale getSpeciale() {
- return speciale;
- }
-
- public void setSpeciale(Speciale speciale) {
- this.speciale = speciale;
- }
-
- @XmlTransient
- public Collection<Plejeteam> getPlejeteamCollection() {
- return plejeteamCollection;
- }
-
- public void setPlejeteamCollection(Collection<Plejeteam> plejeteamCollection) {
- this.plejeteamCollection = plejeteamCollection;
- }
-
- @XmlTransient
- public Collection<Tavle> getTavleCollection() {
- return tavleCollection;
- }
-
- public void setTavleCollection(Collection<Tavle> tavleCollection) {
- this.tavleCollection = tavleCollection;
- }
-
- }
Kan nogen gennemskue hvad der er galt, eller hvordan jeg kommer videre med fejlfinding?
Mvh
Karin