Sprign Data JDBC doesnt save my Domain

I have these classes:
public class KarteikarteSet
private UUID fachId;
private UUID modulFachId;
private String name;
private String beschreibung;
private String lernIntervalle;
private List<Karteikarte> karteikarten;

public class Karteikarte {
private UUID fachId;
private String frage;
private String antwort;
private List<Antwort> antworten;
private LocalDateTime erstelltAm;
private LocalDateTime letzteAenderungAm;
private LocalDateTime zuletztGelerntAm;
private String notiz;
private int level;
private FrageTyp frageTyp;
private int antwortzeitSekunden;
}

public class Antwort {
private String antwort;
private Integer karteikarte;
}

public interface KarteikartenSetDao extends CrudRepository<KarteikartenSetDto, Integer> {
List<KarteikartenSetDto> findAll();

}
public class KarteikarteSet
private UUID fachId;
private UUID modulFachId;
private String name;
private String beschreibung;
private String lernIntervalle;
private List<Karteikarte> karteikarten;

public class Karteikarte {
private UUID fachId;
private String frage;
private String antwort;
private List<Antwort> antworten;
private LocalDateTime erstelltAm;
private LocalDateTime letzteAenderungAm;
private LocalDateTime zuletztGelerntAm;
private String notiz;
private int level;
private FrageTyp frageTyp;
private int antwortzeitSekunden;
}

public class Antwort {
private String antwort;
private Integer karteikarte;
}

public interface KarteikartenSetDao extends CrudRepository<KarteikartenSetDto, Integer> {
List<KarteikartenSetDto> findAll();

}
This is my schema:
create table karteikarte_set(
id serial primary key,
fach_id uuid,
modul_fach_id uuid,
name varchar(255) not null,
beschreibung text,
lern_intervalle varchar(100)
);

create table karteikarte(
id serial primary key,
fach_id uuid,
frage text,
antwort text,
erstellt_am timestamp not null,
letzte_aenderung_am timestamp not null,
zuletzt_gelernt_am timestamp not null,
notiz text,
level int,
frage_typ varchar(20),
antwortzeit_sekunden int,
karteikarte_set int references karteikarte_set(id),
karteikarte_set_key int
);

create table antwort(
id serial primary key,
antwort text,
karteikarte int references karteikarte(id),
karteikarte_key int
);
create table karteikarte_set(
id serial primary key,
fach_id uuid,
modul_fach_id uuid,
name varchar(255) not null,
beschreibung text,
lern_intervalle varchar(100)
);

create table karteikarte(
id serial primary key,
fach_id uuid,
frage text,
antwort text,
erstellt_am timestamp not null,
letzte_aenderung_am timestamp not null,
zuletzt_gelernt_am timestamp not null,
notiz text,
level int,
frage_typ varchar(20),
antwortzeit_sekunden int,
karteikarte_set int references karteikarte_set(id),
karteikarte_set_key int
);

create table antwort(
id serial primary key,
antwort text,
karteikarte int references karteikarte(id),
karteikarte_key int
);
32 Replies
JavaBot
JavaBot6d ago
This post has been reserved for your question.
Hey @Suika! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Suika
SuikaOP6d ago
I have this test:
void test_01() {
KarteikarteSet s = KarteikartenSetMother.initSet();

KarteikarteSet saved = repository.save(s);

assertThat(saved).isEqualTo(s);
}
void test_01() {
KarteikarteSet s = KarteikartenSetMother.initSet();

KarteikarteSet saved = repository.save(s);

assertThat(saved).isEqualTo(s);
}
but it says: Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO "antwort" ("antwort", "karteikarte", "karteikarte_key", "karteikarte_set", "karteikarte_set_key") VALUES (?, ?, ?, ?, ?)] why is it trying to insert something into karteikarte_set and karteikarte_set_key when there is no relation stated?
ayylmao123xdd
ayylmao123xdd6d ago
did you mark the classes as @Entity oh you arent using jpa right try adding those fields to the antwort table see if that helps
dan1st
dan1st6d ago
What is KarteikartenSetDto?
ayylmao123xdd
ayylmao123xdd6d ago
do you think it happens cuz theres no field when creating the tables
dan1st
dan1st6d ago
more like because there's a field that isn't in the DB possible a field that's in some classes but not others
ayylmao123xdd
ayylmao123xdd6d ago
oh yea i wrote that wrong
dan1st
dan1st6d ago
Also it would be know what exactly repository is - it doesn't seem to be KarteikartenSetDao
ayylmao123xdd
ayylmao123xdd6d ago
oh yea it looks like it saves the dto instead of the actual object in that dao
dan1st
dan1st6d ago
the test doesn't match that Also it would be good to see the full stack trace
ayylmao123xdd
ayylmao123xdd6d ago
ping him or something ig nvm i will @Suika
Suika
SuikaOP6d ago
hi sec the dto is not the problem i think everything worked when i didnt add List<Antwort> to my Karteikarte class the dto is to decouple data jdbc annotations from the domain
dan1st
dan1st6d ago
you have 1:n relation
Suika
SuikaOP6d ago
man the stacktrace is 100 lines
dan1st
dan1st6d ago
and it's a List so the information with set the card belongs to is stored in the card at least that's what Spring JDBC assumes so it expects "antwort" to contain a karteikarte_set referencing the karteikarte_set and also contain a karteikarte_set containing the index in the list
Suika
SuikaOP6d ago
but there is no relation bewteen antwort and karteikarte_set onkly to karteikarte and it does not show this error here:
create table test
(
id serial primary key,
test_titel varchar(50),
ergebnis_veroffentlicht boolean
);

create table frage(
id serial primary key,
test int references test(id),
test_key int,
titel varchar(60),
frage text,
loesung text,
punkte int,
typ varchar(20)
);

create table aussage(
id serial primary key,
frage int references frage(id),
frage_key int,
aussage varchar(200),
antwort boolean
);
create table test
(
id serial primary key,
test_titel varchar(50),
ergebnis_veroffentlicht boolean
);

create table frage(
id serial primary key,
test int references test(id),
test_key int,
titel varchar(60),
frage text,
loesung text,
punkte int,
typ varchar(20)
);

create table aussage(
id serial primary key,
frage int references frage(id),
frage_key int,
aussage varchar(200),
antwort boolean
);
its basically the same test has List<Frage> and Frage has List<Aussage> i dont get an error that aussage need test_key or something
dan1st
dan1st6d ago
Why would it?
Suika
SuikaOP6d ago
because its the same schema im only saving reference to frage not to test
dan1st
dan1st6d ago
ah I see
Suika
SuikaOP6d ago
if i have karteikarte_set and karteikarte_set_key, then karteikarteSet would have a List<Antwort> but doesnt and neednt *it
dan1st
dan1st6d ago
Can you show the full test and stack trace?
Suika
SuikaOP6d ago
No description
Suika
SuikaOP6d ago
No description
dan1st
dan1st6d ago
Can you post it as text?
Suika
SuikaOP6d ago
dan1st
dan1st6d ago
Is KarteikartenSetRepositoryImpl a class you created or is it generated?
Suika
SuikaOP6d ago
created
dan1st
dan1st6d ago
Can you show it?
Suika
SuikaOP6d ago
@Repository
public class KarteikartenSetRepositoryImpl implements KarteikartenSetRepository {

private KarteikartenSetDao dao;

public KarteikartenSetRepositoryImpl(KarteikartenSetDao dao) {
this.dao = dao;
}

@Override
public KarteikarteSet save(KarteikarteSet set) {
return toKarteikartenSet(dao.save(toKarteikartenSetDto(set)));
}
@Repository
public class KarteikartenSetRepositoryImpl implements KarteikartenSetRepository {

private KarteikartenSetDao dao;

public KarteikartenSetRepositoryImpl(KarteikartenSetDao dao) {
this.dao = dao;
}

@Override
public KarteikarteSet save(KarteikarteSet set) {
return toKarteikartenSet(dao.save(toKarteikartenSetDto(set)));
}
dan1st
dan1st6d ago
Can you show your KarteikartenSetDto?
Suika
SuikaOP6d ago
@Table("karteikarte_set")
public record KarteikartenSetDto(@Id Integer id,
UUID fachId,
UUID modulFachId,
String name,
String beschreibung,
String lernIntervalle,
List<Karteikarte> karteikarten) {
}
@Table("karteikarte_set")
public record KarteikartenSetDto(@Id Integer id,
UUID fachId,
UUID modulFachId,
String name,
String beschreibung,
String lernIntervalle,
List<Karteikarte> karteikarten) {
}
as far as i know i would not need a KarteikarteDto or a AntwortDto
JavaBot
JavaBot5d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?