The project is a Museum Control project. The problem I am facing is that the “ausstellung” table has the ausstellung_id serial value and the “veranstaltung” table has the veranstaltung_id serial value. In the “veranstaltung_ausstellung” table is a relation between these two. However, here can ausstellung_id be NULL. IMO the problem comes from here, but all of the code and database query and data is provided. Java 21 with Jakarta EE and PostgreSQL.
SQL Queries
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);
All of the tables are checked and filled with appropriate data.
JAVA
Persistence.xml
<!-- Create Persistence-Unit -->
<persistence-unit name="ausstellung" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>DB.Ausstellung</class>
<class>DB.AusstellungBesucher</class>
<class>DB.AusstellungInventar</class>
<class>DB.AusstellungMitarbeiter</class>
<class>DB.Besucher</class>
<class>DB.BesucherPaedagogischesprogramm</class>
<class>DB.Feedback</class>
<class>DB.Inventar</class>
<class>DB.InventarKuenstlerHersteller</class>
<class>DB.InventarTyp</class>
<class>DB.Mitarbeiter</class>
<class>DB.Paedagogischesprogramm</class>
<class>DB.PlzOrt</class>
<class>DB.Veranstaltung</class>
<class>DB.VeranstaltungMitarbeiter</class>
<class>DB.VeranstaltungAusstellung</class>
<properties>
<property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1:1111/Project"/>
<property name="jakarta.persistence.jdbc.user" value="user"/>
<property name="jakarta.persistence.jdbc.password" value="password"/>
</properties>
</persistence-unit>
</persistence>package DB;
the credentials are correct obviously
VeranstaltungAusstellungId class
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);
}
}
Veranstaltung class
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<Ausstellung> 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<Ausstellung> getAusstellungen() {
return ausstellungen;
}
public void setAusstellungen(Set<Ausstellung> ausstellungen) {
this.ausstellungen = ausstellungen;
}
}
VeranstaltungAusstellung Class
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;
}
}
Ausstellung class
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;
}
}
Main
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<Ausstellung> tq = em.createQuery(query, Ausstellung.class);
List<Ausstellung> 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();
}
}
}
Errors when run the process:
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.<clinit>(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
I tried to make the ausstellung_id Primary Key in the “ausstellung_veranstaltung” table, but in this case it can’t be null, which is not appropriate for this project.
I checked all of the misspellings or wrong configurations, which I am aware of.
b_leone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.