SQL-запросы
Код: Выделить всё
DROP TABLE IF EXISTS ausstellung;
DROP TABLE IF EXISTS veranstaltung_ausstellung;
DROP TABLE IF EXISTS veranstaltung;
CREATE TABLE veranstaltung(
veranstaltung_id SERIAL,
titel varchar(100) not null,
programm_id int not null,
ausstellung_id int,
PRIMARY KEY(veranstaltung_id),
FOREIGN KEY(programm_id) REFERENCES paedagogischesProgramm(programm_id),
FOREIGN KEY(ausstellung_id) REFERENCES ausstellung(ausstellung_id)
);
CREATE TABLE ausstellung (
ausstellung_id SERIAL,
name varchar(100) not null,
datum_von date not null,
datum_bis date not null,
typ ausstellung_typ not null,
PRIMARY KEY(ausstellung_id)
);
CREATE TABLE veranstaltung_ausstellung (
veranstaltung_id int not null,
ausstellung_id int,
PRIMARY KEY (veranstaltung_id),
FOREIGN KEY (veranstaltung_id) REFERENCES veranstaltung(veranstaltung_id),
FOREIGN KEY(ausstellung_id) REFERENCES ausstellung(ausstellung_id)
);
INSERT INTO veranstaltung_ausstellung (veranstaltung_id, ausstellung_id) VALUES
(1, 18),
(2, NULL),
(3, NULL),
(4, 19),
(5, NULL),
(6, 20),
(7, NULL),
(8, NULL),
(9, NULL),
(10, NULL),
(11, NULL),
(12, NULL),
(13, NULL),
(14, NULL);
JAVA< /p>
Persistence.xml
Код: Выделить всё
org.hibernate.jpa.HibernatePersistenceProvider
DB.Ausstellung
DB.AusstellungBesucher
DB.AusstellungInventar
DB.AusstellungMitarbeiter
DB.Besucher
DB.BesucherPaedagogischesprogramm
DB.Feedback
DB.Inventar
DB.InventarKuenstlerHersteller
DB.InventarTyp
DB.Mitarbeiter
DB.Paedagogischesprogramm
DB.PlzOrt
DB.Veranstaltung
DB.VeranstaltungMitarbeiter
DB.VeranstaltungAusstellung
package DB;
Класс VeranstaltungAusstellungId
Код: Выделить всё
package Helpers;
import java.io.Serializable;
import java.util.Objects;
public class VeranstaltungAusstellungId implements Serializable {
private int veranstaltungId;
private int ausstellungId;
public VeranstaltungAusstellungId() {
}
public VeranstaltungAusstellungId(int veranstaltungId, int ausstellungId) {
this.veranstaltungId = veranstaltungId;
this.ausstellungId = ausstellungId;
}
public int getVeranstaltungId() {
return veranstaltungId;
}
public void setVeranstaltungId(int veranstaltungId) {
this.veranstaltungId = veranstaltungId;
}
public int getAusstellungId() {
return ausstellungId;
}
public void setAusstellungId(int ausstellungId) {
this.ausstellungId = ausstellungId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VeranstaltungAusstellungId that = (VeranstaltungAusstellungId) o;
return veranstaltungId == that.veranstaltungId && ausstellungId == that.ausstellungId;
}
@Override
public int hashCode() {
return Objects.hash(veranstaltungId, ausstellungId);
}
}
Код: Выделить всё
package DB;
import ENUM.VeranstaltungTyp;
import jakarta.persistence.*;
import java.time.LocalDate;
import java.util.Set;
@Entity(name = "Veranstaltung")
@Table(name = "veranstaltung")
public class Veranstaltung {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "veranstaltung_id", nullable = false)
private int veranstaltungId;
@Column(name = "titel", length = 100, nullable = false)
private String titel;
@Enumerated(EnumType.STRING)
@Column(name = "typ", nullable = false)
private VeranstaltungTyp veranstaltungTyp;
@Column(name = "datum_von", nullable = false)
private LocalDate datumVon;
@Column(name = "datum_bis", nullable = false)
private LocalDate datumBis;
@ManyToOne
@JoinColumn(name = "programm_id", nullable = false)
private Paedagogischesprogramm paedagogischesprogramm;
@ManyToMany
@JoinTable(
name = "veranstaltung_ausstellung",
joinColumns = @JoinColumn(name = "veranstaltung_id"),
inverseJoinColumns = @JoinColumn(name = "ausstellung_id")
)
private Set ausstellungen;
public Veranstaltung() {
}
public int getVeranstaltungId() {
return veranstaltungId;
}
public void setVeranstaltungId(int veranstaltungId) {
this.veranstaltungId = veranstaltungId;
}
public String getTitel() {
return titel;
}
public void setTitel(String titel) {
this.titel = titel;
}
public VeranstaltungTyp getVeranstaltungTyp() {
return veranstaltungTyp;
}
public void setVeranstaltungTyp(VeranstaltungTyp veranstaltungTyp) {
this.veranstaltungTyp = veranstaltungTyp;
}
public LocalDate getDatumVon() {
return datumVon;
}
public void setDatumVon(LocalDate datumVon) {
this.datumVon = datumVon;
}
public LocalDate getDatumBis() {
return datumBis;
}
public void setDatumBis(LocalDate datumBis) {
this.datumBis = datumBis;
}
public Paedagogischesprogramm getPaedagogischesprogramm() {
return paedagogischesprogramm;
}
public void setPaedagogischesprogramm(Paedagogischesprogramm paedagogischesprogramm) {
this.paedagogischesprogramm = paedagogischesprogramm;
}
public Set getAusstellungen() {
return ausstellungen;
}
public void setAusstellungen(Set ausstellungen) {
this.ausstellungen = ausstellungen;
}
}
Код: Выделить всё
import Helpers.VeranstaltungAusstellungId;
import jakarta.persistence.*;
@Entity(name = "VeranstaltungAusstellung")
@Table(name = "veranstaltung_ausstellung")
@IdClass(VeranstaltungAusstellungId.class)
public class VeranstaltungAusstellung {
@Id
@Column(name = "ausstellung_id", nullable = false)
private int ausstellungId;
@Id
@Column(name = "veranstaltung_id", nullable = false)
private int veranstaltungId;
@ManyToOne
@JoinColumn(name = "ausstellung_id", insertable = false, updatable = false)
private Ausstellung ausstellung;
@ManyToOne
@JoinColumn(name = "veranstaltung_id", insertable = false, updatable = false)
private Veranstaltung veranstaltung;
public VeranstaltungAusstellung() {
}
public Ausstellung getAusstellung() {
return ausstellung;
}
public void setAusstellung(Ausstellung ausstellung) {
this.ausstellung = ausstellung;
}
public Veranstaltung getVeranstaltung() {
return veranstaltung;
}
public void setVeranstaltung(Veranstaltung veranstaltung) {
this.veranstaltung = veranstaltung;
}
}
Код: Выделить всё
package DB;
import ENUM.AusstellungTyp;
import jakarta.persistence.*;
import java.time.LocalDate;
@Entity(name = "Ausstellung")
@Table(name = "ausstellung")
public class Ausstellung {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ausstellung_id", updatable = false, nullable = false)
private int ausstellungId;
@Column(name = "name", nullable = false, length = 100)
private String name;
@Column(name = "datum_von", nullable = false)
private LocalDate datumVon;
@Column(name = "datum_bis", nullable = false)
private LocalDate datumBis;
@Enumerated(EnumType.STRING)
@Column(name = "typ", nullable = false)
private AusstellungTyp ausstellungTyp;
public Ausstellung() {
}
public int getAusstellungId() {
return ausstellungId;
}
public void setAusstellungId(int ausstellungId) {
this.ausstellungId = ausstellungId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getDatumVon() {
return datumVon;
}
public void setDatumVon(LocalDate datumVon) {
this.datumVon = datumVon;
}
public LocalDate getDatumBis() {
return datumBis;
}
public void setDatumBis(LocalDate datumBis) {
this.datumBis = datumBis;
}
public AusstellungTyp getAusstellungTyp() {
return ausstellungTyp;
}
public void setAusstellungTyp(AusstellungTyp ausstellungTyp) {
this.ausstellungTyp = ausstellungTyp;
}
}
Код: Выделить всё
import DB.Ausstellung;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import jakarta.persistence.TypedQuery;
import java.util.List;
public class Main {
private static EntityManagerFactory EMF = Persistence.createEntityManagerFactory("ausstellung");
public static void main(String[] args) {
displayAllAusstellungen();
EMF.close();
}
private static void displayAllAusstellungen() {
EntityManager em = EMF.createEntityManager();
String query = "SELECT a FROM Ausstellung a";
TypedQuery tq = em.createQuery(query, Ausstellung.class);
List ausstellungList = null;
try {
ausstellungList = tq.getResultList();
ausstellungList.forEach(ausstellung -> System.out.println(
"Id: " + ausstellung.getAusstellungId() +
", name: " + ausstellung.getName() +
", typ: " + ausstellung.getAusstellungTyp() +
", datum von: " + ausstellung.getDatumVon() +
"- bis: " + ausstellung.getDatumBis()
));
} catch (Exception e) {
e.printStackTrace();
} finally {
em.close();
}
}
}
Код: Выделить всё
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.hibernate.cfg.RecoverableException: A '@JoinColumn' references a column named 'id' but the target entity 'DB.Ausstellung' has no property which maps to this column
at org.hibernate.boot.model.internal.AnnotatedJoinColumns.getReferencedColumnsType(AnnotatedJoinColumns.java:328)
at org.hibernate.boot.model.internal.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:149)
at org.hibernate.boot.model.internal.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:114)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1914)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1828)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.coordinateProcessors(MetadataBuildingProcess.java:358)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:226)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1431)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1502)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:57)
at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:90)
at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:66)
at Main.(Main.java:12)
Caused by: org.hibernate.MappingException: A '@JoinColumn' references a column named 'id' but the target entity 'DB.Ausstellung' has no property which maps to this column
at org.hibernate.boot.model.internal.AnnotatedJoinColumns.getReferencedColumnsType(AnnotatedJoinColumns.java:322)
... 12 more
Я проверил все известные мне орфографические ошибки или неправильные конфигурации.
Подробнее здесь: https://stackoverflow.com/questions/786 ... a-ee-where